how can i improve this code to bacame better more
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ashrakat Adel
am 13 Dez. 2018
Bearbeitet: Mark Sherstan
am 13 Dez. 2018
% 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')
0 Kommentare
Akzeptierte Antwort
Mark Sherstan
am 13 Dez. 2018
Bearbeitet: Mark Sherstan
am 13 Dez. 2018
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulink Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!