필터 지우기
필터 지우기

How to plot sum of series?

조회 수: 4 (최근 30일)
Teb Keb
Teb Keb 2020년 11월 27일
댓글: Walter Roberson 2024년 3월 8일
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.

답변 (1개)

Shubham Khatri
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
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
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)
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by