필터 지우기
필터 지우기

Plotting summation using for loop

조회 수: 2 (최근 30일)
James Crowe
James Crowe 2017년 10월 26일
편집: James Crowe 2017년 10월 26일
How would I plot values of i for 1,2,3,4,5 individually on the same graph?
x = -pi:0.1:pi;
ye = cos(x);
n = length(x);
summe = 0.0;
for i = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
hold on
plot (x, summe);
plot (cos(x), ye)
  댓글 수: 2
James Crowe
James Crowe 2017년 10월 26일
Also the cos graph plots between 0 and around 63? How do I sort this?
Birdman
Birdman 2017년 10월 26일
Check my answer.

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

채택된 답변

Birdman
Birdman 2017년 10월 26일
In previous question, as I told you, if you want your n vector as n=1:5, then you have to set your x vector as
x=-pi:pi/2:pi
and
n=1:length(x)
but it will be not wise to do this since when you plot, the figure will be a triangular shape. Therefore, you have to sample as many points as you can,
x = -pi:0.1:pi
is a wise choice.
  댓글 수: 2
Birdman
Birdman 2017년 10월 26일
x = -pi:pi/2:pi;
xx= -pi:0.1:pi;
ye = cos(xx);
n = length(x);
i=1:n;
summe=0;
for i = 1:n
summe = summe + ((-1).^i).*((x.^(2*i))./(factorial(2.*i)));
end
plot (x, summe);
hold on;grid on
plot(xx,ye)
James Crowe
James Crowe 2017년 10월 26일
편집: James Crowe 2017년 10월 26일
Okay thanks I get that. Sorry I'm new to this. I want my graph to look like this with the summation.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by