フィルターのクリア

Small "toy rocket" - Simulation

4 ビュー (過去 30 日間)
fxo
fxo 2013 年 4 月 23 日
回答済み: Damian Prashad 2019 年 10 月 28 日
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

採用された回答

Kye Taylor
Kye Taylor 2013 年 4 月 23 日
編集済み: Kye Taylor 2013 年 4 月 23 日
You're super close...
Just make one change to the height function so it looks like this one below:
function [h] = height(a,t,h0,v0,t0)
h=h0+v0*(t-t0)+(1/2)*a*(t-t0).^2;
end
(Notice the .^ instead of ^ to signify element-wise exponentiation. This way you can pass entire vectors in place of t.)
Then, modify the main code by remove the for-loop and instead use something like
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
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,'b',t,velocity1,'r');
Notice that t is now a vector and that I'm only calling the plot function once, the first curve (height vs. t) is blue ('b') and the second curve (velcoity1 vs t) is red. Hopefully that helps.
  3 件のコメント
Kye Taylor
Kye Taylor 2013 年 4 月 23 日
編集済み: Kye Taylor 2013 年 4 月 23 日
Sure... could you accept my anser? Builds up my street cred...
Anyway, the general syntax for a for loop is
for incrementVar = vectorVar
% Code in for loop typically uses the variable incrementVar.
% Code block is repeated length(vectorVar) times.
% The ith time through,
% the value of incrementVar is vectorVar(i)
end
for example
x = rand(3,1); % vector of random variables
y = 1:length(x); % vectorVar
for i = y % Variable i will take values in y
str = ['Value ',num2str(i),' of x is ',num2str(x(i))];
disp(str)
end
Is there any part of this that I can clarify?
fxo
fxo 2013 年 4 月 24 日
編集済み: fxo 2013 年 4 月 24 日
So then this code should be correct
t=t0:dt:0.15;
y=1:length(t);
for i = y
velocity1=velocity(acceleration(F,m,g),t,v0,t0);
height1=height(acceleration(F,m,g),t,h0,v0,t0);
end
It do seem to work so once again, thank you really appreciate it!

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

その他の回答 (1 件)

Damian Prashad
Damian Prashad 2019 年 10 月 28 日
Cool project. Now I'd like to do the opposite and land a rocket. Can someone help me with the code? I'd like to use state space form given intial conditions.
Thank you,

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by