I'm trying to simulate a "toy rocket" launch and I want to get the plots of height-time and velocity-time. I've used the code below to simulate the timeperiod 0-0.15 seconds but it takes forever to plot the whole code (step1,step2,step3). Instead I would like to save the loop-data in a array/matrix and plot all the steps later on in the code. How can I succeed with this?
- Functions*
function [a] = acceleration(F,m,g)a=(F-m*g)/m;function [v] = velocity(a,t,v0,t0)v=v0+a*(t-t0);function [h] = height(a,t,h0,v0,t0)h=h0+v0*(t-t0)+(1/2)*a*(t-t0)^2;
Code starts:
v0=0; %Initial velocity
h0=0; %Initial height
t0=0; %Initial time
g=9.81; %The gravitational constant
%Input variables
F=input('Engine force (N): '); m=input('Mass (kg): '); dt=input('Precision/timesteps (s): '); %Step one
hold on for t = t0: dt: 0.15 velocity1=velocity(acceleration(F,m,g),t,v0,t0); height1=height(acceleration(F,m,g),t,h0,v0,t0); plot(t,height1); h=plot(t,velocity1); set(h,'Color','r'); end
Best Answer