How to use double loops to enter the elements in a 2D array using a formula?
Afficher commentaires plus anciens
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 commentaires
KSSV
le 31 Oct 2017
Why confuse with while loop? USe for loop.
Anand Singh
le 31 Oct 2017
Réponses (1)
Walter Roberson
le 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 commentaires
Anand Singh
le 31 Oct 2017
Walter Roberson
le 31 Oct 2017
Modifié(e) : Walter Roberson
le 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!