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])

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Sep 2018
Edited: KALYAN ACHARJYA on 24 Sep 2018
% 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 Comments

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Sep 2018
Edited: KALYAN ACHARJYA on 24 Sep 2018
t is having only two points 0,300
aa bb
aa bb on 24 Sep 2018
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
aa bb
aa bb on 24 Sep 2018
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.

Sign in to comment.

Products

Release

R2017b

Asked:

on 24 Sep 2018

Commented:

on 24 Sep 2018

Community Treasure Hunt

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

Start Hunting!