Problem with inf value
이전 댓글 표시
I am having inf values, while I am supposed to be having 1.0396e+01 value... (which is apparently not at all an infinite value)...
I have 4 loops in my code : and I am trying to get the ratios condGaz(i,j,k) / condGaz(i,j,g), where k=1:3 ; g=1:3.
Here is a part of my code :
----------- Before this, I have 3 loops i (for Temperature), j (for Pressure), k=1:3 and this loop is my final loop ------------
for g=1:3
if k~=g
Phigk(i,j,k,g) = condGaz(i,j,k) / condGaz (i,j,g);
else
Phigk(i,j,k,g) =0 ;
end
end
end
end
end
Here are the condGaz(i,j,k) values.

And here are the values that I obtain for my ratio Phigk (i,j,k,g) = condGaz(i,j,k) / condGaz (i,j,g);


I get the impression that for g=1, I get all values right...
As soon as it gets to g=2 I get 1 value right and the other is INF
And for g=3, I get all the values INF....
I am not supposed to be having INF values because I manually did the ratio of for example Phigk(1,1,2,3) = condGaz(1,1,2) / condGaz(1,1,3) = 1.23e+00. (which is INF value if you see the results with the loops for Phigk(:,:,2,3)...
I tried format shortE and stuff... And it did not work... So I am confused about the sense of this INF value.... It is supposed to give INF value when the result is too big... But here we are talking values of 1.23e+00.... Can you please help me?
댓글 수: 14
Atsushi Ueno
2021년 5월 20일
편집: Atsushi Ueno
2021년 5월 20일
Possible story is that, 1. the indexing is not working as expected, 2. the denominator becomes equal to zero, and 3. the result of the division by zero becomes inf. Division by zero does not become the error. You will need to output the denominator just before the division and debug your code.
Plamen Bonev
2021년 5월 20일
Walter Roberson
2021년 5월 20일
I recommend that you debug with
dbstop if naninf
Plamen Bonev
2021년 5월 20일
Atsushi Ueno
2021년 5월 20일
편집: Atsushi Ueno
2021년 5월 20일
>Actually the indexing does work because It works wel, and it gives the right results (I checked them manually).
Why is that a reason for the indexing to be correct?
MATLAB does produce the same calculation results whether on the command prompt or on the script.
Why don't you try debugging like below?
if k~=g
disp(['i,j,k,g = ' num2str([i j k g]) 'condGaz (i,j,g) = ' num2str(condGaz(i,j,g))]);
Phigk(i,j,k,g) = condGaz(i,j,k) / condGaz (i,j,g);
else
Plamen Bonev
2021년 5월 20일
Plamen Bonev
2021년 5월 20일
Walter Roberson
2021년 5월 20일
This does not help because it stops my code and so nothing happens...
It stops your code allowing you to find the very first step the problem occurs at, and you can then examine the values of the variables.
You do not show us your outer loops. We can predict that something in your loops is setting the values to 0, either explicitly (indexing) or else that you initialize the variable to zeros inside a loop when you should have initialized it before the loop. For example,
for K = 1 : 2
G = zeros(5,7,3);
G(:,:,K+1) = rand(5,7);
end
compared to
G = zeros(5,7,3);
for K = 1 : 2
G(:,:,K+1) = rand(5,7);
end
Atsushi Ueno
2021년 5월 20일
Thank you. So the indexing is not the cause, but rounding is. You must know the reason why rounding happens, but I do not. Anyway, the cause of the Inf problem has been solved.
Plamen Bonev
2021년 5월 20일
Plamen Bonev
2021년 5월 20일
Atsushi Ueno
2021년 5월 20일
I thought you know the reason why denominator becomes zero because you declare that it is rounding problem. However, you explained that rounding is happening because the debug output shows 0 when the value should be 2.4946e-02. I think that's probably your assumption. There is no specification like "floating point value less than e-02 order is rounded to be 0".
Why don't you consider more about @Walter Roberson's comment? Indexing issue is not only for reading value but also for storing value.
Plamen Bonev
2021년 5월 20일
Plamen Bonev
2021년 5월 20일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




