Problems with while loop

2 次查看(过去 30 天)
Hi, I have a problem, I want the subtraction d1 to be less than 1 * 10 ^ -15 after several iterations, but the program stays busy.
  2 个评论
James Tursa
James Tursa 2020-8-31
Please post your code as regular text and highlight it with the CODE button. We can't run pictures.
Tamia Eli
Tamia Eli 2020-8-31
编辑:Matt J 2020-8-31
Sorry, it is this code:
clear
format LONGG
a=6373878;
f=1/297;
k0=0.9996;
e2=2*f-f^2;
e=sqrt(e2);
c0=1+((3/4)*(e^2))+((45/64)*(e^4))+((175/256)*(e^6))+((11025/16384)*(e^8))+((43659/65536)*(e^10));
c2=((3/4)*(e^2))+((15/16)*(e^4))+((525/512)*(e^6))+((2205/2048)*(e^8))+((72765/65536)*(e^10));
c4=((15/64)*(e^4))+((105/256)*(e^6))+((2205/4096)*(e^8))+((10395/16384)*(e^10));
c6=((35/512)*(e^6))+((315/2048)*(e^8))+((31185/131072)*(e^10));
c8=((315/16384)*(e^8))+((3465/65536)*(e^10));
c10=((693/131072)*(e^10));
x=483250.07981339
y=2303647.10551245
zone=39;
hemis='s';
if(strcmp(hemis,'n')||strcmp(hemis,'N'))
siglat=1;
else
if(strcmp(hemis,'s')||strcmp(hemis,'S'))
siglat=-1;
else
fprintf('Wrong');
end
end
if siglat==-1
y=y-10000000;
end
x=x-500000;
tt=1;
phi1=(y/k0)/(a*(1-e2)*c0);
while tt>1e-15
B1=a*(1-e2)*(c0*phi1-c2/2*sin(2*phi1)+c4/4*sin(4*phi1)-c6/6*sin(6*phi1)+c8/8*sin(8*phi1)-c10/10*sin(10*phi1));
d1=(y/k0)-B1;
d1r=d1/(a*(1-e2)*c0);
phi1=phi1+d1r;
tt=abs(d1);
end
phip=phi1 %phi'

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2020-8-31
编辑:Bruno Luong 2020-8-31
"Hi, I have a problem, I want the subtraction d1 to be less than 1 * 10 ^ -15 after several iterations, but the program stays busy."
Well you cannot demand floating point error to be that small.
Double IEEE has about 15 digits relative precision. You compare B1 to (y/k0) which is -7699432.66755457. The most you can demand is error is about
>> tol = eps(y/k0)
tol =
9.31322574615479e-10
So if you replace the break condition by
tol = eps(y/k0);
while tt>tol
...
end
your while loop will stop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by