Write a matlab code for the resolution of sqrt(x+2) - x=0 using first the fixed
ID: 3544550 • Letter: W
Question
Write a matlab code for the resolution of sqrt(x+2) - x=0 using first the fixed point iteration method, as well as the following....
Take x_0=0 and compute x_n+1=sqrt(x_n +2)
Define V_n as (x_n x_n+2 -x^2_n+1)/(x_n+2 -2x_n+1 +x_n)
Compare the convergence rate for solving by printing the errors, x_n+2 - l , v_n - l , where l is the exact solution, as well as the ratios |v_n - l|/|x_n+2 -l| , |v_n-l|/|v_n-1 -l|^k where k=1,2,....
Stop the iterations as soon as |v_n -v_n-1| < 10^-13
For the sake of clarity I've typed the question here : http://i.imgur.com/M472kEv.png
Explanation / Answer
clc
clear
format shortEng
f = @(x) sqrt(x+2)-x;
l = fzero(f,1); %exact solution
x0 = 0;
x1 = sqrt(x0+2);
x2 = sqrt(x1+2);
v0 = (x0*x2 - x1^2)/(x2-2*x1+x0);
err = 1;
n=1;
k=1;
while err > 10^(-13)
x3 = sqrt(x2+2);
v1 = (x1*x3 - x2^2)/(x3-2*x2+x1);
a = sprintf('x%d-l = %f v%d-l = %f',n+2,x3-l,n,v1-l);
disp(a)
r1 = abs(v1 - l)/abs(x3 - l);
r2 = abs(v1 - l)/(abs(v0 - l)^k);
b = sprintf('|v%d-l|/|x%d-l| = %f |v%d-l|/|v%d-l|^%d = %g ',n,n+2,r1,n,n-1,k,r2);
disp(b)
err = abs(v0-v1);
x2 = x3;
v0 = v1;
k = k+1;
n = n+1;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.