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

(a) Write a (MATLAB) code bisection, m which solves a single equation f(x) = 0 b

ID: 3168378 • Letter: #

Question

(a) Write a (MATLAB) code bisection, m which solves a single equation f(x) = 0 by using bisection method. You will solve the equation f(z) =2+3x2-4x-12=0, whose roots are -3, -2, and 2. (b) Write a (MATLAB) code secant.m which solves the same equation f(3 +32 4x-12 = 0 by using secant method Pa Pn-1 f (p) f(Pn-1) (e) Attach the comparison of bisection. m and secant,m by one example, take al =-4, and bi =-2.5 for the bisection method and Po =-4, pi =-2.5 for the secant method. At each step of the iteration, the code prints out the number n, the value P the difference pn - Pn-1 for both the bisection method and the secant method. Terminate the program s when Pn-Pn-1 = 10-14.

Explanation / Answer

a) Bisection method

clc;
clear all;

f=@(x) x^3 +3*x^2 -4*x-12;
a=-4;
b=-2.5;


eps_abs = 1e-14;
eps_step = 1e-14;
n=0;
while ((b -a)/2 >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs )&& n~=100 )
n=n+1;
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);
end
n
c
f(c)
err

%%%Secont method

clc;
clear all;


f=@(x) x^3 +3*x^2 -4*x-12;
x(1)=-4;
x(2)=-2.5;
n=1;


tol=1e-14;
iteration=0;
i=2;
eps = 1e0;


%secont method
while (abs(eps) > tol & i<=100)
x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
eps (i)= abs(x(i+1)-x(i));
i = i+1;
x(i);
  
end
i;
x(end); %root
c=x(end);


disp('______________________________________________________________')
disp( 'itertion x_value error')
disp('______________________________________________________________')
for j=1:i-1
fprintf('%f %15f %15f ',j,x(j),abs(eps(j)))
end
x; %roots of three functions

______________________________________________________________

itertion x_value error

______________________________________________________________

1.000000 -4.000000 1.000000

2.000000 -2.500000 0.128571

3.000000 -2.628571 3.131334

4.000000 -5.759905 3.089868

5.000000 -2.670037 0.039117

6.000000 -2.709154 0.620795

7.000000 -3.329949 0.438637

8.000000 -2.891312 0.073896

9.000000 -2.965207 0.040122

10.000000 -3.005329 0.005558

11.000000 -2.999771 0.000228

12.000000 -2.999999 0.000001

13.000000 -3.000000 0.000000

14.000000 -3.000000 0.000000