How to plot sum of series?

4 visualizaciones (últimos 30 días)
Teb Keb
Teb Keb el 27 de Nov. de 2020
Comentada: Walter Roberson el 8 de Mzo. de 2024
I am trying to plot the function above on interval [-2,2]. I was wondering if the code below is a correct way to do it.
syms n x
f(x)=symsum((((((-1)^(n+1))/(5*n)))*cos((n-1)*x)),n,1,Inf);
figure
fplot(x,[-2 2])
Also, how do I plot the partial sums F1,F2 and F10 on the interval [-2,2] and have them all on the same plot figure.

Respuestas (1)

Shubham Khatri
Shubham Khatri el 3 de Dic. de 2020
Hello Teb,
Please take a look at the following code. You can change the value of n.
x=-2:.1:2
plot(x,expr(x))
function [value]=rbs(n,x)
value=(((((-1).^(n+1))/(5.*n))).*cos((n-1).*x));
end
function [ret]=expr(x)
ret=0;
for n=1:10
ret=ret+rbs(n,x)
end
end
Hope it helps
  2 comentarios
Stefan
Stefan el 8 de Mzo. de 2024
Editada: Walter Roberson el 8 de Mzo. de 2024
This is awesome, very nifty little piece of code, but powerful. Thank you so much
It took me a ton of research to come down to this and make it fit my project
I am working on my university assigment and this is what i got
% prep the workspace
clc
all clear
all clos
% variables
x=0:.1:2;
y=expr(x);
% ploting results
plot(x,expr(x),'-o','MarkerIndices',1:2:length(y))
hold on
plot(x,log(x))
% bells and whistles
grid on
legend('Approximation (Taylor series)', 'ln(1)', 'Location', 'northwest')
title('Taylor Series Approximation of ln(1)')
xlabel('x')
ylabel('y')
% plugging in pre-calculated function
function [value]=rbs(n,x)
value=((-1).^(n+1)*(x-1).^n./n);
end
% summation of 5 degree of approximation
function [ret]=expr(x)
ret=0;
for n=1:5
ret=ret+rbs(n,x);
end
end
% this is the end
Walter Roberson
Walter Roberson el 8 de Mzo. de 2024
Looks like it works, even if it is not as general as we might hope.
syms x
taylor(log(x), x, 1)
ans = 
expr(x)
ans = 
function [value]=rbs(n,x)
value=((-1).^(n+1)*(x-1).^n./n);
end
% summation of 5 degree of approximation
function [ret]=expr(x)
ret=0;
for n=1:5
ret=ret+rbs(n,x);
end
end

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by