I keep getting an error when I try coding this:
4 views (last 30 days)
Show older comments
• Evaluates the series expansion of sin(x) for any value of x for any N number of terms in the expansion
• Calculates the error with respect to the exact value of sin(x)
• Plots the error VS number of terms, N.
2 Comments
Jatin
on 9 Sep 2024
Various errors can occur, so please include the specific error you're encountering to make your question more concise.
Answers (1)
nick
on 9 Sep 2024
Hi Salma,
I understand that you are trying to evaluate the sin(x) series expansion and plot the error wrt the number of terms in expansion.
To evaluate the series expansion of sin(x) and calculate the error with respect to the exact value of sin(x), we can use the Taylor series expansion for sin(x) around 0:
% Define the value of x and the maximum number of terms N
x = pi/4; % For example, pi/4
maxN = 20; % Maximum number of terms to evaluate
errors = zeros(1, maxN);
approximations = zeros(1, maxN);
exactValue = sin(x);
% Calculate series expansion and error for each N
for N = 1:maxN
seriesApprox = 0;
% Calculate the series approximation for the current N
for n = 0:(N-1)
term = ((-1)^n * x^(2*n + 1)) / factorial(2*n + 1);
seriesApprox = seriesApprox + term;
end
approximations(N) = seriesApprox;
errors(N) = abs(exactValue - seriesApprox);
end
% Plot the error vs number of terms
figure;
plot(1:maxN, errors, 'o-');
xlabel('Number of terms, N');
ylabel('Error');
title(['Error in series expansion of sin(x) for x = ', num2str(x)]);
grid on;
disp(table((1:maxN)', approximations', errors', 'VariableNames', {'N', 'Approximation', 'Error'}));
The script calculates the Taylor series approximation of sin(x) for a specified number of terms. It computes the error as the absolute difference between the exact value of sin(x) and the series approximation.
You may refer to the following documentation to learn more about Maclaurin series expansion :
Hope this helps.
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!