MATLAB Answers

Need assistance using bisection method to find equation roots in Matlab function.

30 views (last 30 days)
Jaden Evans
Jaden Evans on 21 Feb 2021
Commented: Jaden Evans on 22 Feb 2021
I'm currently taking a course using Matlab, and I am having trouble finding the error in this code I wrote. The goal is to find the zero of a quadratic equation using the bimethod approach. However, my code outputs the wrong answer everytime, and I do not see my mistake. Can anyone help me?
% input for function that we're finding the roots of
f1 = @(x) ((-0.6)*x^2)+(2.4*x)+(5.5);
xl = 5; % xl sets the inital low value for the code
xu = 10; % xu sets the initial high value for the code
i = 0; % i is the number of iterations that are to be performed
while i <= 15 % this sets the number of iterations the loop is allowed to compute
i = i + 1;
% This if function is testing if the product of fxl and fxm is positive or
% negative. The sign of this value determines which bound is to be replaced
% with the midpoint, cutting the sample size in half.
if f1(xl) * f1(xu) > 0
xu = xm; % xu replaces the value of xm
xm = (xl + xu)/2; % xm is the "midpoint" of the initial low and high values
else
xl = xm; % xl replaces the value of xm
xm = (xl + xu)/2; % xm is the "midpoint" of the initial low and high values
end
end
root = f1(xu);
error_a = (abs(f1(xu)) - f1(xl))/(f1(xu)); % approximate error function
error_t = (abs(f1(xm)) - 5.62859)/5.62859; % true error function
xl_inital = 5;
xu_inital = 10;
fprintf('The zero of the input function between %d and %d is %f.\n',xl_inital,xu_inital,root);
fprintf('The approximate error is %f and the true error is %f.\n',error_a,error_t);
This is the output:
The zero of the input function between 5 and 10 is -10.255035.
The approximate error is -1.999951 and the true error is 0.821955.

Accepted Answer

Alan Stevens
Alan Stevens on 22 Feb 2021
Try this
% input for function that we're finding the roots of
f1 = @(x) ((-0.6)*x^2)+(2.4*x)+(5.5);
xl = 5; % xl sets the inital low value for the code
xu = 10; % xu sets the initial high value for the code
i = 0; % i is the number of iterations that are to be performed
while i <= 15 % this sets the number of iterations the loop is allowed to compute
i = i + 1;
xm = (xl + xu)/2;
% This if function is testing if the product of fxl and fxm is positive or
% negative. The sign of this value determines which bound is to be replaced
% with the midpoint, cutting the sample size in half.
if f1(xm) * f1(xu) > 0
xu = xm; % xu replaces the value of xm
else
xl = xm; % xl replaces the value of xm
end
end
root = f1(xu);
% you are confusing function values with x values below!!
% error_a = abs((f1(xu) - f1(xl))/f1(xu)); % approximate error function
% error_t = abs((f1(xm) - 5.62859)/5.62859); % true error function
error_a = abs((xu - xl)/xm);
error_t = abs((xm - 5.62859)/5.62859);
xl_inital = 5;
xu_inital = 10;
fprintf('The zero of the input function between %d and %d is at x = %f.\n',xl_inital,xu_inital,xm);
fprintf('The approximate error is %f and the true error is %f.\n',error_a,error_t);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!