0% found this document useful (0 votes)
154 views2 pages

Mian Kashif Ali

This document describes using the bisection method in MATLAB to find the root of a function. It defines two functions: InitialGuess to obtain an initial guess for the root interval, and BisectionMethod which implements the bisection algorithm using InitialGuess. It provides the MATLAB code for these functions, defines the specific 5th order polynomial function to find the root of, and demonstrates calling all the functions to calculate the root as 0.579327.

Uploaded by

Mian Kashif Ali
Copyright
© Attribution Non-Commercial (BY-NC)
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)
154 views2 pages

Mian Kashif Ali

This document describes using the bisection method in MATLAB to find the root of a function. It defines two functions: InitialGuess to obtain an initial guess for the root interval, and BisectionMethod which implements the bisection algorithm using InitialGuess. It provides the MATLAB code for these functions, defines the specific 5th order polynomial function to find the root of, and demonstrates calling all the functions to calculate the root as 0.579327.

Uploaded by

Mian Kashif Ali
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 2

Q: Calculate the root of the given function using Bisection Method by MATLAB Programming.

Answer: We first define the General Bisection Method Function to find the root of the any function.
We divide this work and use two functions
1. Initial Guess function name InitialGuess.
2. Bisection Method function that uses the InitialGuess function name BisectionMethod.
Now code for InitialGuess function is given below
function [r,s]=InitialGuess(func) %%for initial guess
c=input('\n\nDo you want to enter the root interval manually?\npress 1 for
manual or press 0 for Automatic= ');
if(c)
ri=input('Enter initial point= ');
si=input('\nEnter last point= ');
r=ri;
s=si;
else
ri=-1;
si=1;
if (((feval('func',ri))*(feval('func',si)))<0)
r=ri;
s=si;
else

while(((feval('func',ri))*(feval('func',si)))>=0)
ri=ri-1;
si=si+1;
end
r=ri;
s=si;

end
end

Now code for BisectionMethod function is given below


function root=BisectionMethod(func)
[Domain1, Domain2]=InitialGuess('func');
rs= Domain1;
re=Domain2;
rm=(rs+re)/2;
rmo=rm;

fs=feval('func', rs);
fe=feval('func', re);
fm=feval('func', rm);
if ((fs<=0) && (fe>=0)&& (fm>0))
re=rm;
elseif ((fs<=0) && (fe>=0)&& (fm<0))
rs=rm;
elseif ((fs>=0) && (fe<=0)&& (fm>0))
rs=rm;
elseif ((fs>=0) && (fe<=0)&& (fm<0))
re=rm;
elseif ((fs>=0) && (fe>=0))
display('You have selected an invalid interval, Fuction never changed
it Sign');
display('you can select automatic option to select interval');
root=BisectionMethod('func')
elseif ((fs<=0) && (fe<=0))
display('You have selected an invalid interval, Fuction never changed
it Sign');
display('you can select automatic option to select interval');
root=BisectionMethod('func')
end
for i=1:1:50
fs=feval('func', rs);
fe=feval('func', re);
rm=(rs+re)/2;
fm=feval('func', rm);
if ((fs<=0) && (fe>=0)&& (fm>0))
re=rm;
elseif ((fs<=0) && (fe>=0)&& (fm<0))
rs=rm;
elseif ((fs>=0) && (fe<=0)&& (fm>0))
rs=rm;
elseif ((fs>=0) && (fe<=0)&& (fm<0))
re=rm;
end
end
root=rm;

Now the function whose root is to be found is given by the Problem 5.3 of the text book as
f ( x )=−26+82.3 x−88 x 2+ 45.4 x 3−9 x 4 +0.65 x 5
The Matlab code for this function is given below
function f=func(x)
f=((-26)+(82.3*x)-(88*(x^2))+(45.4*(x^3))-(9*(x^4))+(0.65*(x^5)));
end

Now to calculate the root of the above function we make a Matlab file that call all the functions, the code
is given below
display('WELCOME');
display('ROOT CALCULATOR USING BISECTION METHOD');
display('LAST MODIFICATION DATE: 26-12-2010');

root=BisectionMethod('func');
fprintf('The root is = %f', root)

When we execute this program the results are given below


WELCOME
ROOT CALCULATOR USING BISECTION METHOD
LAST MODIFICATION DATE: 26-12-2010

Do you want to enter the root interval manually?


press 1 for manual or press 0 for Automatic= 0
The root is = 0.579327>>

You might also like