Kinematics while loop is infinite, plus other errors.
이전 댓글 표시
I have posted similar things before, so apologies if you have already seen this.
My code is this:
clear
h(1)=100000; %Initial Height
t(1)=0;
dt=0.005; %Time Step
u=59.29; %Initial Velocity
a(1)=0.03535; %Initial Acceleration
v(1)=u+(a(1)*t(1)); %Velocity
p(1)=(((h(1))/71)+1.4); %Air Density
g(1)=(40*10^7)/((6371+h(1))^2); %Gravity
A=5; %Area
c=0.7;
m=850; %Mass
Fd(1)=0.5*c*(p(1))*A*(v(1))^2; %Air Resistance
i=1; %Loop counter
while h(end)>=0
t(i+1)=t(i)+dt;
h(i+1)=100000-(u*t(i+1))-(0.5*a(i)*t(i+1)^2); %Suvat s=ut+0.5*a*t^2
p(i+1)=(((h(i+1))/71)+1.4);
g(i+1)=(40*10^7)/((6371+h(i+1))^2);
Fd(i+1)=0.5*c*(p(i+1))*A*(v(i))^2;
a(i+1)=g(i+1)-(Fd(i+1)/m); %Acceleration=Gravity-(Fd/m)
v(i+1)=u+(a(i)*t(i+1)); %Suvat v=u+at
i=i+1;
end
The code is infinite. When I stop it running and plot a graph of (t,h) it appears that h drastically increases after the first time step. I cannot see why this is the case as the equation for height should mean h decreases as time goes in. Pehaps it is the order I have had to code my variables? I have tried various orders but coudnt get any to work without the error 'Index exceeds array elements (1)'. Any help would be appricieted.
댓글 수: 4
darova
2020년 3월 19일
Can you show original equations?
Sam Potter
2020년 3월 19일
darova
2020년 3월 19일
Looks like differential equation. Can i see it on the paper or picture? Where did you get it?
Sam Potter
2020년 3월 19일
답변 (2개)
Cris LaPierre
2020년 3월 19일
편집: Cris LaPierre
2020년 3월 19일
0 개 추천
Quick observation is that you are not being careful with your units. H is specified as height in km, but it looks like you are converting it to meters.
It also looks like you missed the negative sign in the calculation of rho (-H/71 + 1.4)
darova
2020년 3월 19일
0 개 추천
Here are some tips

You forgot about units: height should be in km for density

댓글 수: 10
Sam Potter
2020년 3월 19일
편집: Walter Roberson
2020년 3월 20일
darova
2020년 3월 19일
This should give you an answer

Let me know if it's clear
darova
2020년 3월 19일
Please use special button for code inserting

Sam Potter
2020년 3월 20일
darova
2020년 3월 20일
It's not that easy to explain it on your case. You have complicated example
Your equation is:
can be re-written as: 
I'll give you some links
Euler method - simplest method to solve numericaly diff. equation (used in your case)
ODE - what is differential equation
ode45 - main MATLAB solver of ordinary diff equations
Feel free to ask
darova
2020년 3월 20일
- Also does the change in h have to be constant for it to work?
No. All variable can be different (even dt)
Sam Potter
2020년 3월 20일
Sam Potter
2020년 3월 20일
darova
2020년 3월 20일
You can use v*t to calculate distance only when you have v=constant (no changing)
But velocity in this case changes all the time
- Actually I have just thought,is it because you differentiate displacement to get velocity and then differengiate that to get acceleration?
There is no differentiation here. Only integration: h(i+1) = h(i) + v(i)*dt - integration (summation)
I can't explain it to you here. That is not that simple. You should dig into this by yourself. Practice
Sam Potter
2020년 3월 21일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!