How to plot sum of series?

4 Ansichten (letzte 30 Tage)
Teb Keb
Teb Keb am 27 Nov. 2020
Kommentiert: Walter Roberson am 8 Mär. 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.

Antworten (1)

Shubham Khatri
Shubham Khatri am 3 Dez. 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 Kommentare
Stefan
Stefan am 8 Mär. 2024
Bearbeitet: Walter Roberson am 8 Mär. 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 am 8 Mär. 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by