How can I get a value not NaN when calculating ''inf/inf''?

3 views (last 30 days)
I got 'NaN'(which is not 'NaN') for my for loop calculation and I found that it was because of 'inf/inf'
Here is my coding. I kinda simplified it for better readability.
Could anyone help me getting r(1), r(2), .....,r(11) for this problem?
When I solved it by hand, r(2) was 160.71
Thank you
>> syms x r
>> a=1; b=0; dx=5; t=40;K=10;
>> f(x) = a*x+b;
>> df(x)=diff(f(x));
>> r(1)=inf;
>> for i=1:10
r(i+1)=K*int((1+df(x)).^(1/2),x,i,i+dx)+(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t));
end
>> r
r=
[ Inf , NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
  11 Comments
John D'Errico
John D'Errico on 20 Oct 2018
Note that inf*90/(inf+90) is NOT 90. It is anything you want it to be, once inf is in there. Instead, you need to use limits, as I show. Yes, the limit is still 90 in the end, but once you have inf in there, you cannot solve it.
John D'Errico
John D'Errico on 20 Oct 2018
BTW, The reason why I suggest not encouraging people to use a picture, is because pictures are often unreadable. The writing is poor, or the contrast low, or the text is simply too small, etc.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 20 Oct 2018
Edited: John D'Errico on 20 Oct 2018
There is no consistent numeric value you can obtain when taking the ration inf/inf.
Should it be 1? Clearly x/x is always 1, for any non-zero NUMBER x. But inf is not really a number. And since 2*inf is also inf, then logically, we could conclude that if inf/inf was 1, then
2 = 2*1 = 2*(inf/inf) = (2*inf)/inf = inf/inf = 1
Just as easily, we could construct any number of other nonsense results, that is, if inf/inf were defined to take on any value.
The point is, once your computations have reduced to inf/inf, you are already in a deep hole, one from which you will not escape.
Instead, you need to do some thinking. See if you can avoid computing those infs in the first place.
syms x r
a=1; b=0; dx=5; t=40;K=10;
f(x) = a*x+b;
df(x)=diff(f(x));
r(1)=inf;
Now, lets see what happens when you try to start that loop, trying to create r(2).
i = 1;
K*int((1+df(x)).^(1/2),x,i,i+dx)+(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t))
ans =
NaN
That expression has a few terms in it.
K*int((1+df(x)).^(1/2),x,i,i+dx)
ans =
50*2^(1/2)
(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t))
ans =
NaN
(r(i)*(K*f(i*dx)+t))
ans =
Inf
(r(i)+(K*f(i*dx)+t))
ans =
Inf
So, you have inf/inf, which as I showed before, is an expression that has no reasonable value that can be assigned.
You may have "done it by hand" but if you did so, you made up a result that you decided you like. So, lets go more deeply into that expression for r(2).
You have what is essentially
50*2^(1/2) + inf/inf
and now, you want the inf/inf to magically be 90?
Hmm. I think you are now making up a result. As we saw before, inf/inf is anything you wish it to be. Personally, I think 17 or 42 are better choices for results there. But what have you done? And why do you think the result of
(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t))
ans =
NaN
should be 90?
(K*f(i*dx)+t)
ans =
90
Hmm, so now you have
(inf*90)/(inf+90)
And now you think that
inf + 90 = inf
so 90*inf/inf should be 90, since inf/inf should be 1.
SHEESH!
It seems that you are playing fast and loose with infs here.
Lets see what happens if r(1) is just some large number.
r(1) = 1/eps;
for i=1:10
r(i+1)=K*int((1+df(x)).^(1/2),x,i,i+dx)+(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t));
end
double(r)
ans =
Columns 1 through 7
4.5035996273705e+15 160.710678118653 145.531748914597 153.120263450657 164.190629789399 175.546135998982 186.482443382689
Columns 8 through 11
196.869165257471 206.723594803462 216.097689853589 225.046227726715
Thus, by making r(1) a finite value, while still large in context of double precision arithmetic, it is not infinite. Now we get a value that is at least somewhat more reasonable.
So, can we do this using mathematics? A limit seems like what you really wanted to do.
syms u real
r(1) = u;
for i=1:10
r(i+1)=K*int((1+df(x)).^(1/2),x,i,i+dx)+(r(i)*(K*f(i*dx)+t))./(r(i)+(K*f(i*dx)+t));
end
r = limit(r,u,inf);
r(1)
ans =
Inf
r(2)
ans =
50*2^(1/2) + 90
r(3)
ans =
50*2^(1/2) + (7000*2^(1/2) + 12600)/(50*2^(1/2) + 230)
double(r(11))
ans =
225.046227726715
I imagine that is what you wanted to see.
  1 Comment
Chanhp Jeong
Chanhp Jeong on 20 Oct 2018
Thanks you for the detailed explanation.
I learnt a lot from your comments. Seems i should reestablish the concept of inf.
And also 'eps' you shown here will be quite useful for my future matlab works.
Thank you

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!