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 endendroot = 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.
Best Answer