How do i store an array each time i run the for loop?

How would i store values for F in a seperate array each time i run the for loop and will they be placed in the workspace? I would therefore be looking for 181 different arrays with 101 columns in each.
Is there any way of showing which value of phi corresponds to its F array?
theta=-20*pi/180:pi/180:80*pi/180;
for phi=0:pi/180:pi
phi;
gamma=phi+BOC;
C2=((rmax^2)-(rmin^2))/((cos(gamma+thetamin)-cos(gamma+thetamax)));
C1=(rmax^2)+C2*cos(gamma+thetamax);
a=((C1+C2)^(1/2)+(C1-C2)^(1/2))/2;
b=C2/(2*a);
r=sqrt(C1-C2*cos(gamma+theta));
F=(r/b*a)*m*g*OC*(cos(theta)/sin(gamma+theta));
end

 Accepted Answer

One way is to make F a cell array. Notice that in the loop, I am accessing F using curly brackets instead of parentheses. Each F{i} is an array.
Is there any reason to keep them as separate arrays? You could also just make F one array, which is 101x181.
[Apologies for any little syntax errors here. I could not actually run your code without knowing your constants.]
theta=-20*pi/180:pi/180:80*pi/180;
phi_vec = 0:pi/180:pi;
Nphi = numel(phi_vec);
F = cell(Nphi,1);
for np = 1:Nphi
phi = phi_vec(np);
gamma=phi+BOC;
C2=((rmax^2)-(rmin^2))/((cos(gamma+thetamin)-cos(gamma+thetamax)));
C1=(rmax^2)+C2*cos(gamma+thetamax);
a=((C1+C2)^(1/2)+(C1-C2)^(1/2))/2;
b=C2/(2*a);
r=sqrt(C1-C2*cos(gamma+theta));
F{np}=(r/b*a)*m*g*OC*(cos(theta)/sin(gamma+theta));
end

3 Comments

Thanks! when i ran the code thousands of ' [1x101 double]' appeared in the command window what is this? F could be made using one array, probably easier that way actually. Where would the array be saved to as it does not show up in the workspace. is it just made in the script after the loop to then be used from there on after?
the constraints are
rmin=1.2; % (m)
rmax=2.2; % (m)
OC=4; % (m)
thetamin=-(pi/9); % (rad)
thetamax=((4*pi)/9); % (rad)
BOC=(pi/9); % (rad)
m=4000; %(kg)
g=9.81; %(ms^-2)
theta=-20*pi/180:pi/180:80*pi/180; %(rad)
how would i then make the F array 101x181?
The reason you get all the command window output is probably that you neglected to include a semicolon at the end of a line, probably the line that calculates F, inside the loop.
This code will make F one large array, instead of a cell array of vectors:
phi_vec = 0:pi/180:pi;
Nphi = numel(phi_vec);
F = zeros(Nphi,101);
for np = 1:Nphi
phi = phi_vec(np);
gamma=phi+BOC;
C2=((rmax^2)-(rmin^2))/((cos(gamma+thetamin)-cos(gamma+thetamax)));
C1=(rmax^2)+C2*cos(gamma+thetamax);
a=((C1+C2)^(1/2)+(C1-C2)^(1/2))/2;
b=C2/(2*a);
r=sqrt(C1-C2*cos(gamma+theta));
F(np,:)=(r/b*a)*m*g*OC*(cos(theta)/sin(gamma+theta));
end
Notice that I changed both the initialization of F, and the calculation.
You can transpose F with the command like this:
F = F.';

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!