I can't plot during my loop! Help!

if true
disp('Question 3')
nMAX = input('Enter nMAX');
sumX=0;
i=1;
while i<=nMAX
plot(i,sumX); hold on
sumX = sumX + 1/i^2;
i=i+1;
end
disp('The sum of the series is');
disp(sumX);
disp('END Question 3');
end
The above is my code, when I try to plot, nothing appears. Im new to matlab so go easy lol. I would like to put sumx against i for 1:50. Thanks.

답변 (1개)

Konstantinos Sofos
Konstantinos Sofos 2016년 3월 2일

0 개 추천

Hi you could use
plot(i,sumX,'k*'); % set black asterisk

댓글 수: 4

Bikram Singh
Bikram Singh 2016년 3월 2일
Hi, thanks it does work.... but could you explain why that works and is there a way to just make the plot a line rather than a set of points?
Konstantinos Sofos
Konstantinos Sofos 2016년 3월 2일
편집: Konstantinos Sofos 2016년 3월 2일
To make a line you need to have vectors...in your case you give just points...one possibility would be
if true
disp('Question 3')
nMAX = input('Enter nMAX : ');
sumX=0;
for i = 1 : nMAX -1
sumX(i+1) = sumX(i) + 1/i^2;
end
plot(1:nMAX,sumX);
disp('The sum of the series is');
disp(sumX(end));
disp('END Question 3');
end
Bikram Singh
Bikram Singh 2016년 3월 2일
So, how would I adjust the code to make this happen? Sorry, very new to Matlab.
Walter Roberson
Walter Roberson 2016년 3월 2일
That is the adjusted code that Konstantinos posted.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 3월 2일

댓글:

2016년 3월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by