7

I am a beginner in MATLAB, and I need to represent e(-t2).

I know that, for example, to represent ex I use exp(x), and I have tried the following

1) tp=t^2; / tp=t*t; x=exp(-tp);

2) x=exp(-t^2);

3) x=exp(-(t*t));

4) x=exp(-t)*exp(-t);

What is the correct way to do it?

nbro
  • 15,395
  • 32
  • 113
  • 196
user573382
  • 343
  • 3
  • 10
  • 22
  • 4
    no. 4 (`x=exp(-t)*exp(-t);`) is mathematically wrong. – SIMEL Mar 06 '11 at 12:56
  • 1
    exp(-t)*exp(-t) is NOT equivalent to exp(-t^2), it is equivalent to exp(-2*t), a rather different number. –  Mar 06 '11 at 16:19

2 Answers2

14

If t is a matrix, you need to use the element-wise multiplication or exponentiation. Note the dot.

x = exp( -t.^2 )

or

x = exp( -t.*t )
Tim
  • 13,904
  • 10
  • 69
  • 101
5

All the 3 first ways are identical. You have make sure that if t is a matrix you add . before using multiplication or the power.

for matrix:

t= [1 2 3;2 3 4;3 4 5];
tp=t.*t;
x=exp(-(t.^2));
y=exp(-(t.*t));
z=exp(-(tp));

gives the results:

x =

0.3679    0.0183    0.0001
0.0183    0.0001    0.0000
0.0001    0.0000    0.0000

y =

0.3679    0.0183    0.0001
0.0183    0.0001    0.0000
0.0001    0.0000    0.0000

z=

0.3679    0.0183    0.0001
0.0183    0.0001    0.0000
0.0001    0.0000    0.0000

And using a scalar:

p=3;
pp=p^2;
x=exp(-(p^2));
y=exp(-(p*p));
z=exp(-pp);

gives the results:

x =

1.2341e-004

y =

1.2341e-004

z =

1.2341e-004
SIMEL
  • 8,745
  • 28
  • 84
  • 130