e^x maclaurin serie
Mostra commenti meno recenti
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 Commenti
Dyuman Joshi
il 15 Gen 2024
Modificato: Dyuman Joshi
il 15 Gen 2024
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
il 15 Gen 2024
Risposta accettata
Più risposte (1)
Sulaymon Eshkabilov
il 15 Gen 2024
Modificato: Sulaymon Eshkabilov
il 15 Gen 2024
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 Commenti
John D'Errico
il 15 Gen 2024
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.
Categorie
Scopri di più su Mathematics in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!