Taylor expansion calculation of exp(x^2)

6 次查看(过去 30 天)
Dear Matlab users and experts,
I am aware that the exponential function is standarized as "exp" in Matlab . However, I need to calculate the function value exp(x^2) adjusting the (N) terms in the power series. Can anyone recommend the correct method to compute the function exp(x^2)?
My approach:
x = -3.0:0.1:3.0;
N = 12;
Taylor_p2 = 0;
for n = 0:N
Taylor_p2 = Taylor_p2 + (x.^(2.0.*n))./(factorial(n)); % Taylor_p2 = exp(x^2)
end
isn't giving me the desired value. I am using R2020b Matlab version.
Many thanks in advance.
Bhattarai

采纳的回答

Ameer Hamza
Ameer Hamza 2020-12-30
编辑:Ameer Hamza 2020-12-30
The formula for taylor series is correct. Just increase the number of terms.
x = -3.0:0.1:3.0;
N = 12;
Taylor_p2 = 0;
for n = 0:N
Taylor_p2 = Taylor_p2 + (x.^(2.0.*n))./(factorial(n)); % Taylor_p2 = exp(x^2)
end
p2 = exp(x.^2);
err = norm(p2-Taylor_p2);
plot(x, p2);
hold on
plot(x, Taylor_p2, '*')
Result
>> err
err =
9.1544e-05
  3 个评论
John D'Errico
John D'Errico 2020-12-31
This folllowup question about other powers of x in the exponential is completely different from your original one, because the function is different. What happen when x is negative, and you try raising that number to non-integer powers? Should you not expect a complex number as a result? The warning message you got was from plot, when you told it to plot complex numbers. The bug is in your code, and how you handle complex results.
Finally, I would point out that this function is expected to be poorly convergent for values of x where abs(x) grows greater than 1.
aroj bhattarai
aroj bhattarai 2021-1-1
Dear John D'Errico,
Should you not expect a complex number as a result? The bug is in your code, and how you handle complex results.
Yes, you are right, complex number results are expected which leads to slower convergent for abs(x)>1. For such situation, is there any mathematical solution to get a proper result? Or the real number exponent in the exponential function is fundamentally non-sense and unstable? My apologies for directing the original question into a mathematical discussion rather than the Matlab problem.
Bhattarai

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by