Please help me put this into a proper for loop, using increments

Hi, I am not very good with loops, so I did it be hand for 4 steps and then put them into code, I need somehow to put rFinal into a for loop so goes through 1000 times. I tried to do that, but without success, the loop are very incorrect, but I'm not sure how to fix them Use for reference, the variable status in order: Initial, Next, changed, New, Final Also NOTE: Pi in this case is the velocity not the variable "pi" (had to use variable pi..)
NOTE: the part I need help with is at the bottom (*** after the space), right BEFORE THE FOR LOOP, the rest is just variables if you want to run it.... if true %% Defining the variables
% the masses
M1=606; % galactic mass unit, = 2.32e7 of solar masses % central mass constants
M2=3690; % disk constants
M3=4615; % halo constants
% the radii and distances
rInitial=8.499; % kpc
rMin=3.51; % kpc
rMax=9.15; % kpc
a2=5.3178; % disk constants % scalar lengths
a3=12; % halo constants % scalar lengths
b1=0.3873; % central mass constants % scalar lengths
b2=0.25; % disk constants % scalar lengths
z=0;
% other remining variables and constants
PiInitial=6.827; % kpc/gtm
dt=1e-3; %time step
%%the order of solving the variables - done by hand
DeltarInitial= PiInitial*dt
rNext= rInitial + DeltarInitial
% solving for rChanged:
% partials of Fi1 % the acceleration components - with r initial
DiyFi1OverRInitial= (M1*rInitial) *dt / ( (b1^2+rInitial^2+z^2)^(3/2) );
% partials of Fi2 % the acceleration components - with r initial
DiyFi2OverRInitial= (M2*rInitial) *dt / ( (rInitial^2+( (a2+sqrt(b2^2+z^2))^2) )^(3/2) );
% partials of Fi3 % the acceleration components - with r initial
DiyFi3OverRInitialTerm1= ( 1.02*M3*rInitial*( (sqrt(rInitial^2+z^2) *dt / a3)^2.04 ) ) / (a3*(rInitial^2+z^2)*( 1+( (sqrt(rInitial^2+z^2) / a3)^1.02 )^2 ));
DiyFi3OverRInitialTerm2= ( M3*rInitial*( ( sqrt(rInitial^2+z^2) / a3 )^2.02 ) ) *dt / ( ( (rInitial^2+z^2)^(3/2) )*( 1+( ( sqrt(rInitial^2+z^2) / a3 )^1.02 ) ) );
DiyFi3OverRInitialTerm3= ( -2.02*M3*rInitial*( ( sqrt(rInitial^2+z^2) / a3 )^1.01 ) ) *dt / ( a3*(rInitial^2+z^2)*( 1+( ( sqrt(rInitial^2+z^2) / a3 )^1.02 ) ) ); % a negative term
DiyFi3OverRInitial= DiyFi3OverRInitialTerm1 + DiyFi3OverRInitialTerm2 + DiyFi3OverRInitialTerm3;
% the acceraration component in the r direction
arInitial= DiyFi1OverRInitial + DiyFi2OverRInitial + DiyFi3OverRInitial
PiNext=PiInitial + arInitial*dt
DeltarNext= PiNext*dt
rChanged= rNext + DeltarNext
% sloving for rNew:
% partials of Fi1 % the acceleration components - with rNext
DiyFi1OverRNext= (M1*rNext) *dt / ( (b1^2+rNext^2+z^2)^(3/2) );
% partials of Fi2 % the acceleration components - with rNext
DiyFi2OverRNext= (M2*rNext) *dt / ( (rNext^2+( (a2+sqrt(b2^2+z^2))^2) )^(3/2) );
% partials of Fi3 % the acceleration components - with rNext
DiyFi3OverRNextTerm1= ( 1.02*M3*rNext*( (sqrt(rNext^2+z^2) *dt / a3)^2.04 ) ) / (a3*(rNext^2+z^2)*( 1+( (sqrt(rNext^2+z^2) / a3)^1.02 )^2 ));
DiyFi3OverRNextTerm2= ( M3*rNext*( ( sqrt(rNext^2+z^2) / a3 )^2.02 ) ) *dt / ( ( (rNext^2+z^2)^(3/2) )*( 1+( ( sqrt(rNext^2+z^2) / a3 )^1.02 ) ) );
DiyFi3OverRNextTerm3= ( -2.02*M3*rNext*( ( sqrt(rNext^2+z^2) / a3 )^1.01 ) ) *dt / ( a3*(rNext^2+z^2)*( 1+( ( sqrt(rNext^2+z^2) / a3 )^1.02 ) ) ); % a negative term
DiyFi3OverRNext= DiyFi3OverRNextTerm1 + DiyFi3OverRNextTerm2 + DiyFi3OverRNextTerm3;
% the acceraration component in the r direction
arNext= DiyFi1OverRNext + DiyFi2OverRNext + DiyFi3OverRNext
PiChanged=PiNext + arNext*dt
DeltarChanged= PiChanged*dt
rNew= rChanged + DeltarChanged
%
%
%
% sloving for rFinal: - THE LOOP PART******************************************
% partials of Fi1 % the acceleration components - with rChanged
DiyFi1OverRChanged= (M1*rChanged) *dt / ( (b1^2+rChanged^2+z^2)^(3/2) );
% partials of Fi2 % the acceleration components - with rNext
DiyFi2OverRChanged= (M2*rChanged) *dt / ( (rChanged^2+( (a2+sqrt(b2^2+z^2))^2) )^(3/2) );
% partials of Fi3 % the acceleration components - with rNext
DiyFi3OverRChangedTerm1= ( 1.02*M3*rChanged*( (sqrt(rChanged^2+z^2) *dt / a3)^2.04 ) ) / (a3*(rChanged^2+z^2)*( 1+( (sqrt(rChanged^2+z^2) / a3)^1.02 )^2 ));
DiyFi3OverRChangedTerm2= ( M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^2.02 ) ) *dt / ( ( (rChanged^2+z^2)^(3/2) )*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) );
DiyFi3OverRChangedTerm3= ( -2.02*M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^1.01 ) ) *dt / ( a3*(rChanged^2+z^2)*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) ); % a negative term
DiyFi3OverRChanged= DiyFi3OverRChangedTerm1 + DiyFi3OverRChangedTerm2 + DiyFi3OverRChangedTerm3;
% the acceraration component in the r direction
arChanged= DiyFi1OverRChanged + DiyFi2OverRChanged + DiyFi3OverRChanged
PiNew=PiChanged + arChanged*dt
DeltarNew= PiNew*dt
rFinal= rNew + DeltarNew
for i=1:5
arChanged= DiyFi1OverRChanged + DiyFi2OverRChanged + DiyFi3OverRChanged
arChanged= arChanged + 1
for i=1:5
PiNew(i)=PiChanged + arChanged*dt
for k=1:5
DeltarNew(k)= PiNew*dt
for i=1:5
rFinal(i)= rNew + DeltarNew
end
end
end
end
end
end

6 Comments

they are just the variables, so not that scary...
what are you trying to do in this nested for loop part?
for i=1:5
arChanged= DiyFi1OverRChanged + DiyFi2OverRChanged + DiyFi3OverRChanged
arChanged= arChanged + 1
for i=1:5
PiNew(i)=PiChanged + arChanged*dt
for k=1:5
DeltarNew(k)= PiNew*dt
for i=1:5
rFinal(i)= rNew + DeltarNew
end
end
end
end
Also why you are using one variable i for three for loops in this nested for loop? Which values you wanted to save here in an array? and what is the size of that array?
Sorry, but that's what i'm not sure about. not sure if i would use different variables of the same ones. I have to put the last part in the loop. The thing I know is i would be a 4 nested loops, because there are 4 step to solving it. I couldn't figure out the loop, so I tried to do it by hand, and the last part is supposed to repeat with the same formulas, but really not sure how to pass that into a loop...
ok lets put it this way. Which variables you need as a result after nested for loops finish? and are the processing steps same as currently you written in the nested for loops?
Sorry fir the late reply: yep the variables I need in order are the ones in the nested loop, but the method (formula) written right above the loop (starting at **) makes more sense, because the i's and k's within the loop make the formula wrong...

Sign in to comment.

 Accepted Answer

The part after *************** can be written in loop as follow:
DiyFi1OverRChanged= (M1 * rChanged) * dt / ( (b1^2+rChanged^2+z^2)^(3/2) );
DiyFi2OverRChanged= (M2*rChanged) *dt / ( (rChanged^2+( (a2+sqrt(b2^2+z^2))^2) )^(3/2) );
DiyFi3OverRChangedTerm1= ( 1.02*M3*rChanged*( (sqrt(rChanged^2+z^2) *dt / a3)^2.04 ) ) / (a3*(rChanged^2+z^2)*( 1+( (sqrt(rChanged^2+z^2) / a3)^1.02 )^2 ));
DiyFi3OverRChangedTerm2= ( M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^2.02 ) ) *dt / ( ( (rChanged^2+z^2)^(3/2) )*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) );
DiyFi3OverRChangedTerm3= ( -2.02*M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^1.01 ) ) *dt / ( a3*(rChanged^2+z^2)*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) ); % a negative term
DiyFi3OverRChanged= DiyFi3OverRChangedTerm1 + DiyFi3OverRChangedTerm2 + DiyFi3OverRChangedTerm3;
arChanged= DiyFi1OverRChanged + DiyFi2OverRChanged + DiyFi3OverRChanged;
PiNew(1) = PiChanged + arChanged * dt;
DeltarNew(1) = PiNew(1) * dt;
rFinal(1) = rNew + DeltarNew(1);
for i=1:5
arChanged= arChanged + 1;
PiNew(i + 1)= PiChanged + arChanged * dt;
DeltarNew(i + 1)= PiNew(i + 1) * dt;
rFinal(i + 1)= rNew + DeltarNew(i + 1);
end
It it what you need?

6 Comments

Thankyou :)), but Sorry, ;( , the arChanged part is constantly increasing by one (not following the formula), which is making some of the relationships linear (shouldn't be the case). Tying to figure out if the rest is right though... I know it's a very long and complicaed equation, so I wrote pretty-much the same thing for solving rChanged, rNew, and rFinal - just so very variable settles in and there are no solid contestants left for anything I'm solving for in the loop (arChanged, PiNew, DeltarNew, rFinal) But I like your idea of i+1 , trying to see if that will do it...
how do you like to change 'arChanged' in each loop iteration?
the archanged is obtained from: arChanged= DiyFi1OverRChanged + DiyFi2OverRChanged + DiyFi3OverRChanged - and then the DiyFi...'s also have long equations to find them, they also have to be in to loop because they need the rFinal from the loop to solve
you can do it as follow:
for i = 1:5
DiyFi1OverRChanged(i) = (M1 * rChanged) * dt / ((b1^2+rChanged^2+z^2)^(3/2));
DiyFi2OverRChanged(i) = (M2*rChanged) *dt / ( (rChanged^2+( (a2+sqrt(b2^2+z^2))^2) )^(3/2) );
DiyFi3OverRChangedTerm1(i) = ( 1.02*M3*rChanged*( (sqrt(rChanged^2+z^2) *dt / a3)^2.04 ) ) / (a3*(rChanged^2+z^2)*( 1+( (sqrt(rChanged^2+z^2) / a3)^1.02 )^2 ));
DiyFi3OverRChangedTerm2(i) = ( M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^2.02 ) ) *dt / ( ( (rChanged^2+z^2)^(3/2) )*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) );
DiyFi3OverRChangedTerm3(i) = ( -2.02*M3*rChanged*( ( sqrt(rChanged^2+z^2) / a3 )^1.01 ) ) *dt / ( a3*(rChanged^2+z^2)*( 1+( ( sqrt(rChanged^2+z^2) / a3 )^1.02 ) ) ); % a negative term
DiyFi3OverRChanged(i) = DiyFi3OverRChangedTerm1(i) + DiyFi3OverRChangedTerm2(i) + DiyFi3OverRChangedTerm3(i);
arChanged(i) = DiyFi1OverRChanged(i) + DiyFi2OverRChanged(i) + DiyFi3OverRChanged(i);
PiNew(i) = PiChanged + arChanged(i) * dt;
DeltarNew(i + 1)= PiNew(i) * dt;
rFinal(i)= rNew + DeltarNew(i);
end
Does it solve correctly now?
First of all, thankyou sooo much for all your effort!! It's mostly correct, :)) I'm just trying to find the error which is keeping rFinal the same value, tried for 1000 loops but the rFinal remains exactly the same. rFinal should increase by a few. Trying to figure it out right now, will let you know when I get it ;)). Thank again!!
you are welcome.
You can check the values on which rFinal depends. if those values are changing then rFinal must also change accordingly

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 5 Dec 2013

Edited:

on 8 Dec 2013

Community Treasure Hunt

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

Start Hunting!