Loops - physics - nonlinear gravity & acceleration

7 views (last 30 days)
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
Harry Sergeant on 5 Apr 2020
The dt part of the code is fully incorrect and i have removed the acceleration value from the loop however it keeps retrieving the error message stating the index exceeds the matrix dimensions which is unclear to me as i am not using a matrix and i am just trying to find the set values of velocity and time at h=(userinput) with it being simulated from a starting position of 150km with respect to a nonlinear gravity value based off the equation shown above.

Sign in to comment.

Answers (1)

James Tursa
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
Harry Sergeant on 6 Apr 2020
h0 = h;
v0 = u;
h(1) = h0;
v(1) = u;
i = 1;
while h(i) > 0
dt = 1;
g(i)=-(40*10^7)/((6371+h(i))^2);;% calculate acceleration (should be negative)
v(i+1) = u(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
disp(t(i))
disp(v(1))
this is my current code, the input code is the same as previously shown. The error message "Attempted to access u(2); index out of bounds because numel(u)=1." is now displayed once ive entered the values and it seems no calculations are being made
James Tursa
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.

Sign in to comment.

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!