Bisection method in matlab

9 次查看(过去 30 天)
Dai Nguyen
Dai Nguyen 2020-10-2
HI I wanna graph the bisection method with the function that I have but Idk how to do it. I also want to Iterate until the relative approximate error falls below 0.01% or the number of iterations exceeds 100. this is what I have so far but for some
this is the code
clc
clear
lc=3; lp=3; w=160;
T= 700;
f=@(d) (w*lc*lp/(d*sqrt(lp^2-d^2)))-T;
xl=input('enter the value for Xl');
xu=input('enter the value for Xu');
xm = (xu + xl) / 2;
if f(xl)*f(xu)>0
xmnew=xl
xunew=xu
%calculate the error
error=abs((xmnew-xm)/xmnew)
else if f(xl)*f(xu)<0
xmnew=xu
xlnew=xl
error=abs((xmnew-xm)/xmnew);
else
fprintf('xm is the root')
end
end
  3 个评论
Dai Nguyen
Dai Nguyen 2020-10-2
I don't really know how to create the loop for the iteration, can you help me?
Dai Nguyen
Dai Nguyen 2020-10-2
is it for i=1:xm in this case

请先登录,再进行评论。

回答(1 个)

Asad (Mehrzad) Khoddam
clc
clear
lc=3; lp=3; w=160;
T= 700;
f=@(d) (w*lc*lp/(d*sqrt(lp^2-d^2)))-T;
xl=input('enter the value for Xl ');
xu=input('enter the value for Xu ');
for i=1:100
xm = (xu + xl) / 2;
error=abs(xm-xu)/xm;
if error< 0.01
break;
end
if f(xl)*f(xm)>0
xl = xm;
elseif f(xl)*f(xu)<0
xu = xm;
else
fprintf('%f is the root\n', xm)
break;
end
end

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by