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

function M = bisection_theorem(a,b,tol) Mtemp = (log(b-a)-log(tol))/log(2); M=ce

ID: 3282762 • Letter: F

Question

function M = bisection_theorem(a,b,tol)
Mtemp = (log(b-a)-log(tol))/log(2);
M=ceil(Mtemp);

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Let f(x) = x?4 ?2x?3 ?4x??2 + 4x + 4.

a) Using Matlab, use the above bisection_theorem.m to ?nd the number of iterations M needed to achieve an approximation to the solution of x?4 ?2x?3 ?4x??2 + 4x + 4= 0 with tolerance 10-3, lying in each of the intervals: [?2,?1] [2,3] [?1,0] [0,2]

(b) After?nding M?s,use bisection.m to ?nd the approximation to the root(in each interval)with this tolerance, making sure to note the actual number of iterations. Comment on whether the actual number of iterations needed matches the theoretical M obtained.

Explanation / Answer

function dummy=bisc()
clc;
clear all;

f=@(x)x^4-2*x^3-4*x^2+4*x+4; %function
tol=1e-3; %tolerence
%%%%%%%%%%%%%%%%%%%% First nterval %%%%%%%%%%%%%
disp('Root on interval [-2 -1]')
a=-2;b=-1; % interval
[c ,niter]=bisecion(f,a,b,tol) % function calling
%%%Theoritical iterations
Mtemp = (log(b-a)-log(tol))/log(2)
M=ceil(Mtemp)

%%%%%%%%%%%%%%%%%%%% second nterval %%%%%%%%%%%%%
disp('Root on interval [2 3]')  
a1=2;b1=3; % interval
[c ,niter]=bisecion(f,a1,b1,tol) % function calling
%%%Theoritical iterations
Mtemp = (log(b1-a1)-log(tol))/log(2)
M=ceil(Mtemp)
  
  
%%%%%%%%%%%%%%%%%%%% third nterval %%%%%%%%%%%%%
disp('Root on interval [-1 0]')
a2=-1;b2=0; % interval
[c ,niter]=bisecion(f,a2,b2,tol) % function calling
%%%Theoritical iterations
Mtemp = (log(b2-a2)-log(tol))/log(2)
M=ceil(Mtemp)
  
%%%%%%%%%%%%%%%%%%%% fourth nterval %%%%%%%%%%%%%
disp('Root on interval [0 2]')
a3=0;b3=2; % interval
[c ,niter]=bisecion(f,a3,b3,tol) % function calling
%%%Theoritical iterations
Mtemp = (log(b3-a3)-log(tol))/log(2)
M=ceil(Mtemp)
  
  
function [c ,niter]=bisecion(f,a,b,tol)
% f=@(x)x^4-2*x^3-4*x^2+4*x+4;
%initial interval
% a=1;
% b=2;

if(f(a)*f(b)>0)
fprintf('Root is not applicable in [%f, %f] ',a,b)
else
% tol = 1e-3;
% eps_step = 1e-4;

Nmax=500;
niter=0;
while ((b -a)/2 >= tol && niter<=Nmax )
  
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);
niter=niter+1;

end


end
end
end

%%%%% Solution%%

Root on interval [-2 -1]

c =

-1.416015625000000


niter =

9


Mtemp =

9.965784284662087


M =

10

Root on interval [2 3]

c =

2.732421875000000


niter =

9


Mtemp =

9.965784284662087


M =

10

Root on interval [-1 0]

c =

-0.732421875000000


niter =

9


Mtemp =

9.965784284662087


M =

10

Root on interval [0 2]

c =

1.416015625000000


niter =

10


Mtemp =

10.965784284662087


M =

11

>>