Problem with inf value
Show older comments
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 Comments
Atsushi Ueno
on 20 May 2021
Edited: Atsushi Ueno
on 20 May 2021
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
on 20 May 2021
Walter Roberson
on 20 May 2021
I recommend that you debug with
dbstop if naninf
Plamen Bonev
on 20 May 2021
Atsushi Ueno
on 20 May 2021
Edited: Atsushi Ueno
on 20 May 2021
>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
on 20 May 2021
Plamen Bonev
on 20 May 2021
Walter Roberson
on 20 May 2021
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
on 20 May 2021
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
on 20 May 2021
Plamen Bonev
on 20 May 2021
Atsushi Ueno
on 20 May 2021
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
on 20 May 2021
Plamen Bonev
on 20 May 2021
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



