how can i improve this code to bacame better more

조회 수: 1 (최근 30일)
Ashrakat Adel
Ashrakat Adel 2018년 12월 13일
편집: Mark Sherstan 2018년 12월 13일
% 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