MATLAB Answers

Please help: How to do a time step?

138 views (last 30 days)
Here is my code, i need the final velocity from u_1 to be the initial velocity for u_2. How do i do this please?
close all
clc;
%define variables
mi_1=2290000;
mf_1=130000;
tb_1=165;
Isp_1=263;
mi_2=496200;
mf_2=40100;
tb_2=360;
Isp_2=421;
mi_3=123000;
mf_3=13500;
tb_3=500;
Isp_3=421;
g=9.81; %acceleration due to gravity
%stage 1
t_1=linspace(0,165);
mr_1=mi_1-((mi_1-mf_1)*(t_1/tb_1));
u_1=g.*(Isp_1.*log(mi_1./mr_1)-t_1); %velocity function
a_1=gradient(u_1, t_1(2)-t_1(1)); %differentiation of velocity function to get acceleration
p_1= cumtrapz(t_1,u_1); %integration of velocity function to get position
%end of stage 1
%stage 2
t_2=linspace(165,525);
mr_2=mi_2-((mi_2-mf_2)*(t_2/tb_2));
u_2=g.*(Isp_2.*log(mi_2./mr_2)-t_2);
a_2=gradient(u_2, t_2(2)-t_2(1));
p_2= cumtrapz(t_2,u_2);
%end of stage 2
%stage 3
t_3=linspace(525,1025);
mr_3=mi_3-((mi_3-mf_3)*(t_3/tb_3));
u_3=g.*(Isp_3.*log(mi_3./mr_3)-t_3);
a_3=gradient(u_3, t_3(2)-t_3(1));
p_3= cumtrapz(t_3,u_3);
%end of stage 3
%position-time graph
figure
plot(t_1,p_1,'blue',t_2,p_2,'blue',t_3,p_3,'blue')
title('Position')
ylabel('height[m]')
xlabel('time[s]')
%velocity-time graph
figure
plot(t_1,u_1,'red',t_2,u_2,'red',t_3,u_3,'red')
title('Velocity')
ylabel('Velocity[m/s]')
xlabel('time[s]')
%acceleration-time graph
figure
plot(t_1,a_1,'green',t_2,a_2,'green',t_3,a_3,'green')
title('Acceleration')
ylabel('Acceleration[m/s^2]')
xlabel('time[s]')

  0 Comments

Sign in to comment.

Accepted Answer

Raj
Raj on 3 Dec 2019
The easiest way here would be:
1) Compute u_1 and u2 as you have done already.
2) Just take the last value of u_1 and append it as first value of u_2.
3) Plot accordingly.
So after computation of u_1 and u_2, you can use this:
u_2=[u_1(1,100) u_2]
t_2=[t_1(1,100) t_2]
And your plot command will be same as what you have now:
plot(t_1,u_1,'red',t_2,u_2,'red')
Capture.JPG
Update the code for other parameters on similar lines. Hope this is what you are looking for. Get back in case you get stuck somewhere!

  3 Comments

Chukwunememma Uponi
Chukwunememma Uponi on 3 Dec 2019
Hello, thanks for answering. It works for u_1,2,3 but it does not work for a_1,2,3 and p_1,2,3. I also need to plot a continuous graph for those. thanks!
Raj
Raj on 3 Dec 2019
The logic works irrespective of variable. The code should look like this:
close all
clc;
%define variables
mi_1=2290000;
mf_1=130000;
tb_1=165;
Isp_1=263;
mi_2=496200;
mf_2=40100;
tb_2=360;
Isp_2=421;
mi_3=123000;
mf_3=13500;
tb_3=500;
Isp_3=421;
g=9.81; %acceleration due to gravity
%stage 1
t_1=linspace(0,165);
mr_1=mi_1-((mi_1-mf_1)*(t_1/tb_1));
u_1=g.*(Isp_1.*log(mi_1./mr_1)-t_1); %velocity function
a_1=gradient(u_1, t_1(2)-t_1(1)); %differentiation of velocity function to get acceleration
p_1= cumtrapz(t_1,u_1); %integration of velocity function to get position
%end of stage 1
%stage 2
t_2=linspace(165,525);
mr_2=mi_2-((mi_2-mf_2)*(t_2/tb_2));
u_2=g.*(Isp_2.*log(mi_2./mr_2)-t_2);
a_2=gradient(u_2, t_2(2)-t_2(1));
p_2= cumtrapz(t_2,u_2);
u_2=[u_1(1,100) u_2]
a_2=[a_1(1,100) a_2]
p_2=[p_1(1,100) p_2]
t_2=[t_1(1,100) t_2]
%end of stage 2
%stage 3
t_3=linspace(525,1025);
mr_3=mi_3-((mi_3-mf_3)*(t_3/tb_3));
u_3=g.*(Isp_3.*log(mi_3./mr_3)-t_3);
a_3=gradient(u_3, t_3(2)-t_3(1));
p_3= cumtrapz(t_3,u_3);
u_3=[u_2(1,101) u_3]
a_3=[a_2(1,101) a_3]
p_3=[p_2(1,101) p_3]
t_3=[t_2(1,101) t_3]
%end of stage 3
%position-time graph
figure
plot(t_1,p_1,'blue',t_2,p_2,'blue',t_3,p_3,'blue')
title('Position')
ylabel('height[m]')
xlabel('time[s]')
%velocity-time graph
figure
plot(t_1,u_1,'red',t_2,u_2,'red',t_3,u_3,'red')
title('Velocity')
ylabel('Velocity[m/s]')
xlabel('time[s]')
%acceleration-time graph
figure
plot(t_1,a_1,'green',t_2,a_2,'green',t_3,a_3,'green')
title('Acceleration')
ylabel('Acceleration[m/s^2]')
xlabel('time[s]')
Pos.JPG
vel.JPG
acc.JPG
Chukwunememma Uponi
Chukwunememma Uponi on 6 Dec 2019
This was so helpful! Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!