フィルターのクリア

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 Center および File ExchangeCalculus についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by