How to use double loops to enter the elements in a 2D array using a formula?
Show older comments
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
KSSV
on 31 Oct 2017
Why confuse with while loop? USe for loop.
Anand Singh
on 31 Oct 2017
Answers (1)
Walter Roberson
on 31 Oct 2017
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
Anand Singh
on 31 Oct 2017
Walter Roberson
on 31 Oct 2017
Edited: Walter Roberson
on 31 Oct 2017
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.
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!