Programming Practice - Seoul National...
Transcript of Programming Practice - Seoul National...
![Page 1: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/1.jpg)
Programming Practice2018-11-22
Week 12
1
![Page 2: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/2.jpg)
Notice
2
![Page 3: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/3.jpg)
Final Project
• We are going to start final project from this week.
• Final project is long-term project.
• You have to do both homework and final project.
• More details will be introduced later.
3
![Page 4: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/4.jpg)
Practice Lecture
4
![Page 5: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/5.jpg)
Numerical Analysis
• Calculate approximate value rather than exact answer, using computer.• ex) find the root of ln(𝑥4+1) ∗ ln 𝑥2 + 1 = 7
• hard to solve mathematically
5
![Page 6: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/6.jpg)
Newton-Raphson Method
• Method for finding successively better approximations to the root of 𝑓 𝑥 = 0.
• Root of 𝑓 𝑥 = 0 equals to the point where 𝑦 = 𝑓(𝑥) and x-axis intersect.
6
reference: https://en.wikipedia.org/wiki/Newton%27s_method
![Page 7: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/7.jpg)
Newton-Raphson Method
• Start with initial value 𝑥0.
• Draw a tangent line at (𝑥0, 𝑓 𝑥0 ).• tangent line: a line with slope 𝑓′ 𝑥0
and passes through (𝑥0, 𝑓 𝑥0 ).
• new value 𝑥1 equals to the point wherethe tangent line and x-axis intersect.
• 𝑥1 is closer to the root c.
7
𝑥1 = 𝑥0 −𝑓(𝑥0)
𝑓′(𝑥0)
![Page 8: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/8.jpg)
Newton-Raphson Method
• Repeatedly, you can calculate 𝑥𝑖+1 from 𝑥𝑖 in a same way.
• 𝑥𝑖+1 is better approximation.
• Do until your approximation is sufficiently close to the root.
• How to know it is sufficient?• 𝑥𝑖 − 𝑥𝑖−1 ≤ 10−6
• This method is not always available – related to the shape of function & initial value (Poor initial value problem)
8
Use 𝑥𝑖+1 = 𝑥𝑖 −𝑓(𝑥𝑖)
𝑓′(𝑥𝑖)
![Page 9: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/9.jpg)
Newton-Raphson Method
• Example: 𝑓 𝑥 = 8𝑥3 − 12𝑥2 + 6𝑥 − 1 = 0, 𝑥0 = 0.8• 𝑓′ 𝑥 = 24𝑥2 − 24𝑥 + 6
• absolute answer is 0.5
9
![Page 10: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/10.jpg)
Summary
• Initial value 𝑥0 and equation 𝑓 𝑥 = 0 is given.
• Start with 𝑥0, calculate 𝑥𝑖+1 = 𝑥𝑖 −𝑓 𝑥𝑖
𝑓′ 𝑥𝑖repeatedly.
• Stop when 𝑥𝑖+1 − 𝑥𝑖 ≤ 10−6. That 𝑥𝑖+1 is the answer.
10
![Page 11: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/11.jpg)
HomeworkProblems
1. Newton-Raphson Method
11
![Page 12: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/12.jpg)
Problem.
Description
Input
Output
Sample
Newton-Raphson Method
1
12
• Your only job is to implement newtonMethod function which calculates and returns(do not print) the approximate root of 𝑓 𝑥 = 0 for arbitrary 𝑓(𝑥), 𝑓′(𝑥), and initial value 𝑥0.
• Provided skeleton code newton.c, implement newtonMethod and submit whole of it.
• Do not modify other functions : main(), f1(), df1(), f2(), df2().
• You can assume that the function will only handle the available situation.
• Absolute error is allowed up to 10^-4.
Function Prototype : double newtonMethod(double x0, double (*fp)(double), double (*dfp)(double))
x0: initial value 𝑥0fp: function pointer of 𝑓(𝑥)dfp: function pointer of 𝑓′(𝑥)
![Page 13: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/13.jpg)
Skeleton Code
Function Name Description
main()Reads information of f1(x), f2(x), and initial value of those. It will call newtonMethod(x01,f1,df1) and newtonMethod(x02,f2,df2), and print out the results.
f1(x) Prepared function to test newtonMethod.
df1(x) First derivative of f1(x).
f2(x) Another prepared function to test newtonMethod.
df2(x) First derivative of f2(x).
newtonMethod(x0,f,df) TODO :: implement newtonMethod function
![Page 14: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/14.jpg)
main()
• how does main call newtonMethod function
• f1, df1, f2, df2 are function pointers
14
![Page 15: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/15.jpg)
How to Test
• First line of input contains 4 integers a, b, c and d. f1 and f2 will be• 𝑓1 𝑥 = 𝑎𝑥3 + 𝑏𝑥2 + 𝑐𝑥 + 𝑑
• 𝑓2 𝑥 = 𝑏𝑥3 + 𝑎𝑥2 + 𝑐𝑥 + 𝑑
• Second line contains (𝑥0)1 and(𝑥0)2.• (𝑥0)1 is initial value for 𝑓1 𝑥
• (𝑥0)2 is initial value for 𝑓2 𝑥
• Each line of output is the approximate root of 𝑓1 𝑥 = 0 and 𝑓2 𝑥 = 0.
• Try various inputs and check your output with tools such as wolframalpha.
15
www.wolframalpha.com
![Page 16: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/16.jpg)
Wolframalpha
16
![Page 17: Programming Practice - Seoul National Universitymrl.snu.ac.kr/courses/CourseProgrammingPractice/data/... · 2018-11-22 · Programming Practice 2018-11-22 Week 12 1. Notice 2. Final](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0c96eb7e708231d436281b/html5/thumbnails/17.jpg)
Sample Testcases
[input] [output]4 3 2 1 -0.605830-1.5 -1.4 -1.000000
[input] [output]-1 3 5 7 4.4692205.5 -2.3 -0.863895
17