Not sure why the loop's not working??

1 次查看(过去 30 天)
the loop parts suppose to repeat until (abs((a+b)^2/(4*t)-pi)<= thres).
a=1;
b=1/sqrt(2);
t=1/4;
x=1;
thres = 0.001;
while abs((a+b)^2/( 4*t )-pi) > thres
y=a;
a=(a+b)/2;
b=sqrt(b)*sqrt(y);
t=t-(x)*(y-a)^2;
x=2*x;
if (abs((a+b)^2/(4*t)-pi)<= thres)
break
mypi= (a+b)^2/(4*t);
end
end
  2 个评论
Image Analyst
Image Analyst 2013-5-13
Matthew - please change this to an answer, rather than a comment.

请先登录,再进行评论。

采纳的回答

Matthew Doveton
Matthew Doveton 2013-5-13
mypi is never reached due to the positioning of the break statement, should be:
a=1; b=1/sqrt(2); t=1/4; x=1; thres = 0.001;
while abs((a+b)^2/( 4*t )-pi) > thres
y=a;
a=(a+b)/2; b=sqrt(b)*sqrt(y);
t=t-(x)*(y-a)^2; x=2*x;
if (abs((a+b)^2/(4*t)-pi)<= thres)
mypi = (a+b)^2/(4*t);
break
end
end

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by