e^x maclaurin serie
显示 更早的评论
Hello everyone, I'm very new to MATLAB. Could you help me with this question please?
How to write codes that calculate e^x by serializing the x value entered from the keyboard into a series equal to the number of terms (N) entered from the keyboard?
4 个评论
Dyuman Joshi
2024-1-15
编辑:Dyuman Joshi
2024-1-15
To clarify, you want to find the Maclauren series of exponential function upto N terms? If yes, do you want to do that numerically or symbolically?
I realized that I can use the taylor() function to compute the Maclaurin series without having to memorize the series expansion. There are several solutions, depending on whether certain built-in functions can be used or not in your homework.
%% Get input values from the Keyboard User
% x = input('Enter the value of x: ');
% N = input('Enter the number of terms (N): ');
x = 1;
N = 5;
sympref('PolynomialDisplayStyle', 'ascend');
syms x
%% Display the series expansion for e^x
T = taylor(exp(x), x, 'Order', N+1) % Unsure how you define the number of terms
%% Evaluate the series
Teval = double(subs(T, x, 1))
Firuze
2024-1-15
采纳的回答
更多回答(1 个)
Sulaymon Eshkabilov
2024-1-15
编辑:Sulaymon Eshkabilov
2024-1-15
A function can be written to compute Mclaurin series approximation, e.g.:
% E.g.: approximation of exp(x) at x = pi
x = pi;
N_terms = 10; % Number of terms in the series
Solution = Maclaurin(x, N_terms);
fprintf('Maclaurin series approx. for exp(%g) with %d terms: %g\n', x, N_terms, Solution);
function function SOL = Maclaurin(x, terms)
n = 0:terms;
SOL = sum((x.^n) ./ factorial(n));
end
Alt. Solution (computationally slow)
% E.g.: approximation of exp(x) at x = pi
x = pi;
N_terms = 10; % Number of terms in the series
Solution = Maclaurin2(x, N_terms);
fprintf('Maclaurin series approx. for exp(%g) with %d terms: %g\n', x, N_terms, Solution);
function SOL = Maclaurin2(x, terms)
SOL = 1; % Initialize with the first term
for n = 1:terms
SOL = SOL + x^n / factorial(n);
end
end
2 个评论
John D'Errico
2024-1-15
Please do not do obvious homework assignments for students who show no effort. This does not help the student. It teaches them only that there is always some sucker willing to do their homework for them.
Worse, it teaches that same student to then keep on posting additional homework assignments, thinking they have landed on a homework gold mine.
And finally, it teaches other students to follow their lead. This damages the forum itslef, as we will be overwhelmed with students posting their homework.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!