Euler's number syntax in matlab
61 ビュー (過去 30 日間)
古いコメントを表示
How do I type euler's number in matlab for the follwing function with t being an input of vectors and xN should be the same size vector of t?
xN=C1*e^(S1*t)+C2*e^(S2*t)
e is meant to be Euler's number in the equation.
So far I got
xN=C1.*exp(S1.*t)+C2.*exp(S2.*t);
0 件のコメント
回答 (2 件)
John D'Errico
2020 年 11 月 13 日
編集済み: John D'Errico
2020 年 11 月 13 日
So what is wrong with what you wrote? In fact, that is how you do it (and how most common languages I can think of usually do it.)
Use exp, as you did in what you wrote. That was correct code.
As I show in my comment to Ameer, wanting to use a form like e^A will be slower AND less accurate. The reason for this is because MATLAB has used highly optimized codes that are specific for the special function case of exp. They are optimized for speed as well as accuracy.
>> e = exp(1);
>> A = randn(5000);
>> timeit(@() exp(A))
ans =
0.053855
>> timeit(@() e.^A)
ans =
0.19793
exp is 3x (almost 4x) faster.
>> A = randn(1,100);
>> expA = exp(A);
>> eA = e.^A;
>> norm(vpa(exp(sym(A))) - expA)
ans =
0.00000000000000095308427606366842484002999218734
>> norm(vpa(exp(sym(A))) - eA)
ans =
0.0000000000000054082099663712269073068228626587
And 5 times more accurate.
The gain in accuracy is because you first formed an approximation to exp(1) when you did e = exp(1). That value is NOT the exact number. It is a 52 binary bit approximation to the same number. In a binary scientific form, we might have written it as:
10.101101111110000101010001011000101000101011101101010
But that is just what MATLAB stores. That is not the true binary expansion, which has infinitely many bits, since e is a transcendental number. so the value e=exp(1) is rounded at the least significant bits as it is stored in MATLAB. If you then compute e.^A, you compound the errors, partly because e was not the correct number, but also because there are additional tiny errors created just in the power operation.
If you want more bits, here are 128 bits of e:
10.101101111110000101010001011000101000101011101101001010100110101010111111011100010101100010000000100111001111010011110011110001
So, when you form e = exp(1), you are starting with the wrong number in the first place.
0 件のコメント
Ameer Hamza
2020 年 11 月 13 日
編集済み: Ameer Hamza
2020 年 11 月 13 日
'e' is not defined as a constant in MATLAB. The only way is to use exp(). Or you can define 'e' yourself
e = exp(1)
xN=C1*e^(S1*t)+C2*e^(S2*t)
1 件のコメント
John D'Errico
2020 年 11 月 13 日
編集済み: James Tursa
2020 年 11 月 13 日
Note that if you do define e yourself, you will not be taking advantage of any optimizations in MATLAB for that computation. For example:
>> e = exp(1);
>> A = randn(5000);
>> timeit(@() exp(A))
ans =
0.053855
>> timeit(@() e.^A)
ans =
0.19793
So is there any reason you could imagine to want to define e as a separate variable? It runs more slowly, taking 3x as much time! Is it more accurate?
>> A = randn(1,100);
>> expA = exp(A);
>> eA = e.^A;
>> norm(vpa(exp(sym(A))) - expA)
ans =
0.00000000000000095308427606366842484002999218734
>> norm(vpa(exp(sym(A))) - eA)
ans =
0.0000000000000054082099663712269073068228626587
So compared to the higher precision result from syms and vpa, e.^A is considerably less accurate.
Just because you can do something does not mean it is always a good idea.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!