필터 지우기
필터 지우기

Trying to use summation notation in a for loop

조회 수: 1 (최근 30일)
James Crowe
James Crowe 2017년 10월 26일
댓글: Birdman 2017년 10월 26일
Hi I'm trying to estimate a cos graph using summation between 1:1, 1:2, 1:3, 1:4 and 1:5. How would I plot each of these individually?
Could someone help me out please! Thank you
x = -pi:0.1:pi;
ye = cos (x);
n = 5;
summe = 0.0;
for i = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot (x, summe);

채택된 답변

Birdman
Birdman 2017년 10월 26일
편집: Birdman 2017년 10월 26일
Firstly, this code will give an error since X vector is 1x63 and n is 1x5. There will be a size mismatch. You have to correct this. Use the following code.
x = -pi:0.1:pi;
ye = cos (x);
n = length(x);%has to be same size with x
i = 1:n;
for k = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot(x,summe)
  댓글 수: 2
James Crowe
James Crowe 2017년 10월 26일
It's for some coursework I've been told to use a for loop
Birdman
Birdman 2017년 10월 26일
I have corrected it.

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

추가 답변 (1개)

KSSV
KSSV 2017년 10월 26일
x = -pi:0.1:pi;
ye = cos (x);
n = length(x);
i = 1:n;
summe = 0.0;
for k = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot (x, summe);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by