I keep getting an error when I try coding this:

24 Ansichten (letzte 30 Tage)
Salma
Salma am 9 Sep. 2024
Beantwortet: nick am 9 Sep. 2024
• 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 Kommentare
VBBV
VBBV am 9 Sep. 2024
Bearbeitet: VBBV am 9 Sep. 2024
Please show what error you get. All text in red. If possible share your code
Jatin
Jatin am 9 Sep. 2024
Various errors can occur, so please include the specific error you're encountering to make your question more concise.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

nick
nick am 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'}));
N Approximation Error __ _____________ __________ 1 0.7854 0.078291 2 0.70465 0.0024541 3 0.70714 3.6265e-05 4 0.70711 3.1161e-07 5 0.70711 1.7503e-09 6 0.70711 6.928e-12 7 0.70711 2.0428e-14 8 0.70711 0 9 0.70711 0 10 0.70711 0 11 0.70711 0 12 0.70711 0 13 0.70711 0 14 0.70711 0 15 0.70711 0 16 0.70711 0 17 0.70711 0 18 0.70711 0 19 0.70711 0 20 0.70711 0
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.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by