How to plot multiple iterations of a while loop on one plot? [MATLAB]

In summary, to plot multiple iterations of a while loop on one plot without distorting the original line, you can build up a vector or array of values and plot them all at the end. This can be done by concatenating the values onto a vector or building an array and then plotting it.
  • #1
nickadams
182
0
If I want to plot multiple iterations of a while loop on one plot, how can I do that? Every time I use "hold on", it just distorts the original line after every iteration instead of drawing independent lines.

Please help
 
Physics news on Phys.org
  • #2
I'd just build up a vector or array of values.

For instance, if you're plotting the convergence (the difference between the iterative result and the 'real' result), I'd just concatenate the value onto the vector of error values:
>> delta=[delta, loop_delta];

If, instead, you're generating an entire vector of values, I'd just build up an array, and then plot them all at the end.

Good luck!
 

1. How do I plot multiple iterations of a while loop on one plot in MATLAB?

To plot multiple iterations of a while loop on one plot in MATLAB, you can use the hold on command after each iteration. This will allow you to plot multiple graphs on the same plot without overwriting the previous one.

2. Can I change the colors and styles of the lines for each iteration in the plot?

Yes, you can use the plot function to specify the color and style of each line in your plot. For example, plot(x,y,'r--') will plot a red dashed line for the data points x and y.

3. How do I label each iteration in the plot?

You can use the legend function to label each iteration in your plot. This function takes in a cell array of labels as an input, in the order of the plots you want to label. For example, legend({'Iteration 1', 'Iteration 2'}) will label the first plot as "Iteration 1" and the second plot as "Iteration 2".

4. Is it possible to save the plot with all the iterations for future use?

Yes, you can save the plot by using the saveas function. This function takes in the figure handle and the desired file name as inputs, and saves the plot as an image file (e.g. .png, .jpg) in your current working directory.

5. Can I add a title and axis labels to the plot?

Yes, you can add a title and axis labels to your plot by using the title, xlabel, and ylabel functions. These functions take in a string as an input, which will be displayed as the title or label on your plot.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
503
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
697
Back
Top