How to start at 0?

35 views (last 30 days)
Anna
Anna on 19 Apr 2015
Edited: Anna on 19 Apr 2015
For this assignment I have to start at 0, this means that when I plot the figure, it has to start at 0 and end at 30. With the code that I wrote, it starts at 1 and ends at 31 when I plot it. How can I fix this?
x=zeros(31,1);
y=zeros(31,1);
i=0;
for i = 0:30;
if i == 0
x((i+1),1)= sum(sum(M));
y((i+1),1)= 0;
i = i+1;
else
[M,l]=k(M);
x((i+1),1)=sum(sum(M));
y((i+1),1)=l;
i=i+1;
end
end
  4 Comments
Geoff Hayes
Geoff Hayes on 19 Apr 2015
Anna - what is the code that you are using to plot the data? Is it x versus y or something else? Since x is just the sum of all elements in the matrix M then it won't necessarily start at zero...
As an aside, you can simplify the double sum of M to simply
x((i+1),1) = sum(M(:));
Anna
Anna on 19 Apr 2015
Edited: Anna on 19 Apr 2015
Geoff, This is the code that I use to plot the data:
figure('name', 'E.jpg', 'NumberTitle', 'off');
bar(x,'b');
hold on
plot (y,'-ro');
xlabel('T(dags)');
ylabel('A');
grid on
axis ([-5 35 0 10000])

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 19 Apr 2015
Anna - if you wish to plot the first element of y against zero, and each subsequent value at 1,2,3,... then just do the following
plot(0:length(y)-1,y,'ro');
which will plot all elements of y against values of "x" (this isn't your x but the x-axis) from 0 to 30.
Try the above and see what happens!

Categories

Find more on Startup and Shutdown 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!