Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave/Matlab: Difference between e^(-1*z) and exp(-1*z)

Tags:

matlab

octave

I am new with Octave and I have a problem.I thought the following codes were the same, but they produces different results. What is the difference? Thanks

Octave/Matlab: Difference between e^(-1*z) and exp(-1*z)

g = 1./(1 + e^(-1*z));

g = 1./(1 + exp(-1*z));

Where z is a vector, element or matrix

like image 623
vicase98 Avatar asked Mar 04 '18 19:03

vicase98


People also ask

What is exp () in octave?

In Octave. exp(1) equals e where e is Euler's number. There are 4 operations/functions that are to be noted here: e^x is same as expm(x) and e.

Is exp in MATLAB e?

as exp(x) so the number e in MATLAB is exp(1).

What is exp in MATLAB?

The exp function is an elementary function that operates element-wise on arrays. Its domain includes complex numbers. Y = exp(X) returns the exponential for each element of X . For complex , it returns the complex exponential. .


2 Answers

In Octave

exp(1) equals e where e is Euler's number.

There are 4 operations/functions that are to be noted here:

e^x is same as expm(x) and e.^(x) is same as exp(x).

  • e^x and expm(m) represent e raise to the matrix x.
  • e.^(x) and exp(x) represent exponential ex for each element in matrix x.

If x is a scalar then all (e^x, expm(x), e.^x and exp(x)) are mathematically equal.
For your case, z is a matrix and hence you get different results.


In MATLAB,

e is not defined in MATLAB. exp(x) and expm(x) have same definitions in MATLAB as those that are described for Octave above.


PS: e or E are also used for E-notation in both MATLAB and Octave but that's a different thing.

like image 142
Sardar Usama Avatar answered Oct 19 '22 07:10

Sardar Usama


In Octave, it is important to note that e^x and exp(x), where x is a double precision scalar variable, are not necessarily the same.

For instance:

>> a = e ^ 2
a =  7.3891

>> b = exp (2)
b =  7.3891

>> b - a
ans = 8.8818e-16

The reason is that exp (2) uses a dedicated algorithm to compute the exponential function, while e ^ 2 actually calls the function e () to get the value of e, and then squares it:

>> c = realpow (e (), 2)
c =  7.3891

>> c - a
ans = 0

Another reason why e ^ x and exp (x) differ is that they compute completely different things when x is a square matrix, but this has already been discussed in Sardar's answer.

like image 26
Julien Bect Avatar answered Oct 19 '22 07:10

Julien Bect