How can I store a value from each iteration of a for loop, ready for plotting?

79 ビュー (過去 30 日間)
A
A 2017 年 8 月 2 日
編集済み: Stephen23 2017 年 8 月 3 日
Hey guys, I'm trying a fairly basic exercise, to simulate a bouncing ball by calculating it's height. Currently what i have is this:
height = 150
velocity = 0
acceleration = -9.81
bounce_factor = -0.4
increment = 0.01
end_time = 20
hold on
for time = 0 : increment : end_time
height = height + velocity * increment
velocity = velocity + acceleration * increment
if (height <= 0 && velocity < 0)
height = 0
velocity = velocity * bounce_factor
end
scatter(time,height)
end
This works but ideally I'd like to display using a smooth linegraph. My main approach would be to store the height in an array of equal size to the time variable, and then plot the two. I am having troubles iterating the array, I am more used to C and the old fashioned approach to arrays, and am unsure of how i can store all my values.
I have tried this approac to no avail:
height_store = zeros(end_time/increment,1)
store_inc = 0
hold on
for time = 0 : increment : end_time
height = height + velocity * increment
velocity = velocity + acceleration * increment
if (height <= 0 && velocity < 0)
height = 0
velocity = velocity * bounce_factor
end
height_store(store_inc) = height
store_inc = store_inc + 1
end
plot(time,height_store)
This seems really basic, so it's proabably just a matter of properly using Matlabs data types. I checked the documentation and couldn't find what I needed (or didn't recognise it as such!)
Many thanks

回答 (2 件)

Ganesh Hegade
Ganesh Hegade 2017 年 8 月 2 日
you can store values like this.
height = 150;
velocity = 0;
acceleration = -9.81;
bounce_factor = -0.4;
increment = 0.01;
end_time = 20;
Height = [];
for nTimeLen= 1: length(0 : increment : end_time)
height = height + velocity * increment ;
velocity = velocity + acceleration * increment ;
if height <= 0 && velocity < 0
height = 0;
velocity = velocity * bounce_factor;
end
Height(nTimeLen) = height;
end
mTime = 0 : increment : end_time;
plot(mTime , Height);
  3 件のコメント
Stephen23
Stephen23 2017 年 8 月 3 日
Note this code has several "features" that could be improved:
  • expanding arrays inside loop without array preallocation: for large arrays this is very inefficient as the array must get moved in memory each time it changes size.
  • creating a large vector twice: first length(0:increment:end_time), then later mTime = 0:increment:end_time;. Simply create the array once and measure its length.
  • Variables whose names only differ by case: although this is permitted it is a regular cause of bugs by beginners (as evidenced by many threads on this forum), and is not recommended.

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


Stephen23
Stephen23 2017 年 8 月 3 日
編集済み: Stephen23 2017 年 8 月 3 日
You should preallocate the output array and use indexing to put the values into it. In this example I also create an array for the velocity: this is not strictly required and is a tad slower, but it allows you to generate all time, height, and velocity vectors (e.g. for plotting). I preallocated the arrays using |NaN|s: this is a simple way to check if data is being calculated, and where bugs occur during a loop. Note that the initial values do not change when the code is run: this is very useful when debugging code!
timeV = 0:increment:end_time;
veloV = nan(size(timeV));
highV = nan(size(timeV));
veloV(1) = velocity;
highV(1) = height;
for k = 2:numel(timeV)
highV(k) = highV(k-1) + increment*veloV(k-1);
veloV(k) = veloV(k-1) + increment*acceleration;
if highV(k)<=0 && veloV(k)<0
highV(k) = 0;
veloV(k) = veloV(k)*bounce_factor;
end
end
you can then do a simple plot:
plot(timeV,highV)
or plot the velocity as well:
plotyy(timeV,highV,timeV,veloV)

Community Treasure Hunt

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

Start Hunting!

Translated by