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
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.
as exp(x) so the number e in MATLAB is exp(1).
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. .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With