MATLAB: Calculating orbital speed using input and loops, wondering if how I can make this work? Thanks for the help!

for loops and inputs

I am trying to use inputs and loops to allow the user to choose whether they would like to go to the moon or mars, and their orbital distance, and have the program give the user the required velocity. If anybody has any ideas on how make this work, or a better way to code it I would apreciate it a lot.
input('Which planet would yoy like to orbit? (Moon/Mars): ','s');
Mars = {3.71,3389500};
[g02,Re2] = Mars{:};
Moon = {1.26,1737125.91};
[g01,Re1] = Moon{:};
% g02 and g01 represents gravitational constant and Re2 and Re1 represents mean radius.
for index = Mars
h = input('What height would you like to orbit at above Mars? ');
Velocity = sqrt((g02*Re2.^2)/(Re2+h));
for index1 = Moon
h = input('What height would you like to orbit at above the Moon? ');
Velocity = sqrt((g01*Re2.^2)/(Re1+h));
end
end
fprintf('The speed rquired to orbit at this altitude is %8.2f Meters/Sec \n' ,Velocity);

Best Answer

Anders - I think you almost have all that you need, though I don't think that you need to use loops...unless you expect the user to continue to choose Mars or the moon and provide different heights above each. A couple of things: the code needs to capture the response to the question about orbiting Mars or the Moon. This response is then used to compare against the strings 'Mars' and 'Moon' to ask the next appropriate question. In your case, you were trying to compare unknown index and index1 variables againts the Mars and Moon cell arrays. Try the following
target = input('Which planet would you like to orbit? (Moon/Mars): ','s');
Mars = {3.71,3389500};
[g02,Re2] = Mars{:};
Moon = {1.26,1737125.91};
[g01,Re1] = Moon{:};
% g02 and g01 represents gravitational constant and Re2 and Re1 represents mean radius.
if strcmpi(target,'Mars')
h = input('What height would you like to orbit at above Mars? ');
Velocity = sqrt((g02*Re2.^2)/(Re2+h));
elseif strcmpi(target,'Moon')
h = input('What height would you like to orbit at above the Moon? ');
Velocity = sqrt((g01*Re2.^2)/(Re1+h));
end
fprintf('The speed required to orbit at this altitude is %8.2f Meters/Sec \n' ,Velocity);
Compare this to your code and note the differences: the target for the user to orbit about, and the use of target in if/elseif statements to determine the correct velocity.