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!