How to plot sum of series?
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Shubham Khatri
2020-12-3
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 个评论
Stefan
2024-3-8
编辑:Walter Roberson
2024-3-8
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
2024-3-8
Looks like it works, even if it is not as general as we might hope.
syms x
taylor(log(x), x, 1)
expr(x)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!