How to combine a matrix that has elements up to n?

1 view (last 30 days)
So, I have this pre calculate matrixes of A that are 4x4 and they are all positioned to be along the diagonal I then have matrix r that are 4x1, set up in a similar pattern but begin with 2. Now, I have set this matrix to go up to 4 calculated values but I need it to completed this pattern for a number of n=1:ntotal, where ntotal is any number. The BA matrix will always be something like (ntotal*4)X(ntotal*5) while by BR will be something like (ntotal*4)X(ntotal-1).
clear; clc;
nspan=4;
EI=[200,600,600,200];
L=[10,20,20,10];
%%constants setup
a=zeros(1,nspan);
b=a;c=a; d=a;
for n=1:nspan
a(n)=L(n);
b(n)=L(n)/EI(n);
c(n)=L(n)^2/(2*EI(n));
d(n)=L(n)^3/(6*EI(n));
end
%%Compute A to the n
for n = 1:nspan
A{n} = [1,0,0,0;a(n),1,0,0;c(n),b(n),1,0;-d(n),-c(n),-a(n),1];
end
for n = 1:nspan
r{n} = [1;a(n);c(n);-d(n)];
end
I=eye(4);
Z = zeros(4,4);
Z1 = zeros(4,1);
BA=[A{1},-I,Z,Z,Z;...
Z,A{2},-I,Z,Z;...
Z,Z,A{3},-I,Z;...
Z,Z,Z,A{4},-I];
BR=[Z1,Z1,Z1;...
-r{2},Z1,Z1;...
Z1,-r{3},Z1;...
Z1,Z1,-r{4}];
B=[BA,BR];
  3 Comments
N/A
N/A on 30 Mar 2018
Well, it runs fine but this is set up for the total of nspans set to 4, it needs to set up to automatically to account for nspans of 3, 4, 5, 6, 7, or up to 20 and more without having to change the matrix BA and BR every time. if I wanted to set up a system for 10 spans, I only would need to change the array L and EI to have 10 values within. The matrix of BR and BA are hard coded for 4 spans as in needs to account for n spans. Thanks for any help.
N/A
N/A on 30 Mar 2018
EI and L and initial conditions. Everything else is based on these values. If EI and L looked like this:
nspan=5;
EI=[200,600,600,200,200];
L=[10,20,20,10,10];
Then the matrix BA and BR would be like this:
BA=[A{1},-I,Z,Z,Z,Z;...
Z,A{2},-I,Z,Z,Z;...
Z,Z,A{3},-I,Z,Z;...
Z,Z,Z,A{4},-I,Z;...
Z,Z,Z,Z,A{5},-I];
Same for something like BR
BR=[Z1,Z1,Z1,Z1;...
-r{2},Z1,Z1,Z1;...
Z1,-r{3},Z1,Z1;...
Z1,Z1,-r{4},Z1;...
Z1,Z1,Z1,-r{5}];

Sign in to comment.

Accepted Answer

Rik
Rik on 30 Mar 2018
This code should do the trick. I also slightly modified some earlier code.
nspan=4;
EI=[200,600,600,200];
L=[10,20,20,10];
%%constants setup
L=L(:)';%force direction
EI=EI(:)';%force direction
a=L;
b=L./(n);
c=L.^2./(2*EI);
d=L.^3./(6*EI);
%%Compute A to the n
A=cell(1,nspan);r=cell(1,nspan);
for n = 1:nspan
A{n} = [1,0,0,0;a(n),1,0,0;c(n),b(n),1,0;-d(n),-c(n),-a(n),1];
r{n} = [1;a(n);c(n);-d(n)];
end
I=eye(4);
Z = zeros(4,4);
Z1 = zeros(4,1);
%pre-allocate BA as a cell to make indexing easier
BA=repmat({Z},nspan,nspan+1);
BA((nspan+1)*(1:nspan))={-I};
BA(((nspan+1)*(1:nspan))-nspan)=A;
BA=cell2mat(BA);
% BA=[A{1},-I,Z,Z,Z;...
% Z,A{2},-I,Z,Z;...
% Z,Z,A{3},-I,Z;...
% Z,Z,Z,A{4},-I];
%pre-allocate BA as a cell to make indexing easier
BR=repmat({Z1},nspan,nspan-1);
BR(2:(nspan+1):end)=cellfun(@(x) -x,r(2:end),'UniformOutput',false);
BR=cell2mat(BR);
% BR=[Z1,Z1,Z1;...
% -r{2},Z1,Z1;...
% Z1,-r{3},Z1;...
% Z1,Z1,-r{4}];
B=[BA,BR];

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!