Help with while loop as it failed as left and right sides have different number of elements.

I am trying to work out the velocity of a falling object at 100km above earth. I have inputted initial height as 150km, initial v = 0, and initial t=0. Acceleration (a) in this case is equal to (40x10^7)/(6371+h)^2 Height = is 150k-(0.5*a*t^2). V=a*t loop counter (i) =1 Time step (dt)=0.005
My code is:
>> h(1)=150000;
>> dt=0.005;
>> t(1)=0;
>> v(1)=0;
>> a=(40*10^7)/(6371+h)^2;
>> h=150000-(0.5*a*t^2);
g=(40*10^7)/(6371+h)^2;
v=a*t;
>> while h>=100000
t(i+1)=t(i)+dt;
h(i+1)=150000-(0.5*a*t(i+1)^2);
a(i+1)=(40*10^7)/(6371+h(i+1))^2;
v(i+1)=a(i+1)*t(i+1);
i=i+1;
end
The goal of this loop is to get a value for v with a non constant acceleration that changes with h.
When I do this I get an error saying that the left and right sides have different number of elements. I also think that I need to do something with the differential equations or maybe use the F=ma as I know the mass of the object to be 850kg. I am knew to matlab and dont really know what im doing so any help would be appreciated.

2 Comments

Please post actual code. MATLAB does not use While it uses while and 100km is invalid syntax
Apologies. The code copied and pasted from MatLab:
>> h(1)=150000;
>> dt=0.005;
>> t(1)=0;
>> v(1)=0;
>> a=(40*10^7)/(6371+h)^2;
>> h=150000-(0.5*a*t^2);
g=(40*10^7)/(6371+h)^2;
v=a*t;
>> while h>=100000
t(i+1)=t(i)+dt;
h(i+1)=150000-(0.5*a*t(i+1)^2);
a(i+1)=(40*10^7)/(6371+h(i+1))^2;
v(i+1)=a(i+1)*t(i+1);
i=i+1;
end
Tha aim of the while looop is to work out v at 100km above earth, using a non constant acceleration that changes with h.

Answers (0)

This question is closed.

Products

Asked:

on 8 Mar 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!