Problems with while loop

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 Comments

Please post your code as regular text and highlight it with the CODE button. We can't run pictures.
Tamia Eli
Tamia Eli on 31 Aug 2020
Edited: Matt J on 31 Aug 2020
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'

Sign in to comment.

 Accepted Answer

Bruno Luong
Bruno Luong on 31 Aug 2020
Edited: Bruno Luong on 31 Aug 2020
"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.

1 Comment

Thank you, I could understand your answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 30 Aug 2020

Commented:

on 31 Aug 2020

Community Treasure Hunt

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

Start Hunting!