Compiling output vectors from each iteration of a for-loop into a Matrix
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ryan Jimenez
el 17 de Mzo. de 2022
Comentada: Ryan Jimenez
el 19 de Mzo. de 2022
Explanation
Simply put, I'm looking to create a matrix from a bunch of output vectors from a for-loop.
I have a simple for-loop that takes a coefficient matrix, A, that I got from a problem by solving equilibrium equations of trusses(essentially a given), a vector B that is composed of a couple values that are witten in terms of an external variable, P, and solves the equation Ax=B for x a certain amount of times. In my problem, the x vector is F, for forces.
I'm looking to use this for-loop, where the condition statement of the loop defines a value for the variable, P, in the B matrix to solve F in the equation AF = B to get multiple output vectors and then compile all those vectors into a matrix. I need to get 20 F-vectors for the 20 P variables I'm supposed to use, i.e. P = -1000:100:1000. I want to store all of the resulting column vectors I get from each iteration of the for-loop into a larger matrix where each column are the individual vectors.
My Code So Far
The code I have right now uses the for-loop to solve the AF=B equation for the changing P values to get all 20 of the different force vectors, F, in my problem. I don't know how to then store all of those vectors into a matrix, so if you could help me with that part, that would be extremely appreciated. And if there's a different way, like changing the condition statement for the for-loop that makes doing this easier, I dont mind doing that too.
The code I have so far is attached below,
% New P and reaction force values with P2 varying
P1_c = 1000;
P2_c = -1000 : 100 : 1000;
Fay_c = (P2_c + 2*P1_c)/3;
Fdy_c = (P1_c + 2*P2_c)/3;
for P2_c = -1000 : 100 : 1000
A_c = [ 1 0 0 0 0 c45 0 0 0
0 0 0 0 0 s45 0 0 0
1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0
0 1 1 0 0 0 0 -c45 0
0 0 0 0 0 0 0 s45 1
0 0 1 -c45 0 0 0 0 0
0 0 0 s45 0 0 0 0 0
0 0 0 -c45 1 0 0 0 0
0 0 0 s45 0 0 0 0 1
0 0 0 0 1 c45 0 -c45 0
0 0 0 0 0 s45 1 s45 0 ];
B_c = [ 0
(P2_c + 2*P1_c)/3
0
-P1
0
-P2
0
(P1_c + 2*P2_c)/3
0
0
0
0 ];
F_1c = A_c\B_c
% part where I somehow store F_1c into matrix here
end
0 comentarios
Respuesta aceptada
Stephen23
el 17 de Mzo. de 2022
Editada: Stephen23
el 17 de Mzo. de 2022
% New P and reaction force values with P2 varying
P1_c = 1000;
P2_c = -1000 : 100 : 1000;
Fay_c = (P2_c + 2*P1_c)/3;
Fdy_c = (P1_c + 2*P2_c)/3;
% Values you did not provide:
c45 = 1;
s45 = 2;
P1 = pi;
P2 = 23;
N = numel(P2_c);
C = cell(1,N);
for k = 1:N
A_c = [ 1 0 0 0 0 c45 0 0 0
0 0 0 0 0 s45 0 0 0
1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0
0 1 1 0 0 0 0 -c45 0
0 0 0 0 0 0 0 s45 1
0 0 1 -c45 0 0 0 0 0
0 0 0 s45 0 0 0 0 0
0 0 0 -c45 1 0 0 0 0
0 0 0 s45 0 0 0 0 1
0 0 0 0 1 c45 0 -c45 0
0 0 0 0 0 s45 1 s45 0 ];
B_c = [ 0
(P2_c(k) + 2*P1_c)/3
0
-P1
0
-P2
0
(P1_c + 2*P2_c(k))/3
0
0
0
0 ];
F_1c = A_c\B_c;
% store F_1c into matrix here:
C{k} = F_1c;
end
M = [C{:}] % comma-separated list
Más respuestas (0)
Ver también
Categorías
Más información sobre Quadratic Programming and Cone Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!