On 10/24/2016 8:30 PM, Aurelien PELISSIER wrote:
> Hi,
>
> My problem is very simple :
> if you define this function
>
> fun = @(t)exp(t.^2)/(exp(t.^2));
> it is supposed to give 1 everywhere, but if I do for example
> fun(40), it's NaN, simply because the right term reach
>more than 10^300 and matlab stop counting above this.
>
> So my question is, how do I make matlab define the right answer for any real numbers ?
>
>
You can't have your cake and eat it too. You have to decide
if you want to do numerical or symbolic.
syms x
fun = @(t) limit(exp(x^2)/(exp(x^2)),x,t)
fun(40)
1
In Matlab, if you do x=10^330, then x=Inf and
Inf/Inf is NaN. You can not real change this behavior.
If you want to do numerical, then you have to add extra
code your self to check for overflow and underflow in
the function itself. I do not know if Matlab has
buildin function which detects overflow in floating points
IEEE operations and raise warning or such. So that in
the example above, you'll get warning or error, which you can
trap.
Why not instead test your function and the smallest and
largest possible values of input, and make sure it works
ok for these?
Nasser
