How to run an algorithm 5 times and graph all 5 runs in one grid

2 ビュー (過去 30 日間)
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2021 年 8 月 17 日
コメント済み: Prateek Rai 2021 年 8 月 19 日
I have an iterative algorithm that outputs a graph (decaying function) recording progress of a function during the iteration.
I want to run this algorithm 5 times (knowing that each time I run it the output is different) which should output 5 graphs each time I run it but I want these 5 graphs to be plotted in one grid axis. I hope someone can guide me for this problem.

回答 (1 件)

Prateek Rai
Prateek Rai 2021 年 8 月 19 日
To my understanding, you want 5 different graphs to be plotted on one grid axis.
You can use the hold on command to combine plots in the same axes. This will retain plots in the current axes so that new plots added to the axes do not delete existing plots. When you plotted all the 5 graphs on the same axes, you can then use the hold off command to set the hold state to off .
You can refer to Combine Multiple Plots MathWorks documentation page to find more on combining multiple plots in MATLAB. You can also refer to hold MathWorks documentation page to find more on hold commands.
  2 件のコメント
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2021 年 8 月 19 日
編集済み: Tarek Hajj Shehadi 2021 年 8 月 19 日
Hello, thank you for your answer but the issue is that let us say I have an algorithm that outputs where m is a user input value. I want to run this algorithm 5 times each time I insert a different value for m so each output after running the algorithm will be a graph of that equation. Therefore, there will be 5 graphs in total. My question is how can I combine these 5 graphs into one axis containing all 5 graphs?
Prateek Rai
Prateek Rai 2021 年 8 月 19 日
A possible workaround for your y=mx example could be:
x = 1:10;
figure(1)
hold on
for i = 1:5
y = i.*x;
plot(x,y);
end
hold off
In the scenario of taking m as an input value, since it is already decided that the code will run for 5 times and plot 5 graphs accordingly, therefore, you can place the code inside the for loop.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by