Compiling output vectors from each iteration of a for-loop into a Matrix

5 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 17 Mar 2022
Edited: Stephen23 on 17 Mar 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 = 9×21
-114.9712 -115.6515 -116.3317 -117.0120 -117.6923 -118.3726 -119.0528 -119.7331 -120.4134 -121.0936 -121.7739 -122.4542 -123.1345 -123.8147 -124.4950 -125.1753 -125.8555 -126.5358 -127.2161 -127.8964 -128.5766 86.2384 82.8371 79.4357 76.0343 72.6330 69.2316 65.8303 62.4289 59.0275 55.6262 52.2248 48.8235 45.4221 42.0207 38.6194 35.2180 31.8167 28.4153 25.0139 21.6126 18.2112 -196.8126 -176.4044 -155.9962 -135.5881 -115.1799 -94.7717 -74.3636 -53.9554 -33.5473 -13.1391 7.2691 27.6772 48.0854 68.4936 88.9017 109.3099 129.7180 150.1262 170.5344 190.9425 211.3507 -168.0798 -143.5900 -119.1002 -94.6104 -70.1206 -45.6308 -21.1410 3.3488 27.8386 52.3284 76.8182 101.3080 125.7978 150.2876 174.7773 199.2671 223.7569 248.2467 272.7365 297.2263 321.7161 -225.5453 -209.2188 -192.8923 -176.5657 -160.2392 -143.9127 -127.5861 -111.2596 -94.9331 -78.6066 -62.2800 -45.9535 -29.6270 -13.3004 3.0261 19.3526 35.6792 52.0057 68.3322 84.6588 100.9853 143.7040 148.4659 153.2278 157.9897 162.7516 167.5135 172.2754 177.0373 181.7992 186.5611 191.3230 196.0849 200.8468 205.6087 210.3706 215.1325 219.8944 224.6563 229.4182 234.1802 238.9421 -5.9679 -23.6549 -41.3420 -59.0291 -76.7162 -94.4032 -112.0903 -129.7774 -147.4645 -165.1515 -182.8386 -200.5257 -218.2128 -235.8998 -253.5869 -271.2740 -288.9610 -306.6481 -324.3352 -342.0223 -359.7093 -139.3069 -126.3817 -113.4566 -100.5314 -87.6062 -74.6811 -61.7559 -48.8307 -35.9055 -22.9804 -10.0552 2.8700 15.7951 28.7203 41.6455 54.5706 67.4958 80.4210 93.3462 106.2713 119.1965 295.8867 258.4717 221.0568 183.6418 146.2268 108.8119 71.3969 33.9819 -3.4330 -40.8480 -78.2630 -115.6779 -153.0929 -190.5079 -227.9228 -265.3378 -302.7528 -340.1677 -377.5827 -414.9977 -452.4126

More Answers (0)

Categories

Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!