I want to loop by giving the C stops at a value less than 0.0001.

1 次查看(过去 30 天)
Hi all, I tried to work on bisection method. However, that is an error at the loop. Please help me correct them. Thank you,
A=input('Enter A: ');
B=input('Enter B: ');
func = @(x) exp(x)-2*cos(x);
for i=1:10
C=(A+B)/2
FC=func(C);
FB=func(B);
if(FC*FB>0)
B=C;
else
A=C;
end
end

回答(1 个)

Mischa Kim
Mischa Kim 2015-6-17
编辑:Mischa Kim 2015-6-17
Angkhana, almost there.
A = input('Enter A: ');
B = input('Enter B: ');
func = @(x) exp(x)-2*cos(x);
err = 1e-4;
while abs(func(B))>err
C = (A+B)/2;
FC = func(C);
FB = func(B)
if (FC*FB>0)
B = C;
else
A = C;
end
end
I recommend using a while loop with an adequate breaking condition, though.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by