# Matrix Dimensions must agree

1 view (last 30 days)
Kane Patrick Bobier on 19 Oct 2020
Hi everyone, I am having trouble getting my code to run. (Specifically line 71, inside the for loop GK). I don't get it because I use a similar code in line 51-58:
clc;
clear;
close all;
prompt = 'Specifiy Node Value';
N = input(prompt)
A1 = 0.05;
K1 = 125;
L1 = 0.6;
Q1 = 0;
A2 = 0.08;
K2 = 80;
L2 = 0.7;
Q2 = 100;
A3 = 0.7;
K3 = 100;
L3 = 0.2;
Q3 = 0;
A4 = 0.01;
K4 = 150;
L4 = 0.5;
Q4 = 200;
NS = N/4
NS = round(NS)
NES = NS-2
K1 = ((A1*K1)/L1)*[1,-1;-1,1];
K2 = ((A2*K2)/L2)*[1,-1;-1,1];
K3 = ((A3*K3)/L3)*[1,-1;-1,1];
K4 = ((A4*K4)/L4)*[1,-1;-1,1];
GK1 = zeros(NES+1,NES+1);
GK2 = zeros(NES+1,NES+1);
GK3 = zeros(NES+1,NES+1);
GK4 = zeros(NES+1,NES+1);
GK5 = zeros(N,N);
for k = 1:NES,
GK1(k:k+1,k:k+1) = (GK1(k:k+1,k:k+1)+K1);
GK2(k:k+1,k:k+1) = (GK2(k:k+1,k:k+1)+K2);
GK3(k:k+1,k:k+1) = (GK3(k:k+1,k:k+1)+K3);
GK4(k:k+1,k:k+1) = (GK4(k:k+1,k:k+1)+K4);
end
GK1
GK2
GK3
GK4
ProtoGK = {GK1,GK2,GK3,GK4};
Nu = numel(ProtoGK);
Nu
GK = zeros(1+Nu,1+Nu);
for k = 1:Nu,
GK(k:k+1,k:k+1) = (GK(k:k+1,k:k+1) + ProtoGK{k});
end
GK
##### 2 CommentsShowHide 1 older comment
Kane Patrick Bobier on 19 Oct 2020
This code is for a stiffness matrix of a rod with four elements. I just need help putting together the final global stiffness matrix.

change the varibales to vector form:
A=[0.05, 0.08, 0.7, 0.01];
E=[125, 80, 100, 150];
L=[0.6, 0.7, 0.2, 0.5];
ndof=length(A)+1;
KG = zeros(ndof, ndof);
for i = length(A)
k=A(i)*E(i)/L(i) * [1, -1; -1, 1]
dof = [i, i+1];
KG(dof, dof) = KG(dof, dof) + k;
end
##### 2 CommentsShowHide 1 older comment
Replace your code with the new one, I modified some parts,
A=[0.05, 0.08, 0.7, 0.01];
E=[125, 80, 100, 150];
L=[0.6, 0.7, 0.2, 0.5];
Q=[0, 100, 0, 200];
ndof=length(A)+1;
KG = zeros(ndof, ndof);
for i = 1:length(A)
k=A(i)*E(i)/L(i) * [1, -1; -1, 1];
dof = [i, i+1];
KG(dof, dof) = KG(dof, dof) + k;
end
KG