Info

This question is closed. Reopen it to edit or answer.

How to begin with multiple graphs of 'y(x+1)=y(​x)+0.02*ra​ndn;' from x=1? i.e. 10 graphs?

1 view (last 30 days)
a=0;
b=100;
c=0;
y=a+(b-a).*rand;
for x=1:365
y(x+1)=y(x)+0.02*randn;
if y(x+1)>100
y(x+1)=y(x)
else if y(x+1)<0
c=c+1
break
end
plot(y)
c
end
end

Answers (1)

Chris Perkins
Chris Perkins on 17 Nov 2017
Hi Karolina,
If I understand your question correctly, you want multiple graphs created, and then to plot your function on all of them.
You can create multiple figures, and store a handle to the axes for each, you can then later use that axes handle to plot on the graph of that specific figure.
Here's your code, with a few lines that I have added to show how you can accomplish this:
a=0;
b=100;
c=0;
y=a+(b-a).*rand;
% Added Code
axisHandles = [];
for i=1:10
figure
axisHandles(i) = gca;
end
% End Added Code
for x=1:365
y(x+1)=y(x)+0.02*randn;
if y(x+1)>100
y(x+1)=y(x)
else if y(x+1)<0
c=c+1
break
end
% Adjusted Code
for i=1:10
plot(axisHandles(i),y);
end
% End Adjusted Code
c
end
end
If you wanted different plots on each graph, you can change the second section of added code to plot a different function on each graph.
Alternatively, if you wanted all of these graphs in the same figure, you could use the "subplot" command, as described in the following documentation page:
  2 Comments
Karolina
Karolina on 19 Nov 2017
I apologize for asking unclearly, I wasn't sure how to put this into words. I mean to ask, how to plot ten different results on same figure?
Karolina
Karolina on 19 Nov 2017
Here is what I tried but doesn't seem to work
a=0;
b=100;
c=0;
y_i=a+(b-a).*rand;
for i=1:10
for x=1:365
y_i(x+1)=y_i(x)+0.02*randn;
if y_i(x+1)>100
y_i(x+1)=y_i(x)
else if y_i(x+1)<0
c=c+1
break
end
plot(y_i)
end
end
end

Community Treasure Hunt

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

Start Hunting!