How to plot all values of a variable that changes?

4 views (last 30 days)
I have some code where x starts off at some number and then is either divided by 2 or multiplied by 3 and then added to 1. This goes on until x=1. I need to keep track of every value that x was in order to reach 1.

Accepted Answer

Star Strider
Star Strider on 2 Dec 2015
Edited: Star Strider on 2 Dec 2015
This assumes ‘x’ begins as greater than one and will stop when it is less than or equal to one (taking into account floating-point-approximation error):
k1 = 1;
while x > 1
... CODE ...
x_sav(k1) = x; % Save Each Value OF ‘x’
k1 = k1 + 1;
end
Save ‘x’ in a separate vector after you have calculated it in each iteration of the loop.
  4 Comments
Star Strider
Star Strider on 2 Dec 2015
My pleasure!
You did all the heavy lifting — I just tweaked your code a bit to save your iterative ‘x’ values.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!