multiplication of infinity by zero in Matlab Calculation

12 ビュー (過去 30 日間)
Jamal Ahmad
Jamal Ahmad 2014 年 2 月 12 日
回答済み: Logan Capizzi 2021 年 11 月 30 日
Hi,
I have a problem with the evaluation of an equation. My problem is that; one of the parts in the equation will result to infinity, and another part will result to zero. Then the product of them should give me zero BUT it gave me NAN. How I can solve this problem?
Thank you
  3 件のコメント
Jamal Ahmad
Jamal Ahmad 2014 年 2 月 23 日
Yes I have.
Roger Stafford
Roger Stafford 2017 年 3 月 18 日
@Jamal Ahmad. It should be noted that matlab does not decide this answer of a NaN. It is hard-wired into all computers which use the IEEE 754 floating point standard, which almost surely means your computer.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2014 年 2 月 12 日
編集済み: the cyclist 2014 年 2 月 12 日
The product of 0 and infinity, mathematically, is not zero. It is indeterminate. That is why it gives you a NaN.
  10 件のコメント
Jamal Ahmad
Jamal Ahmad 2014 年 2 月 23 日
How?
Iain
Iain 2014 年 2 月 24 日
You'll need to work in log-space.
ln(exp(634)) = 634
ln(exp(-632.2)) = -632.2
634 + (-632.2) = 1.8
exp(1.8) = exp(634)*exp(-632.2)
I doubt that there is an automatic way to work in logarithms rather than normal numbers.

サインインしてコメントする。

その他の回答 (6 件)

Patrik Ek
Patrik Ek 2014 年 2 月 12 日
The solution to this problem comes from the mathematical limit. 1/0 = inf is really a bit sloppy. If you remember how this was introduced in the mathematical analysis course you most likely took for many years ago you would see that the correct way to work with infinities is to work with limits for example,
lim x->0 a/x = inf, a<inf equ(1)
This is however in most cases seen as a general theorem, which allows you to write 1/0 = inf. Another way to express a<inf is to say that a is bounded. Otherwise it is unbounded, it can take any value larger than the largest real value. The same applies for,
lim x->inf b/x = 0; b<inf, equ(2)
since x->inf can be anything is could be larger than 0.00... so this still applies. But what happens if we have an expression?
lim x->0 ax*1/bx = a/b*x/x = a/b, equ(3)
You see that x cancels out and the answer is a/b. So the limit of two undefined values a*inf and 1/(b*inf) actually depends on the speed with which they go towards their limit.
The problem is that when matlab becomes inf or zero, matlab can not say how fast they apporach the limit. The obvious solution to that problem is to say that the limit can not be set, or the limit does not exist. Matlab then returns a nan for the cases
inf/inf
0/0
0*inf
where equ(3) applies.

Walter Roberson
Walter Roberson 2014 年 2 月 24 日
To make Matlab not consider exp(1000) to be infinity, overload the exp function for double datatype, and tell the function to return 42 instead of infinity when it detects that the absolute value of the argument exceeds realmax()
Good luck keeping from $%#@$'ing up other MATLAB code that expects infinity. Overloading a fundamental mathematical operation to make it lie is sure to be ... an interesting experience.

David Young
David Young 2014 年 2 月 23 日
So x contains infinities and y contains zeros and we are willing to assume from knowledge of the earlier computation that when an infinity in x is multiplied by a zero in y, the correct answer is zero. Then it is reasonble to write:
z = x .* y;
z(isinf(x) & y == 0) = 0;
This replaces the NaNs that have been generated in this way by zeros.
  2 件のコメント
Walter Roberson
Walter Roberson 2014 年 2 月 23 日
Or more efficient,
z(y == 0) = 0;
If the inf might appear on either side,
z(~(x | y)) = 0;
David Young
David Young 2014 年 2 月 24 日
Yes, indeed.

サインインしてコメントする。


Logan Capizzi
Logan Capizzi 2021 年 11 月 30 日
If you want the NaN to be zero, you can do the following.
A(isnan(A)) = 0;

Sri Vastava
Sri Vastava 2017 年 2 月 25 日
編集済み: Walter Roberson 2017 年 2 月 26 日
the cyclist wrote: The product of 0 and infinity, mathematically, is not zero. It is indeterminate. That is why it gives you a NaN.
The answer is infinity.
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 2 月 26 日
Sri Vastava: are you indicating that the product of 0 and infinity is infinity? We explore above why the answer is indeterminate, not infinity.
David Goodmanson
David Goodmanson 2017 年 2 月 26 日
編集済み: David Goodmanson 2017 年 2 月 26 日
Hello Sri, It really is indeterminate.
as x -> 0,
x -> 0 (of course)
x^2 -> 0
1/x is unbounded, -> inf
1/x^2 is unbounded, -> inf
now take three different expressions that are basically 0*inf:
as x-> 0
x^2 * (1/x) = x -> 0
x * (1/x^2) = 1/x is unbounded, -> inf
x * (6/x) = 6 -> 6
You can get any value that you want, so Matlab goes with the IEEE 754 standard and says NaN.

サインインしてコメントする。


Andrea Barletta
Andrea Barletta 2017 年 3 月 16 日
I don't fully agree with previous comments on the meaning of the product between 0 and infinity. It is true that the result of 0*Inf is indeterminate when the latter is interpreted as the product between two limits - it simply doesn't make any sense if it is interpreted as a standalone expression. But the limit of 0*f(x) will always give 0, no matter where x and f(x) are going. In such case, in Matlab, we should get a genuine 0*Inf=0. Neglecting this exception may cause some issues in programming. Suppose that I have a function f defined as a function g truncated over a compact set A. Mathematically, the value f(x) should be zero for every x outside A, no matter how g is defined. But as Jamal Ahmad more or less remarked in one comment if we take
f=@(x) exp(x).*(abs(x)<=10);
and we evaluate x=1e3 we incorrectly get f(1e3)=NaN. Of course, IF using anonymous functions is not a necessity, one may overcome the problem by defining f as
function y=f(x)
if abs(x)<10
y=exp(x);
else
y=0;
end
end
But apparently truncation and anonymous functions don't like each other in Matlab...
  9 件のコメント
Andrea Barletta
Andrea Barletta 2017 年 7 月 21 日
I don't see why it should be difficult to check, every time that an anonymous function is evaluated, if the defining expression is of the form (indicator function of A)*(composition, product or sum between functions belonging to a preset list of smooth and well behaved built-in functions). In this case every time that the function is evaluated outside A Matlab can safely return 0. You could say that it wouldn't be worth investing any effort on that, given that one can always use an if-else statement, but that's completely another story. Anyway, a "truncation operator" to be used within anonymous functions would be very much useful.
Walter Roberson
Walter Roberson 2017 年 7 月 22 日
What might make sense could be to define false * inf as 0 . Not 0 in general, but logical(0) specifically.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by