In: Advanced Math
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature
function output = solve(f,options)
% your code here
end
where
the input is
the output is also a struct type with the following fields
600x^4 – 550x^3 +200x^2 – 20x -1 = 0
function dummy=bisc_newton_secant()
clc;
clear all;
f=@(x)600*x^4 - 550*x^3 +200*x^2-20*x -1; %function
fp=@(x)600*4*x^3 - 550*3*x^2 +200*2*x-20; % derivative of
unction
tol=1e-8;
a=0;
b=2; % interval
x0=0.5;
disp('Root by Bisection method')
y=bisecion(f,a,b,tol) % function calling
% a1=0;
% b1=1; % interval
% disp('Root by fixed point method')
% C1=fixedp(g,x0,tol) % function calling
disp('Root by Newton method')
y2=newt(f,x0,tol)% function calling
a2=0.5;
b2=2; % interval
disp('Root by Secant method')
y1=secn(f,a2,b2,tol)% function calling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bisection
method%%%%%%%%%%%
function c=bisecion(f,a,b,tol)
if(f(a)*f(b)>0)
fprintf('Root is not applicable in [%f, %f]\n',a,b)
else
disp('_____________________________________________________')
disp('n p_n ea')
disp('_____________________________________________________')
err=0.1;
k=1;
while ( err>tol )
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
err=abs( (b-a)/2);
k=k+1;
fprintf('%f\t%10f \t %10f\n',k,c,err)
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Secant method
%%%%%%%%%%%%%%%%%%%%%%
function x1=secn(f,a,b,tol)
x(1)=a;
x(2)=b;
err=0.1;
%secont method
i=2;
disp('_____________________________________________________')
disp('x f(x) error')
disp('_____________________________________________________')
while (abs(err) > tol & i<=100)
x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
err (i-1)= abs(x(i+1)-x(i));
i = i+1;
end
for j=1:i-2
fprintf('%f\t%10f \t %10f \n',x(j),f(x(j)),err(j))
end
x1=x(end);
err1=err(end);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Newton method %%%%%%%%%%%%%%%%%%%%%%
function x=newt(f,x0,tol)
k=1;
err=0.1;
x=x0;
disp('_____________________________________________________')
disp('x f(x) error')
disp('_____________________________________________________')
while(err>tol)
x1=x-(f(x)/fp(x)); %newton method
err=abs(x-x1);
x=x1;
k=k+1;
fprintf('%f\t%10f \t %10f \n',x,f(x),err)
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Result%%%%%%%%%%%%
Root by Bisection method
_____________________________________________________
n p_n ea
_____________________________________________________
2.000000 1.000000 0.500000
3.000000 0.500000 0.250000
4.000000 0.250000 0.125000
5.000000 0.125000 0.062500
6.000000 0.187500 0.031250
7.000000 0.218750 0.015625
8.000000 0.234375 0.007813
9.000000 0.226563 0.003906
10.000000 0.230469 0.001953
11.000000 0.232422 0.000977
12.000000 0.231445 0.000488
13.000000 0.231934 0.000244
14.000000 0.232178 0.000122
15.000000 0.232300 0.000061
16.000000 0.232361 0.000031
17.000000 0.232330 0.000015
18.000000 0.232346 0.000008
19.000000 0.232353 0.000004
20.000000 0.232349 0.000002
21.000000 0.232351 0.000001
22.000000 0.232352 0.000000
23.000000 0.232353 0.000000
24.000000 0.232353 0.000000
25.000000 0.232353 0.000000
26.000000 0.232353 0.000000
27.000000 0.232353 0.000000
28.000000 0.232353 0.000000
y =
0.2324
Root by Newton method
_____________________________________________________
x f(x) error
_____________________________________________________
0.385185 2.745637 0.114815
0.281283 0.714026 0.103903
0.234849 0.034931 0.046434
0.232358 0.000068 0.002491
0.232353 0.000000 0.000005
0.232353 0.000000 0.000000
y2 =
0.2324
Root by Secant method
_____________________________________________________
x f(x) error
_____________________________________________________
0.500000 7.750000 1.501953
2.000000 5959.000000 0.001923
0.498047 7.619192 0.113670
0.496124 7.492449 0.063088
0.382454 2.674183 0.063160
0.319366 1.337855 0.021495
0.256206 0.339699 0.002312
0.234710 0.032990 0.000045
0.232398 0.000636 0.000000
0.232353 0.000001 0.000000
y1 =
0.2324
>>