Department of Mechatronics Engineering University of Engineering & Technology, Peshawar
Department of Mechatronics Engineering University of Engineering & Technology, Peshawar
Department of Mechatronics Engineering University of Engineering & Technology, Peshawar
Lab No: 3
Bisection Method
Section: A
Software:
MATLAB
Theory:
Basically, we do computations and visualization in MATLAB. In this lab, we did the
computations. We learn how to write the polynomial in MATLAB and do arithmetic operations
on polynomial. We learn how to find roots and derivative of polynomial. We use many
commands like roots, polyval, polyder, poly, conv, deconv, and fprintf. In bisection method,
we used the conditional statements and loops.
if f(XL)==0
fprintf('XL is one of the root\n');
elseif f(XU) == 0
fprintf('XU is one of the root\n');
end
if f(XL)*f(XU)>0
fprintf('no roots exist in given interval\n');
end
if f(XL)*f(XU) < 0
fprintf(' XL XU XM\n'); %This line is just for
displaying lables in output
for i=1:20
XM = (XL+XU)/2;
fprintf('%.4f %.4f %.4f\n', XL, XU ,XM);
if f(XL) > 0 & f(XM) > 0 & f(XU) <0
XU = XU;
XL = XM;
elseif f(XL)>0 & f(XM) < 0 & f(XU) <0
XL = XL;
XU = XM;
elseif f(XL) <0 & f(XM) < 0 & f(XU) > 0
XL = XM;
XU = XU;
elseif f(XL) <0 & f(XM) > 0 & f(XU) > 0
XL = XL;
XU = XM;
end
end
fprintf('the final root is: %.4f ' ,XM);
end
Results:
2. 4x^2-7x+5
MATLAB Code:
%4*x^2-7*x+5
f = @(x) 4*x^2-7*x+5;
XL = input('enter value of XL: ');
XU = input('enter value of XU: ');
if f(XL)==0
fprintf('XL is one of the root\n');
elseif f(XU) == 0
fprintf('XU is one of the root\n');
end
if f(XL)*f(XU)>0
fprintf('no roots exist in given interval\n');
end
if f(XL)*f(XU) < 0
fprintf(' XL XU XM\n'); %This line is just for
displaying lables in output
for i=1:20
XM = (XL+XU)/2;
fprintf('%.4f %.4f %.4f\n', XL, XU ,XM);
if f(XL) > 0 & f(XM) > 0 & f(XU) <0
XU = XU;
XL = XM;
elseif f(XL)>0 & f(XM) < 0 & f(XU) <0
XL = XL;
XU = XM;
elseif f(XL) <0 & f(XM) < 0 & f(XU) > 0
XL = XM;
XU = XU;
elseif f(XL) <0 & f(XM) > 0 & f(XU) > 0
XL = XL;
XU = XM;
end
end
fprintf('the final root is: %.4f ' ,XM);
end
Results: