0% found this document useful (0 votes)
50 views1 page

Bisection Method for Root Finding

This MATLAB code implements the bisection method to find the root of a function. It takes in a function, initial bracketing values for the root, a tolerance value, and maximum number of iterations. It iteratively computes the midpoint of the bracketing interval and checks if the function value at that point is positive or negative to shrink the interval toward the root. It returns the computed root once the tolerance is reached or max iterations are exceeded.

Uploaded by

Yonda Kedua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views1 page

Bisection Method for Root Finding

This MATLAB code implements the bisection method to find the root of a function. It takes in a function, initial bracketing values for the root, a tolerance value, and maximum number of iterations. It iteratively computes the midpoint of the bracketing interval and checks if the function value at that point is positive or negative to shrink the interval toward the root. It returns the computed root once the tolerance is reached or max iterations are exceeded.

Uploaded by

Yonda Kedua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

clc

clear
function [root]=bisection(fun, x, tol, maxit)
if fun(x(1))>0 then
xu=x(1);xl=x(2);
else
xu=x(2);xl=x(1);
end
Ea=1;
iter=1;
while(1)
xr(iter)=(xl(iter)+xu(iter))/2;
if fun(xr(iter))>0 then
xu(iter+1)=xr(iter);
xl(iter+1)=xl(iter);
elseif fun (xr(iter))<0 then
xl(iter+1)=xr(iter);
xu(iter+1)=xu(iter);
else
break
end
if iter>1 then
Ea(iter)=100*abs((xr(iter)-xr(iter-1))/xr(iter));
end
if Ea(iter)<tol|iter==maxit then
break
end
iter=iter+1
end
root=xr(iter);
endfunction
function f=fun1(x)
f=0.99403-1.1+1.671*10^(-4)*x+9.7215*10^(-8)*x^2-9.5838*10^(-11)*x^3+1.9520*10^(-
14)*x^4;
endfunction
x=[0 1200];
tol=1e-4;
maxit=100;
root=bisection(fun1,x,tol,maxit);
disp(root,"root=")
root=

544.08731

You might also like