how can i improve this code to bacame better more

1 次查看(过去 30 天)
% matlab program for solving the bungee jumber problem using Eulers method
g=9.81;
m=80;
cd=0.25;
t0=0;
tend=20;
dt=0.5;
vi=0;
t=t0:dt:tend;
% the analytic solution
vel=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);
% the numrical solution
for ti=0:2:20
v1=vi+(g-cd/m*vi.^2).*((ti+2)-ti)
v2=v1+(g-cd/m*v1.^2).*((ti+2)-ti)
v3=v2+(g-cd/m*v2.^2).*((ti+2)-ti)
v4=v3+(g-cd/m*v3.^2).*((ti+2)-ti)
v5=v4+(g-cd/m*v4.^2).*((ti+2)-ti)
v6=v5+(g-cd/m*v5.^2).*((ti+2)-ti)
v7=v6+(g-cd/m*v6.^2).*((ti+2)-ti)
v8=v7+(g-cd/m*v7.^2).*((ti+2)-ti)
v9=v8+(g-cd/m*v8.^2).*((ti+2)-ti)
v10=v9+(c-cd/m*v9.^2).*((ti+2)-ti)
end
tt=[2 4 6 8 10 12 14 16 18 20]
v=[v1 v2 v3 v4 v5 v6 v7 v8 v9 v10]
% plotting of results
plot(t,vel)
hold on
plot(tt,v,'r')
grid
xlabel('time (s)')
ylabel('velocity (m/s)')
title('vilocity for the bungee jumber')
legend('analytical','numerical')

采纳的回答

Mark Sherstan
Mark Sherstan 2018-12-13
编辑:Mark Sherstan 2018-12-13
Give this a try! Let me know if you have questions (I have commented my changes).
% matlab program for solving the bungee jumber problem using Eulers method
g=9.81;
m=80;
cd=0.25;
t0=0;
tend=20;
dt=0.5;
vi=0;
t=t0:dt:tend;
% the analytic solution
vel=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);
% Pre allocate memory for faster run times
v = zeros(11,1);
tt = zeros(11,1);
count = 2;
% Loop through your calculations knowing that initial conditions are zero
for ti = 2:2:20
v(count) = v(count-1) + (g-cd/m*v(count-1).^2).*((ti+2)-ti);
tt(count) = ti;
count = count + 1;
end
% plotting of results
plot(t,vel)
hold on
plot(tt,v','r')
grid
xlabel('time (s)')
ylabel('velocity (m/s)')
title('velocity for the bungee jumber')
legend('analytical','numerical')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by