How to use double loops to enter the elements in a 2D array using a formula?

I am trying to create a table solution for the given parameters but am unable to get it running in the loop. Kindly help me out!
nu = 225;
G = 6.67408*10^-11;
M = 5.972*10^24;
a = 7500000;
while a <=10000000;
e = 1;
while e <= 10;
r = (a*(1-(e*.1)^2))/(1 + (e*.1)*cos(nu));
phi = atan((e*sin(nu))/(1+e*cos(nu)));
v = sqrt(2*G*M(2/r)-(2/a));
r(a,e) = x;
phi(a,e) = y;
v(a,e) = z;
e = e + 1;
end
a = a + 250000;
end
the loop formed here is 10000000*10 whereas as per the matrix, i should be getting a matrix of 110 elements for each equation with size 10*11 or vice-versa. kindly help we out where i am wrong and what should i do?

2 Comments

Why confuse with while loop? USe for loop.
Sir, can you suggest me how to proceed with that? for i have tried to use the for loop too but the value is entered in the last row of matrix only

Sign in to comment.

Answers (1)

e = 0.1;
nu = 225;
G = 6.67408*10^-11;
M = 5.972*10^24;
a = 7500000;
a_idx = 0;
while a <=10000000;
a_idx = a_idx + 1;
e = 1;
while e <= 10;
r = (a*(1-(e*.1)^2))/(1 + (e*.1)*cos(nu));
phi = atan((e*sin(nu))/(1+e*cos(nu)));
v = sqrt(2*G*M(2/r)-(2/a));
r(a_idx,e) = x;
phi(a_idx,e) = y;
v(a_idx,e) = z;
e = e + 1;
end
a = a + 250000;
end
But note that you initialize e=0.1 but before you use e, you use e = 1; That is confusing.

2 Comments

Sir that was a typo mistake. the value of e has to be taken as 1. tried to follow the steps you mentioned. it just gave only one output rather than a matrix. can you please look into it?
You have
v = sqrt(2*G*M(2/r)-(2/a));
but M is not a function and it is not an array that can be indexed at a fraction.
Also, x is not defined. Or y. Or z.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 31 Oct 2017

Edited:

on 31 Oct 2017

Community Treasure Hunt

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

Start Hunting!