Create a list within a for loop
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a for loop, in which I solve a coupled second order system. I'm interested in making 2 lists of some values I get from solving this system, but I don't know how to create these lists. I want these list so I can plot them. The values I want is some of the end values from my phase 2, look at the end of the code to see what values I mean.
The code is:
for L = 0.5:0.1:1.5
global L_4 m M M_K L_1 L_2 L_3 theta_i
% constant that varies from experiment to experiment
L_4 = L; %length in meter
% constants
m = 0.006; %weight in kg
M = 0.640; %weight in kg
M_K = 0.088; %weight in kg
L_1 = 0.295; %length in meter
L_2 = 0.07; %length in meter
L_3 = 0.0925; %length in meter
theta_i = 48*(pi/180); %angle in radians
% Phase 1
t1 = [0 1.3902]; %reduced time scale
I_1 = [theta_i; 0; 0; 0]; %initial theta, theta', spi and spi'
options = odeset('RelTol',1e-9,'AbsTol',1e-9,'Events',@Begivenheder_fase1);
[T1,Y1] = ode45(@DifLign_fase1,t1,I_1,options); %call of ode45 to solve the system
L = L_3/L_2;
l = L_1/L_2;
l_4 = L_4/L_1;
b1 = theta_i;
mu = m/M;
mu_b= M_K/M;
sin_phi = (cos(b1)-cos(Y1(:,1)))/l_4;
phi = asin(sin_phi);
cos_phi = cos(phi);
f = -cos(Y1(:,1)-phi)./cos_phi;
df = sin(Y1(:,1)-phi)./cos_phi-sin(Y1(:,1)).^2/l_4./cos_phi.^3;
HS1 = (1-1/2*mu_b*(l-1))*sin(Y1(:,1))+L*sin(Y1(:,1)+Y1(:,2)).*Y1(:,4).^2-mu*l^2*f.*df.*Y1(:,3).^2;
HS2 = -sin(Y1(:,2))+sin(Y1(:,1)+Y1(:,2)).*Y1(:,3).^2;
VS11 = 1+mu*l^2*f.^2+1/3*mu_b*(l^2-l+1);
VS12 = L*cos(Y1(:,1)+Y1(:,2));
VS21 = cos(Y1(:,1)+Y1(:,2));
VS22 = L*ones(size(VS11,1),1);
L_off = ones(size(VS11,1),1);
NormalForce = ones(size(VS11,1),1);
for i=1:size(VS11,1)
VS = [VS11(i) VS12(i); VS21(i) VS22(i)];
HS = [HS1(i);HS2(i)];
Angular_acc = inv(VS)*HS;
acc = Angular_acc(1);
L_off(i)=cos(phi(i)-Y1(i,1))*acc;
L_off(i)=L_off(i)+sin(phi(i)-Y1(i,1))*Y1(i,3)^2+sin(Y1(i,1))^2*Y1(i,3)^2/l_4/cos_phi(i)^2;
L_off(i)=l*sin_phi(i)*L_off(i);
L_off(i)=L_off(i)-cos_phi(i)^2;
NormalForce(i) = -m*1000*L_off(i);
end
% Phase 2
% initial values
Y_1 = Y1(:,1); % Extract first column
Y_2 = Y1(:,2); % Extract second column
Y_3 = Y1(:,3); % Extract third column
Y_4 = Y1(:,4); % Extract last column
phi = asin(L_1/L_4*(cos(theta_i)-cos(Y_1(end)))); %phi start value
t2 = [T1(end) T1(end)+0.8]; %reduceret time scale
I_2 = [Y_1(end); Y_1(end); phi; Y_1(end); Y_1(end); 0]; %initial values
[T2,Y2] = ode45(@DifLign_fase2,t2,I_2); %call of ode45 to solve the system
% List of release angle theta and phi
Y2_1 = Y2(:,1); % Extract first column (theta)
Y2_3 = Y2(:,3); % Extract second column (phi)
theta = Y2_1(end); %Creating a list of the release angles theta
phi = Y2_3(end); %Creating a list of the release angles phi
end
4 comentarios
Star Strider
el 15 de Dic. de 2015
It seems to me you can do that essentially as you wrote it. All you have to do is assign it to a variable:
List1 = [value1; value2; value3; .....];
What problems are you having doing that sort of vertical concatenation?
Respuestas (1)
Star Strider
el 15 de Dic. de 2015
Since ‘Y2’ has 3 columns, you can create list by first preallocating it, then saving it:
Y2_final = nan(size(VS11,1), 3); % Preallocate
for i=1:size(VS11,1)
. . . CODE . . .
[T2,Y2] = ode45(@DifLign_fase2,t2,I_2); %call of ode45 to solve the system
. . . CODE . . .
Y2_final(i,:) = Y2(end,:); % Save Last Value Of Each ‘Y2’
end
If you want to include the times as well, the code becomes:
T2Y2_final = nan(size(VS11,1), 4); % Preallocate
for i=1:size(VS11,1)
. . . CODE . . .
[T2,Y2] = ode45(@DifLign_fase2,t2,I_2); %call of ode45 to solve the system
. . . CODE . . .
T2Y2_final(i,:) = [T2(end) Y2(end,:)]; % Save Last Value Of Each ‘T2’ & ‘Y2’
end
This is UNTESTED CODE but should work.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!