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

10 ビュー (過去 30 日間)
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.

採用された回答

Alan Stevens
Alan Stevens 2021 年 2 月 22 日
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);
  1 件のコメント
Jaden Evans
Jaden Evans 2021 年 2 月 22 日
Thank you so much for helping, I see what I was doing wrong. Now I can sleep in peace lol

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by