Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
Karolina
Karolina 2017년 11월 15일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

Chris Perkins
Chris Perkins 2017년 11월 17일
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
Karolina
Karolina 2017년 11월 19일
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 2017년 11월 19일
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!

Translated by