How to prevent an exponential function to return infinity

Hello everybody
I currently try to simulate the performance of a portfolio and have - at some points - to deal with very high numbers. This works fine as long as I dont feed an exponential function since the return value is then Inf.
e.g. exp(x) where x = 2.9678e+03.
Can I somehow chance the configuration of the exp function such that it returns real numbers even if I feed the function with high values?
Best, Clemens

댓글 수: 2

M=expm(vpa(2.9678e3))
you do not need expm if you are passing in a symbolic scalar; just use exp()

댓글을 달려면 로그인하십시오.

 채택된 답변

John D'Errico
John D'Errico 2014년 8월 2일

2 개 추천

And, what exactly do you want it to return, when you pass it an argument that is larger than the maximum value it can handle?
A double can store nothing larger than realmax. Any larger value is inf as far as that format can handle.
My suggestion is that you see where you can use logs in your computations. Most of the time large intermediate values only that - intermediates. Logs allow these big numbers to be dealt with properly. It really is very rare that properly written computations need exceed the limit of a double.
As a VERY last resort, you might consider use of a tool that will allow a larger dynamic range than a double. Such a tool is my own HPF (found on the file exchange), or the symbolic toolbox. These tools will be dramatically slower than use of a double, and they have their own limits too. It really is a rare case where you cannot do better by simply learning the techniques one needs to deal with these problems in the range afforded by a double. Your code will be the better for it, and your problems will be less in the future as your own numerical coding skills improve.

댓글 수: 4

Clemens
Clemens 2014년 8월 2일
편집: Clemens 2014년 8월 2일
thank you for your suggestions. it is indeed only an intermediate value. but how would you use log effectively?
say, I have something like
  • a = some real number
  • z = @(x) exp(2.9678e+03) - a + x
  • y = fsolve(z,x0);
how could i rewrite this by using log?
I would solve it using basic high (grade?) school algebra, so unless you have a better, more realistic example...
However, even in that case, scaling can still solve the problem. Divide z by some large number. What number, you ask?
round(log10(exp(2.9678e+03)))
This will yield an integer. I think that again, basic algebra will let you compute that log without needing more than a double too.
Scale the problem by using that log scale factor.
Solve for the scaled x using a double, then compute the log of the solution, by adding back in the scale factor.
Surely you don't think that a double can handle the precision required anyway, unless a is very near in magnitude to that constant?
Again, I claim that simple good computational & numerical skills will let you solve the problem you posed.
ok thanks for your help and patience. I now understood the concept and will give it a try.
Hadn't thought of logs thankyou (problem was in one equation getting a rounded inf x 0 returning nan when should have been reasonable in the end)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sigitas Babraitis
Sigitas Babraitis 2021년 1월 5일

0 개 추천

exp^inf

댓글 수: 1

not valid in MATLAB as exp must be passed an argument.
It is also not obvious how this would solve the problem?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2014년 8월 2일

댓글:

2021년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by