Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. For the following question your submission should contain 3 matlab functions a

ID: 3210246 • Letter: #

Question

. For the following question your submission should contain 3 matlab functions and a matlab script.

(a) Create a Matlab program that applies Newton’s method for a given function f, it’s derivative f1, an initial guess x0, and a predetermined tolerance for residue tol. This program should output x: the estimate for the root of f with residue smaller then tol, n: the number of iterations the program used to reach the specified tolerance.

(b) Create a Matlab program that applies secant method for a given function f, initial guesses x0 and x1, and a predetermined tolerance for residue tol. This program should output x: the estimate for the root of f with residue smaller then tol, n: the number of iterations the program used to reach the specified tolerance.

(c) Create a Matlab program that applies the bisection method for a given function f, initial guesses a and b, and a predetermined tolerance for residue tol. This program should output x: the estimate for the root of f with residue smaller then tol, n: the number of iterations the program used to reach the specified tolerance.

(d) Create a Matlab script that uses the functions created in (a), (b) and (c) for the function f = x 27 10 with tolerances 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001, 0.00000001, 0.000000001, 0.0000000001. Also take x0=1 for (a), x0=5,x1=15 for (b) and a=0, b=15 for (c).

(e) Report and compare the results of these calculations as comments on your script file.

Explanation / Answer

function dummy=Three_methods()
clc;
clear all;
f=@(x)x^27-10;
fp=@(x)27*x.^26;
x0=1;
x01=5;
x02=15;
a=0; b=15;
N=100;
tol1=[1e-1 1e-2 1e-3 1e-4 1e-5 1e-6 1e-7 1e-8 1e-9 1e-10 ] ;
disp('Newton method')
disp('____________________________________________')
disp( ' root      num_iteration       tolerence' )
disp('____________________________________________')
for k=1:10
[y1,n,tol]=Newtons_method(f,fp,x0,tol1(k),N);
y2(k)=y1;
n2(k)=n;
tol2(k)=tol;
fprintf('%f %10f %10e ',y2(k), n2(k), tol2(k))

end
disp('secant method')
disp('____________________________________________')
disp( ' root        num_iteration         tolerence' )
disp('____________________________________________')
for k=1:10
[x1,i,tol]=Secnt_method(f,x01,x02,tol1(k));
y2(k)=x1;
n2(k)=i;
tol2(k)=tol;
fprintf('%f %10f %10e ',y2(k), n2(k), tol2(k))
end
disp('bisection method')
disp('____________________________________________')
disp( ' root      num_iteration      tolerence' )
disp('____________________________________________')
for k=1:10
[ xs, n, tol]=bisecroot(f,a,b,tol1(k));
y2(k)=xs;
n2(k)=n;
tol2(k)=tol;
fprintf('%f %10f %10e ',y2(k), n2(k), tol2(k))
end

function[y1,n,tol]=Newtons_method(f,fp,x0,tol,N)
%x0 initial guess
%f function
% fp derivative of f
%tol tolerence
%N max iterations
%     f = inline(f);
%     fp = inline(fp);
n=0;
err=1;
while (abs(err>tol)& (n<=N))
   
y1=x0-(f(x0)/fp(x0));%Newton method

err=abs((y1-x0)); %erorr
x0=y1;
y1;
% if abs(erorr<1e-3)
% break
%end
n=1+n;
end
end
   function[x1,i,tol]=Secnt_method(f,x01,x02,tol)
x(1)=x01;
x(2)=x02;
eps1=1;
i=1;
while (abs(eps1) > tol)
x1=x02-(f(x02)*(x02-x01)/(f(x02)-f(x01)));
eps1 = abs((x1-x02));


x01=x02;
x02=x1;
i = i+1;

end
end
%
%
function[ xs, n, tol]=bisecroot(f,a,b,tol)
n=1;
N_max=500;
while (b - a >= tol || ( abs( f(a) ) >= tol && abs( f(b) ) >= tol ) )
    xs = (a + b)/2; % midpoint
    if ( f(xs) == 0 )
       break;
    elseif ( f(a)*f(xs) < 0 )
       b = xs;
    else
       a = xs;
    end
%     C(n)=xs;
    err(n)=abs(b-a);
    n=n+1;

end
end
end

%%% Solution

Newton method
____________________________________________
root      num_iteration       tolerence
____________________________________________
1.284160       2.000000    1.000000e-01
1.089814       8.000000    1.000000e-02
1.089030       9.000000    1.000000e-03
1.089023      10.000000    1.000000e-04
1.089023      10.000000    1.000000e-05
1.089023      11.000000    1.000000e-06
1.089023      11.000000    1.000000e-07
1.089023      11.000000    1.000000e-08
1.089023      11.000000    1.000000e-09
1.089023      12.000000    1.000000e-10
secant method
____________________________________________
root        num_iteration         tolerence
____________________________________________
5.000000       3.000000    1.000000e-01
5.000000       3.000000    1.000000e-02
5.000000       3.000000    1.000000e-03
5.000000       3.000000    1.000000e-04
5.000000       3.000000    1.000000e-05
5.000000       3.000000    1.000000e-06
5.000000       3.000000    1.000000e-07
5.000000       3.000000    1.000000e-08
5.000000       3.000000    1.000000e-09
5.000000       3.000000    1.000000e-10
bisection method
____________________________________________
root      num_iteration      tolerence
____________________________________________
1.089020      16.000000    1.000000e-01
1.089020      16.000000    1.000000e-02
1.089020      16.000000    1.000000e-03
1.089023      23.000000    1.000000e-04
1.089023      29.000000    1.000000e-05
1.089023      29.000000    1.000000e-06
1.089023      34.000000    1.000000e-07
1.089023      34.000000    1.000000e-08
1.089023      35.000000    1.000000e-09
1.089023      43.000000    1.000000e-10
>>