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!