Problem with inf value
31 views (last 30 days)
Show older comments
Plamen Bonev
on 20 May 2021
Commented: Plamen Bonev
on 20 May 2021
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
Accepted Answer
Walter Roberson
on 20 May 2021
condGaz(i,j,k)=cond0(i,k)+(condPseudoCr(k)*psi(i,j,k)*(rhor(i,j,k)));
That sets condGaz(I,j,:) according to the largest k value that has been seen so far. For example at k = 1, condGaz(i,j,1) will be set, and condGaz(i,j,2) will not have been set yet.
for g=1:numel(NbGaz)
disp(['i,j,k,g = ' num2str([i j k g]) ' ; condGaz (i,j,g) = ' num2str(condGaz(i,j,g))])
but numel(NbGaz) is greater than k = 1, so in this inner loop within for k, as soon as g is greater than the current k, you index condGaz(i,j,:) elements that are still 0, and that gets you a division by 0.
Perhaps that loop needs to be postponed until after the other arrays have been fully built.
3 Comments
Walter Roberson
on 20 May 2021
There are multiple ways to proceed. One of them is
for g=1:min(k, numel(NbGaz))
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));
Phigk(i,j,g,k) = condGaz(i,j,g) ./ condGaz(i,j,k); %symmetry
end
Another way is to remove the for g loop entirely, and then after the entire for k loop, do
Phigk = condGaz ./ permute(condGaz, [1 2 4 3]);
with no nested loops needed (but does need R2016b or later for this particular version of the code.)
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!