How to start at 0?

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 - the code you have written start at i equal to zero. Isn't that what you want, or is there a problem when you run the above code?
I noticed that M isn't defined before you enter the for loop so that will cause a problem. What is the initial value of M?
Also, you may want to consider renaming your indexing variable i to k since MATLAB uses both i and j to represent the imaginary number.
Anna
Anna on 19 Apr 2015
Edited: Anna on 19 Apr 2015
Hi! Yes I wrote it that way but it doesn't show when I plot it.
I will show you the whole code that I wrote :). Thank you for the tip, but my assignment demands me to use variable k.
Mycode=[0 4 9 5 4 7 2];
M = m(My);
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
x;
y;
M;
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

Asked:

on 19 Apr 2015

Edited:

on 19 Apr 2015

Community Treasure Hunt

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

Start Hunting!