e^x maclaurin serie

51 vues (au cours des 30 derniers jours)
Firuze
Firuze le 15 Jan 2024
Modifié(e) : owen le 16 Sep 2024
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 commentaires
Sam Chak
Sam Chak le 15 Jan 2024
Hi @Firuze,
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
T = 
%% Evaluate the series
Teval = double(subs(T, x, 1))
Teval = 2.7167
Firuze
Firuze le 15 Jan 2024
thank you

Connectez-vous pour commenter.

Réponse acceptée

Hassaan
Hassaan le 15 Jan 2024
Modifié(e) : Hassaan le 15 Jan 2024
Numerically:
% Prompt the user to enter the value of x
x = input('Enter the value of x: ');
% Prompt the user to enter the number of terms N
N = input('Enter the number of terms N: ');
% Initialize the result of e^x
ex = 1; % The first term of the series is always 1
% Calculate e^x using the series expansion [for-loop]
for n = 1:N
term = x^n / factorial(n); % Calculate each term in the series
ex = ex + term; % Add each term to the result
end
% % Calculate e^x using the series expansion [vectorization]
% n = 0:N;
% ex = sum(x.^n ./ factorial(n));
% Display the result
fprintf('e^x calculated using the series expansion is: %f\n', ex);
% For comparison, calculate e^x using MATLAB's exp function
ex_true = exp(x);
fprintf('e^x calculated using MATLAB''s exp function is: %f\n', ex_true);
It will prompt you to input the value of x and the number of terms N. Then, it will calculate and display the result of e^x using the series expansion up to N terms. Additionally, it shows the result calculated by MATLAB's built-in exp function for comparison.
Output
Enter the value of x: 1
Enter the number of terms N: 5
e^x calculated using the series expansion is: 2.716667
e^x calculated using MATLAB's exp function is: 2.718282
Symbol Evaluation
% Ask for input from the keyboard
x = input('Enter the value of x: ');
N = input('Enter the number of terms N: ');
% Initialize the symbolic variable and the result
syms n;
ex = sym(0); % Symbolic 0
% Calculate the series expansion symbolically
for n = 0:N-1
ex = ex + (x^n)/factorial(n);
end
% Display the symbolic result
disp(ex);
Symbolic Expression
% Define the symbolic variable x and the summation index n
syms x n;
% Ask for input for the number of terms in the series N
N = input('Enter the number of terms N: ');
% Define the symbolic expression for the series expansion of e^x
e_x_series = symsum(x^n/factorial(n), n, 0, N-1);
disp(e_x_series)
% Pretty print the symbolic series expansion
% pretty(e_x_series)
Output
Enter the number of terms N: 5
x^4/24 + x^3/6 + x^2/2 + x + 1
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  4 commentaires
John D'Errico
John D'Errico le 15 Jan 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.
Hassaan
Hassaan le 15 Jan 2024
Modifié(e) : Hassaan le 15 Jan 2024
@John D'Errico I do agree and I try to avoid as much as possible such scenarios.

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 15 Jan 2024
Modifié(e) : Sulaymon Eshkabilov le 15 Jan 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 commentaires
John D'Errico
John D'Errico le 15 Jan 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.
owen
owen le 16 Sep 2024
Modifié(e) : owen le 16 Sep 2024
I am trying to do the same series but not with e^x, i tried looking into to code but i am a bit confused where i put the equation and how i change the e^x into the function i want. Could someone please help me with that?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by