I can't plot during my loop! Help!
Show older comments
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.
Answers (1)
Konstantinos Sofos
on 2 Mar 2016
Hi you could use
plot(i,sumX,'k*'); % set black asterisk

4 Comments
Bikram Singh
on 2 Mar 2016
Konstantinos Sofos
on 2 Mar 2016
Edited: Konstantinos Sofos
on 2 Mar 2016
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
on 2 Mar 2016
Walter Roberson
on 2 Mar 2016
That is the adjusted code that Konstantinos posted.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!