Bisection method code - question.

1 次查看(过去 30 天)
Yuval
Yuval 2013-12-26
编辑: Matt J 2013-12-26
Hi, I wrote the following function for solving V=L[arccos(h/r)r^2 - h(r^2-h^2)^0.5] using the bisection method. However, as I execute the program it gets stuck, yet I cannot figure out why. I'd appreciate any comments.
function h = volume(V,L,r,h1,h2)
h=(h1+h2)/2;
err=abs(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2)));
while (err > 0.01)
if (V-L*(acosd(h1/r)*r^2-h1*sqrt(r^2-h1^2))*(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2))) < 0)
h2=h;
else
h1=h;
end
h=(h1+h2)/2;
err=abs(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2)));
end

回答(1 个)

Matt J
Matt J 2013-12-26
I think you're just missing some parentheses in
if (V-L*(acosd(h1/r)*r^2-h1*sqrt(r^2-h1^2))*(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2))) < 0)
  4 个评论
Yuval
Yuval 2013-12-26
编辑:Yuval 2013-12-26
Okay, now the following code doesn't get stuck but the answer seems to be inaccurate. Any idea why?
function h = volume(V,L,r,h1,h2)
h=(h1+h2)/2;
err=abs(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2)));
while (err > 0.01)
if ((V-L*(acosd(h1/r)*r^2-h1*sqrt(r^2-h1^2)))*(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2))) < 0)
h2=h;
else
h1=h;
end
h=(h1+h2)/2;
err=abs(V-L*(acosd(h/r)*r^2-h*sqrt(r^2-h^2)));
end
I mean, it keeps giving out h=0.9998, even when I change the while condition to (err > 2.009032E-11), for instance.
Matt J
Matt J 2013-12-26
编辑:Matt J 2013-12-26
What data are you using for V,L,r,h1,h2?

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by