MATLAB The following question needs to be programmed in MATL
MATLAB) The following question needs to be programmed in MATLAB.
Please make sure the it has codes related to the ratio and the convergence that is stated in the question as well!
10. Test the secant method on an example in which r, f\'(r), and f \"(r) are known in advance. Monitor the ratios en 1-1/(enen to see whether they converge to en-1) f\' r). The function f (x) arctan x is suit able for this experiment.Solution
The Matlab function Seant_method.m
function Secant_method(f,x0,x1,tol,n) % secant method for finding root of f(x)
 iter=1;% intial iteration value
 u=f(x0);% function value at x0
 v=f(x1);% function value at x1
 err(iter)=abs(x1-x0);% error
 % displaying the result in tabular form
 disp(\' iter      f(x)            x     e(n+1)/(e(n)e(n-1))\')
 disp(\'___________________________________________________\')
 fprintf(\'%2.0f %12.6f %12.6f\ \',iter-1,u,x0); %result to the screen
 while (err(iter)>tol)&&(iter<=n)&&((v-u)~=0) % loop to until converg or reach N iter
       iter=iter+1;% increase number of iteration
       x=x1-v*(x1-x0)/(v-u);% new approximation of root
       x0=x1; % replace the old value in x0 with x1
       u=v; % replace the old functional value f(x0) with f(x1)
       x1=x;% replace the old value in x1 with new value
       v=f(x1);% function value at new x1
       err(iter)=abs(x1-x0);% error
       if iter < 3
           fprintf(\'%2.0f %12.6f %12.6f\ \',iter-1,u,x0); %result to the screen
       else
           fprintf(\'%2.0f %12.6f %12.6f %12.6f\ \',iter-1,u,x0,err(iter)/(err(iter-1)*err(iter-2))); %result to the screen
       end
 end
 if ((v-u)==0)% Checking devision by zero occured or not
       error(\' Division by zero\')% if yes display error message
 elseif (iter>n)% checking method diverge or not
       error(\' Method failed to converge\')% if yes display message
 end
 end
Calling the function
>> f =@(x) atan(x);
 >> df =@(x) 1./(x.^2+1);
 >> ddf = @(x) (-2*x)./(x.^2+1).^2;
 >> x0 = -10;x1 = 2;tol = 10^-15;n=100;
 >> Secant_method(f,x0,x1,tol,n)
 iter      f(x)            x     e(n+1)/(e(n)e(n-1))
 ___________________________________________________
 0     -1.471128    -10.000000
 1      1.107149      2.000000
 2     -1.263671     -3.152971     0.044418
 3     -0.385999     -0.406386     0.085349
 4      0.675691      0.801560     0.231717
 5      0.032776      0.032787     0.042204
 6     -0.006404     -0.006404     0.212623
 7      0.000002      0.000002     0.007353
 8     -0.000000     -0.000000     0.002134
 9      0.000000      0.000000     0.000001
 >> fprintf(\'The ratio -ddf/(2 df) at root x = 0 is %f\ \',-ddf(0)/(2*df(0)));
 The ratio -ddf/(2 df) at root x = 0 is 0.000000
>>


