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

Click For Summary
SUMMARY

To plot multiple iterations of a while loop on a single plot in MATLAB, utilize the "hold on" command correctly to avoid distorting the original line. Instead of plotting each iteration immediately, build up a vector or array of values. For example, concatenate the iterative results into an error vector using the syntax delta=[delta, loop_delta];. After accumulating all values, plot them collectively to achieve the desired visualization.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of while loops in programming
  • Basic knowledge of plotting functions in MATLAB
  • Experience with vector and array manipulation in MATLAB
NEXT STEPS
  • Research MATLAB's "hold on" and "hold off" commands for plotting
  • Learn about vector concatenation in MATLAB
  • Explore MATLAB's plotting functions such as plot and subplot
  • Investigate techniques for visualizing convergence in iterative algorithms
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and anyone involved in visualizing iterative processes or convergence in numerical methods.

nickadams
Messages
182
Reaction score
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
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!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K