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 日

0 投票

% 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 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 24 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 24 日
t is having only two points 0,300
aa bb
aa bb 2018 年 9 月 24 日
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 24 日
the sum of the first 3
aa bb
aa bb 2018 年 9 月 24 日
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.

サインインしてコメントする。

カテゴリ

製品

リリース

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