Why do I get NAN?

조회 수: 349 (최근 30일)
Yasmin Tamimi
Yasmin Tamimi 2014년 1월 19일
댓글: Dheeraja Karthikeyan 2020년 4월 25일
Hey everyone,
I'm running this equation
hMphi(1) = - (dd_Anis(1) * ddu_acos(1) * du_dMphi(1))/(C2*sin(Mtheta(1)))- ((Is*P_hbar/(2*P_Q*g*C2))*((ephix(1)*Ffx(1))+(ephiy(1)*Ffy(1))))- Ms*(Ny-Nx)*sin(Mtheta(1))*sin(2*Mphi(1))+ (Happy*cos(Mphi(1)) - Happx*sin(Mphi(1))) ;
in a loop and a certain point the value of this vector becomes a NAN?! Can Anyone tell me why is this happening? I' dividing by a sin, so I thought that maybe the problem is from it, but I printed the values of it I didn't get Inf.
Thanks

채택된 답변

Roger Stafford
Roger Stafford 2014년 1월 19일
Any of these arithmetic operations will produce a NaN: zero/zero, zero*infinity, infinity/infinity, infinity-infinity. For example, if any of the three quantities, dd_Anis, ddu_acos, or du_dMph were to become zero at the same time that sin(Mtheta) is zero, that would produce a NaN. You should make a test on hMphi each time it is computed and arrange to stop if a NaN is detected so you can check all the values that go into your equation at that point.

추가 답변 (2개)

Jan
Jan 2014년 1월 19일
You can stop the code automatically, when the NaN appears:
dbstop if naninf
Then inspect the local variables and evaluate the different parts of the equation in the command window until you find the reason for the NaN.
  댓글 수: 1
Dheeraja Karthikeyan
Dheeraja Karthikeyan 2020년 4월 25일
Thanks a lot for this! You were a blessing in disguise!

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


Anna
Anna 2014년 1월 19일
Have you tried to isolate the part of the equation where the fault is at? Debug the equation in the loop iteration before the equation turns into a NaN.

카테고리

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