필터 지우기
필터 지우기

Problems with while loop

조회 수: 2 (최근 30일)
Tamia Eli
Tamia Eli 2020년 8월 30일
댓글: Tamia Eli 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.
  댓글 수: 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.
  댓글 수: 1
Tamia Eli
Tamia Eli 2020년 8월 31일
Thank you, I could understand your answer.

댓글을 달려면 로그인하십시오.

추가 답변 (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