



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions on how to use matlab to approximate the square root of two using newton's method and the secant method. Code examples and explanations of the results obtained from each method. It also compares the number of iterations required for each method and discusses the advantages and disadvantages of each method.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Math 151A Homework #2 – due Wednesday 10/18, in class
Show all your work!
2 to within 10−^4. As usual, submit your code and a list of intermediate approximations.
a. Use Newton’s method with an initial guess p 0 = 1. How does this compare with the bisection method (from last week’s homework), in terms of number of iterations required? Answer: Create a function file called f.m containing: function f = f(x) f = x.^2 - 2; end and another called f1.m containing the first derivative: function f1 = f1(x) f1 = 2*x; end and a script called newton.m containing, for example: %---------------------------------------------------------- % Matlab/Octave program to find the root of a function % using Newton’s method. % - the function must be defined in f.m, and its % derivative in f1.m %----------------------------------------------------------
% set initial guess p0, and the desired precision p0 = 1; tol = 1.0e-4;
% prevent infinite loops, by setting a maximum % number of iterations maxits = 1000;
for i = 1:maxits
% compute the subsequent approximation p1 = p0 - f(p0)/f1(p0);
% nicely formatted printing of each approximation printf("%.2d: p = %.8f\n",i,p1);
% check to see if we are at the root if f(p1)==
break endif
% check to see if we’ve reached the desired precision if( abs(p1-p0) < tol) break endif
% new approximation becomes old aproximation p0 = p1;
end Then run the script: octave:1> newton 01: p = 1. 02: p = 1. 03: p = 1. 04: p = 1.
Notice that we converge much faster than with the bisection method (from last week’s homework).
b. Use Newton’s method with an initial guess p 0 = 0.0001. What happens to the number of iterations required, and why? Answer: Modify the newton.m code to start from initial condition p0 = 0.0001 and run it: octave:2> newton 01: p = 10000. 02: p = 5000. 03: p = 2500. 04: p = 1250. 05: p = 625. 06: p = 312. 07: p = 156. 08: p = 78. 09: p = 39. 10: p = 19. 11: p = 9. 12: p = 5. 13: p = 2. 14: p = 1. 15: p = 1. 16: p = 1. 17: p = 1. 18: p = 1.
octave:3> secant 01: p = 2. 02: p = 1. 03: p = 1. 04: p = 1. 05: p = 1. 06: p = 1. The number of iterations required is comparable to Newton’s method, although slightly more. This increase in number of iterations may be offset by not having to compute the derivative.
Answer: Obviously pn converges to 0, since limn→∞ 1 /n^2 = 0. Now consider the ratio:
pn+1 − p pn − p
1 /(n + 1)^2 1 /n^2
n^2 (n + 1)^2
lim n→∞
( (^) n 2
(n + 1)^2
= lim n→∞
n^2 n^2 + 2n + 1
= lim n→∞
2 n 2 n + 2
= lim n→∞
Which means that
lim n→∞
|pn+1 − p| |pn − p|
so we have convergence of order α = 1 with asymptotic error constant λ = 1. Note that this is not linear convergence, since linear convergence requires that α = 1 and λ < 1.
n converges quadratically to 0. Answer: Since limn→∞ 2 n^ = ∞, then limn→∞ 10 −^2 n = 0. Now consider the ratio:
pn+1 − p (pn − p)^2
n+ ( 10 −^2 n^
n· 2 ( 10 −^2 n^
n ) 2 ( 10 −^2 n^
So then
lim n→∞
|pn+1 − p| |pn − p|^2
and we have the order of convergence to be α = 2.
a. Show that p = 0 is a simple zero of f (x). Answer: We know that 0 is a zero of f , since f (0) = 0^3 + 4 · 02 + 8 · 0 = 0. It is a simple zero, since the first derivative f ′(0) = 3 · 02 + 8 · 0 + 8 6 = 0 is non-zero. b. Use Newton’s method to find the root to within 10−^6 , with initial guess p 0 = 1. Answer: We can use the same Newton’s method code from the first problem, with the function files f.m: function f = f(x) f = x.^3 + 4x.^2 + 8x; end and its derivative f1.m: function f1 = f1(x) f1 = 3x.^2 + 8x + 8; end which gives the result: octave:1> newton 01: p = 0. 02: p = 0. 03: p = 0. 04: p = 0. 05: p = 0.
Now consider the similar function g(x) = x^3 + 4x^2.
c. Show that p = 0 is a zero of multiplicity 2 of g(x). Answer: We know that 0 is a zero of g since g(0) = 0^3 + 4 · 02 = 0. It is not a simple zero, because the first derivtive is also zero at 0: g′(0) = 3 · 02 + 8 · 0 = 0. However, the second derivative is non-zero (g(2)(0) = 6 · 0 + 8 6 = 0), so 0 is a zero of g of multiplicity two. d. Use Newton’s method to find the root to within 10−^6 , with initial guess p 0 = 1. Answer:
octave:2> newton 01: p = 0. 02: p = 0. 03: p = 0. 04: p = 0. 05: p = 0. 06: p = 0. 07: p = 0. 08: p = 0. 09: p = 0.