Razoredge
Razoredge

Reputation: 3

Matlab exp produces unexpected result

As a Matlab-noob i've been experimenting a bit with the exp-function. What I found was that putting:

a = 1.1
b = 5
test = exp (a+b)

produced the desired (correct) answer. (test = 445.8578)

However: replacing a with a matrix gave completely the wrong result. so

a = [1.1 1.3 2.5 4.3]
b = 5
test = exp(a+b)

produced a wrong result (test = 1.0e+04 *

0.0446 0.0545 0.1808 1.0938)

I'm probably overlooking something, but I don't know where I'm going wrong. Can anyone enlighten me?

Upvotes: 0

Views: 81

Answers (2)

hbaderts
hbaderts

Reputation: 14371

It produces the correct result:

a = [1.1 1.3 2.5 4.3];
b = 5;
c = a + b

c = 
  6.1    6.3    7.5    9.3

Now lets calculate the exponential function of each of those values (rounded a bit):

exp(6.1) =    445.86
exp(6.3) =    544.6
exp(7.5) =  1'808.0
exp(9.3) = 10'938

You get the same results by

exp(c)

ans = 
    1.0e+04 *
     0.0446    0.0545    0.1808    1.0938

which means you have to multiply each printed element with 1.0e+04 = 10'000, which is the correct result.

This is the standard output format of MATLAB and it allows to quickly see how large the different values are. There are several different formats (see the documentation for more detailed information). Interesting might be shortG:

format shortG
exp(c)
ans =
    445.86       544.57         1808        10938

Which is exactly what you expected. As you see: It's just the output format.

Upvotes: 2

Partha Lal
Partha Lal

Reputation: 541

It's giving the right answer, it's just presenting it in exponential format: 1.0e+04 * 0.0446 is the same as 445.8578.

You could try evaluating test(1) to verify that.

Upvotes: 1

Related Questions