Loops - physics - nonlinear gravity & acceleration
Show older comments
I was just wondering if anyone could help me understand iteration loops. i have been trying to loop a changing acceleration value which is dependent on the equation "g=(400000000/(6371+h)^2") over a course of time t and output the velocity at a set height however since the height is changing due to an object moving closer to earth under the influence of its gravity im unsure of how to loop two changing values gravity and height with respect to time. i have been struggling with it for a few days and im unsure of what to do, any help is greatly appreciated
Code Below (im sure its wrong in most ways)
m=0;
csa=0;
s=0;
u=0;
v=0;
t=0;
dt=0;
a=a*t;
h=0;
g=(400000000/(6371+h)^2);
Qnumb = ('Question 1, Find V and T');
disp(prompt);
mass = ('what is the mass of the spacecraft (kg)?');
m=input(mass);
iVelocity = ('what is the initial velocity of the spacecraft (m/s)?');
u=input(iVelocity);
iHeight = ('what is the initial height of the spacecraft (km)?');
h=input(iHeight);
he = ('what is the desired height of the spacecraft (km)?');
he=input(he);
i=1;
while he >=0
dt=2*v(i+1)*sin(a)/g;
dt= (v(i+1))/a(i+1);
t(i+1)=t(i)+dt;
h(i+1)=h(i)-(v(i)*dt);
if he >=100
g(i+1)=(400000000/(6371+h)^2);
g(i)=g(i+1);
v(i+1)=v(i)+(g(i+1)*dt);
end
end
disp(v(i+1))
1 Comment
Harry Sergeant
on 5 Apr 2020
Answers (1)
James Tursa
on 5 Apr 2020
Edited: James Tursa
on 6 Apr 2020
Your immediate problem is that h is a vector, so the right hand side of this statement is a vector:
g(i+1)=(400000000/(6371+h)^2);
Same issue with this statement, since g is a vector:
dt=2*v(i+1)*sin(a)/g;
You have a mix of scalars and vectors in your code and it looks like your code is not consistent with this.
I would advise you have separate variables for initial conditions and your vector results to help keep things straight. E.g.,
h0 = initial height
v0 = initial velocity
Then your vectors of changing values would be
t = your time vector
h = your height vector at times in t
v = your velocity vector at times in t
g = your gravity vector at times in t
Your initialization code for the height and velocity vectors would look like
h(1) = h0;
v(1) = v0;
i = 1;
An outline of your basic looping code would then look something like this:
while h(i) > 0
dt = something
g(i) = some expression involving h(i) % calculate acceleration (should be negative)
v(i+1) = v(i) + g(i) * dt; % update velocity
h(i+1) = h(i) + v(i) * dt; % update height
t(i+1) = t(i) + dt; % update time
i = i + 1;
end
If there are conditions that change during the looping (e.g., parachute opens) then you can insert that into the above looping logic as well.
8 Comments
Harry Sergeant
on 5 Apr 2020
James Tursa
on 5 Apr 2020
So, you did not follow the outline I gave you. I had this inside my loop:
g(i) = some expression involving h(i) % calculate acceleration (should be negative)
Instead of doing that, you have this outside the loop:
g(i)=(40*10^7)/((6371+h)^2);
and this inside the loop:
g(i+1)= g(i)*dt;% calculate acceleration (should be negative)
Just follow my outline. Put something like this inside the loop (note the negative sign):
g(i)=-(40*10^7)/((6371+h(i))^2);
Harry Sergeant
on 6 Apr 2020
James Tursa
on 6 Apr 2020
Edited: James Tursa
on 6 Apr 2020
OK, well, I told you exactly the line to put inside your loop. Not sure why you again didn't do it. The line is
g(i)=-(40*10^7)/((6371+h(i))^2);
You have to re-calculate g(i) with the current h(i) at each step in the iteration. You can't use a constant like -(A/B) as you are currently doing.
Harry Sergeant
on 6 Apr 2020
James Tursa
on 6 Apr 2020
Edited: James Tursa
on 6 Apr 2020
You also need i = i + 1 as the last line in the while loop.
What does your current code look like?
Harry Sergeant
on 6 Apr 2020
James Tursa
on 6 Apr 2020
This
v(i+1) = u(i) + g(i) * dt; % update velocity
needs to be this
v(i+1) = v(i) + g(i) * dt; % update velocity
You should be updating the next value of v with the current value of v, not the current value of u.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!