(30 points) Write a MATLAB code to perform the Bisection method of root finding.
ID: 3870146 • Letter: #
Question
(30 points) Write a MATLAB code to perform the Bisection method of root finding. Write the code to output the table used in class showing the iteration, lower bound, function value at the lower bound, upper bound, function value at the upper bound, approximate root, function value at the approximate root, and the approximate error. Show that the code works by using it to re-solve Homework Assignment 1 Problem 2b, which asked you to find the positive root of f(x)-5x2-3r-4 using z-1.0 and Tu = 2.0, have the code iterate until the approximate error is less than 1%.Explanation / Answer
PLEASE REFER BELOW CODE
close all
clear all
clc
f=@(x) 5*x^2 - 3*x - 4;
xl=1.0;
xu=2.0;
x_mid = (xl + xu)/2;
%checking initial condition
if f(xu)*f(xl) < 0
else
fprintf('The guess is incorrect! ');
end
fprintf('xl f(xl) xu f(xu) xmid f(xmid) ')
%running loop for 1000 iteration to get closer root and converge solution
for i=2:1000
xr=(xu+xl)/2;
if f(xu)*f(xr)<0
xl=xr;
else
xu=xr;
end
fprintf('%f %f %f %f %f %f ', xl,f(xl),xu,f(xu),xr,f(xr));
if f(xl)*f(xr)<0
xu=xr;
else
xl=xr;
end
xnew(1)=0;
xnew(i)=xr;
%if approx error is less than 1% then we are terminating loop
if abs((xnew(i)-xnew(i-1))/xnew(i)) < 0.01
break
end
end
fprintf('The required root of the equation is: %f ', xr);
PLEASE REFER BELOW OUTPUT
xl f(xl) xu f(xu) xmid f(xmid)
1.000000 -2.000000 1.500000 2.750000 1.500000 2.750000
1.000000 -2.000000 1.250000 0.062500 1.250000 0.062500
1.125000 -1.046875 1.250000 0.062500 1.125000 -1.046875
1.187500 -0.511719 1.250000 0.062500 1.187500 -0.511719
1.218750 -0.229492 1.250000 0.062500 1.218750 -0.229492
1.234375 -0.084717 1.250000 0.062500 1.234375 -0.084717
1.242188 -0.011414 1.250000 0.062500 1.242188 -0.011414
The required root of the equation is: 1.242188
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.