Plotting a power series

I want to plot a power series with the first non zero term, sum of the first two terms, up to a sum of terms defined by the for loop. I am confused how to add each sum because when I try g(n+1)=g(n)+ series it tells me the number of elements in A and B must be the same. The original function is: 10*cos(50t). I have found the series as seen inside the for loop but it does not plot the summations
clear;
N=400;
prompt = ('Enter number of terms to sum: ');
i=input(prompt);
t=linspace(0,300,N+1);
f=10*cos(50*t);
g=zeros(1,N+1);
for n=1:i
g = g + ((-1)^n * (2500^n) * t.^(2*n)/(2*factorial(n)))
end
hold on
plot(t,g)
axis([0 150 -12 15])

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-9-24
编辑:KALYAN ACHARJYA 2018-9-24
% I didn't find any error during running the code, May be there is logical error
clc;
clear;
close all;
N=400;
prompt=('Enter number of terms to sum:');
i=input(prompt);
t=linspace(0,300,N+1);
f=10*cos(50*t);
g=zeros(1,N+1);
for n=1:i
g = g + ((-1)^n * (2500^n) * t.^(2*n)/(2*factorial(n)));
end
hold on
plot(t,g)
axis([0 150 -12 15])

4 个评论

t is having only two points 0,300
it does not plot the the first term, sum of first 2 , sum of first 3 , etc... I am not sure where am I going wrong with the loop
the sum of the first 3
I want it to plot the sum of 1, then 2, then 3 with respect to the x axis. I believe right now it just sums all the terms in one go and shows just a line instead of a curve of some sort.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品

版本

R2017b

提问:

2018-9-24

评论:

2018-9-24

Community Treasure Hunt

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

Start Hunting!

Translated by