Loops - physics - nonlinear gravity & acceleration

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

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)

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

i have attempted to integrate your code into mine, but unfortunately it didnt work. i have taken the equation for g out and tested it with a prefixed value and it was successful in finding a value, however i assume it is the issue with the loop as its the code i wrote. If you could help me correct it would be greatly appreciated. it also keeps showing the error message "Subscript indices must either be real positive integers or logicals." but i do not understand the problem with the indices.
m=0;
csa=0;
s=0;
u=0;
v=0;
t=0;
dt=0;
a=0;
a=a*t;
h=0;
g=(40*10^7)/((6371+h)^2);
Qnumb = ('Question 1, Find V and T');
disp(Qnumb);
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);
g(i)=(40*10^7)/((6371+h)^2);
h0 = h;
v0 = u;
h(1) = h0;
v(1) = u;
i = 1;
while h(i) > 0
dt = 1;
g(i+1)= g(i)*dt;% 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
end
disp(t(i+1))
disp(v(i+1))
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);
g(i) would not calculate in the numerical value i displayed as the equation so i had to separate it into separate values and it seems to be working without any errors being displayed. However, it shows no results even when asked to display. When i test the individual loops it doesnt seem to be calculating new values and inputting them back in. the loop seems to be working but the values are just repeated over and over again. Any ideas on the matter would be greatly appreciated.
-harry
m=0;
csa=0;
s=0;
u=0;
v=0;
t=0;
dt=0;
a=0;
a=a*t;
h=0;
Qnumb = ('Question 1, Find V and T');
disp(Qnumb);
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);
A=(40*10^7);
B=((6371+h).^2);
h0 = h;
v0 = u;
h(1) = h0;
v(1) = u;
i = 1;
while h(i) > he
dt = 10;
g(i)= -(A/B);% 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
end
disp(t(i))
disp(v(i))
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.
It states the "Error using / Matrix dimensions must agree dimensions must agree" as an error whenever i try using this form.
You also need i = i + 1 as the last line in the while loop.
What does your current code look like?
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
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

Asked:

on 5 Apr 2020

Commented:

on 6 Apr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!