Example Sheet 2

advertisement
MATH49111/69111
Scientific Computing
October 2, 2013
Example Sheet 2
Example: Newton’s root finding algorithm
Use Newton’s method to find roots of f (x) = 0 where
f (x) = 9x4 − 42x3 − 1040x2 + 5082x − 5929.
Solution:
Create a function to find the root of
/ / function to find root of
double poly ( double x ){
r e t u r n ( ( ( 9 . ∗ x − 4 2 . ) ∗ x − 1 0 4 0 . ) ∗ x + 5 0 8 2 . ) ∗ x −5929;
}
its derivative
/ / derivative function to find root of
double p o l y d e r i v ( double x ){
return ( ( 3 6 . ∗ x − 126.)∗ x − 2080.)∗ x + 5 0 8 2 . ;
}
and a simple root finding function defined by:
/ / find the root of a function
/ / On i n p u t : x i s i n i t i a l g u e s s , On r e t u r n x i s t h e r o o t
/ / m a x i t e r i s t h e max no o f i t s , t o l i s t h e a c c u r a c y o f t h e r o o t
/ / 0 w i l l be r e t u r n e d i f no r o o t i s f o u n d
// 1 if it is successful
i n t f i n d r o o t ( d o u b l e& x , i n t m a x i t e r , d o u b l e t o l ) {
/ / find root
}
The algorithm to find the root is given by:
xn+1 = xn −
f (xn )
f 0 (xn )
so we may write the code inside the function as
/ / find root
int i t e r ;
f o r ( i t e r = 0 ; i t e r <m a x i t e r ; i t e r ++){
x = x − poly ( x ) / p o l y d e r i v ( x ) ;
i f ( s t d : : a b s ( p o l y ( x )) < t o l ) break ;
}
i f ( i t e r == m a x i t e r ) r e t u r n 0 ;
e l s e return 1;
Example use of the program is
1
MATH49111/69111
Scientific Computing
October 2, 2013
i n t i t e r =100;
d o u b l e x , t o l = 1 . e −6;
s t d : : c o u t << ” E n t e r i n i t i a l g u e s s \n ” ;
s t d : : c i n >> x ;
if ( find root (x , iter , tol ))
s t d : : c o u t << ” T h e r e i s a r o o t a t ” << x << ” \n ” ;
else
s t d : : c o u t << ” No r o o t f o u n d . \ n ” ;
Functions and Loops
(2.1)
Repeat the root finding algorithm using the Secant method:
xn = xn−1 −
f (xn−1 )(xn−1 − xn−2 )
f (xn−1 ) − f (xn−2 )
(2.2) Write a function to compute the value of sin(x) using the first N terms of the
power series expansion (N should be an argument to the function):
sin(x) ≈
N
X
(−1)k
k=0
x2k+1
(2k + 1)!
How many terms of the series are required to agree with the built-in sin function. Produce output of the two functions side by side for the values 0, π/6, π/4, π/2, 2π/3 and
π.
(2.3) Rewrite the above function for sin so that terms are added until the result is
accurate to 10 decimal places. Think about what condition is satisfied for this to happen
– i.e. what is the truncation error?
(2.4) Write a function that takes the arguments a, b and c, computes and then displays
the roots of the quadratic equation
ax2 + bx + c = 0.
You will need to identify whether the roots are real or complex. If the roots are complex, display the results in the form A + Bi.
(2.5)
Write functions to evaluate the following expressions.
1
1+γ 2
1. f (γ) = 2 tan−1
sin
γ
1−γ
2. g(x) =
√1
2π
log(x3 + 1)e−x
2
Using x = 0.14, a = 0.235 and b = 1.763, evaluate y = f (g(x)) and
y = g(f (x + a) + g(b)).
2
MATH49111/69111
Scientific Computing
October 2, 2013
(2.6) Write a function to calculate the nth Fibonacci number. Recall that the Fibonacci numbers, fn , are defined by
f0 = 0, f1 = 1, fn = fn−1 + fn−2
Use your function to calculate and display the Fibonacci quotient, qn = fn /fn−1 , for
a number of different values
of n. You should find that as n increases qn converges to
√
the golden mean, (1 + 5)/2 ≈ 1.618.
(2.7)
(*) The recurrence relation
19
1
, pn =
pn−1 − 2pn−2 , (n ≥ 2)
3
3
n
has the unique solution pn = 13 , n ≥ 0.
Write a C++ program to compute the terms n = 0 up to n = 20 of the series using the
data type float to store variables. Output the results to a file with columns showing
the following quantities:
p0 = 1,
n,
p1 =
pn (analytical),
pn (numerical),
relative error,
absolute error
(2.8) (*) Run your program again this time using double. Is there any difference
between the results? Can you think of a reason why the results are different?
Coursework
Please complete the starred problems and submit your solution by 4pm Wednesday
9nd October. Solutions should preferably be submitted using the TurnItIn system on
blackboard! You should submit a single document (can be written with latex or any
other office package you choose) containing
• Title page should contain your student id number.
• program - Doesn’t need annotations or supporting text but you may add comments.
This exercise is worth 4% of the total mark for the course.
3
Download