How can I store a value from each iteration of a for loop, ready for plotting?
105 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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 个评论
Ganesh Hegade
2017-8-3
Here i am running loop from 1 to length of the matrix [0 : increment : end_time] instead of taking each element of the time signal. So just i am getting increment numbers for for loop instead of actual values. Example: now nTimeLen = 1, 2, 3...... till end earlier loop was 0, 0.01, 0.02 .....
Stephen23
2017-8-3
编辑: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
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177663/image.png)
or plot the velocity as well:
plotyy(timeV,highV,timeV,veloV)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177664/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!