Consider the general problem of finding a root of f, that is, find a solution to
ID: 3110451 • Letter: C
Question
Consider the general problem of finding a root of f, that is, find a solution to the equation f(x) = 0. The Steffensen's iteration is x_n + 1 = x_n - f(x_n)/g(x_n), g(x_n) = f(x_n + f(x_n)) - f(x_n)/f(x_n), with x_0 as an initial guess of the root. The project has the following parts: Write a computer program that will implement Steffensen's method. Consider the problem of finding a solution to the equation x = cos(x). Use your computer implementation of Steffensen's method with initial guess x_0 = 1 to compute at least the first 12 iterations. Calculate the error at each iteration e_n = x_n - alpha, where alpha is the exact solution. Use alpha = 0.7390851332151607.Explanation / Answer
clear all
format long
P=12; % Number of iteration
f=@(xs) xs-cos(xs); % Function declaration
xn=1; % initial guess
alpha=0.7390851332151607; % exact root
x(1)=xn;
for n=1:P
gxn=(f(xn+f(xn))-f(xn))/f(xn);
x(n+1)=xn-f(xn)/gxn; % updating the root
xn=x(n+1);
E(n)=xn-alpha; % error calculation
if E(n)==0
break;
end
end
x(1) = 0.762327191814211, e(1) = 0.0232420585990502
x(2) = 0.739390924029096, e(2) = 0.000305790813935558
x(3) = 0.739085188387237, e(3) = 5.51720759078833e-08
x(4) = 0.739085133215162, e(4) = 1.77635683940025e-15
x(5) = 0.739085133215161, e(5) = 0
Hence after 5 iterations, we get the exact root.