i keep receiving this error message in matlab

4 views (last 30 days)
I keep receiving this error message in matlab. im trying to write a script for a plane trust problem for school dont know what im doing wrong at this point. any help is welcome. error message "Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns
in the first matrix matches the number of rows in the second matrix. To operate on
each element of the matrix individually, use TIMES (.*) for elementwise
multiplication.
Error in untitled17 (line 78)
k_Aeg=T_x'*k_ee*T_x+T_y'*k_ee*T_y;
% Constants
i = 1; % length of horizontal and vertical members
sqrt2i = sqrt(2) * i; % length of inclined members
E = 100e9; % Young's modulus (Pa)
A = 1e-4; % cross-sectional area (m^2)
i = 0.3; % moment of inertia (m)
% Define external loads
Fx13 = 10000;
Fx14 = 10000;
Fy13 = 0;
Fy14 = 0;
% Load Case A: Fx13 = Fx14 = 10,000 N
FxA = [10000; 0];
FyA = [0; -10000];
% Load Case B: Fy13 = Fy14 = 10,000 N
FxB = [0; -10000];
FyB= [-10000; 0];
% Load Case C: Fx13=10,000 N and Fx14=-10,000 N
FxC= [10000;-10000];
FyC= [-20000;-20000];
% Assemble global stiffness matrix K and global force vector P for each load case
% Initialize matrices/vectors with zeros
K_A=zeros(6);
P_A=zeros(6,1);
K_B=zeros(6);
P_B=zeros(6,1);
K_C=zeros(6);
P_C=zeros(6,1);
% Define local element stiffness matrix k_e and local force vector p_e
k_e=A*E/i*[1 -1;-1 1]
k_e = 2×2
1.0e+07 * 3.3333 -3.3333 -3.3333 3.3333
p_e=[sqrt2i/2;i/2]*[Fx13,Fy13]
p_e = 2×2
1.0e+03 * 7.0711 0 1.5000 0
for e=[3] %(number of elements)
k_ee=k_e;
p_ee=p_e;
if(e==3)%horizontal member
T_x =[1 0 0 -1];
T_y=[0 1 -1 0];
end
if(e==4)%vertical member
T_x =[0 -1 1 0];
T_y=[-1/2 sqrt2i/2 sqrt2i/2 -1/2];
end
% Compute global element stiffness matrix K_e and global element force vector P_e
K_Ae = zeros(6);
p_Ae = zeros(6, 1);
K_Be = zeros(6);
p_Be = zeros(6, 1);
K_Ce = zeros(6);
p_Ce = zeros(6, 1);
size(T_x')
size(k_ee)
size(T_x)
size(T_y')
size(k_ee)
size(T_y)
% Transform local stiffness matrix to global coordinates
k_Aeg=T_x'*k_ee*T_x+T_y'*k_ee*T_y;
p_Aeg=T_x'*p_ee;
% Add element contributions to the global stiffness matrix and force vector
K_A([3:4],[3:4])=K([3:4],[3:4])+k_Aeg;
P_A([3:4])=P([3:4])+p_Peg;
end
ans = 1×2
4 1
ans = 1×2
2 2
ans = 1×2
1 4
ans = 1×2
4 1
ans = 1×2
2 2
ans = 1×2
1 4
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
% Solve for displacements using KU=P equation
U_A=K\P
  3 Comments
Hector Meza
Hector Meza on 9 Dec 2023
im not the best are coding. im kinding strruggling her. so how can i fix the problem?
Torsten
Torsten on 9 Dec 2023
Edited: Torsten on 9 Dec 2023
It doesn't have to do with coding. You must know what you want to compute in the two expressions and why the arrays you provide are not suitable for this. I cannot help you in this respect.

Sign in to comment.

Answers (1)

Ishaan Mehta
Ishaan Mehta on 12 Dec 2023
Hi Hector,
I understand that you are facing an error regarding incompatible dimensions of matrices while performing matrix multiplication.
When two 2-dimensional matrices are multiplied, the number of columns in the first matrix must be equal to the number of rows in the second matrix. Therefore, making sure that the matrix dimensions conform to this rule will help in resolving this error.
The following line in your code is the one producing the error:
k_Aeg=T_x'*k_ee*T_x+T_y'*k_ee*T_y;
Upon using the debugger with a breakpoint on the mentioned line, I found that "T_x' " has 4 rows and 1 column, while "k_ee" has 2 rows and 2 columns, therefore, "T_x' " and "k_ee" matrices can not be multiplied. Please verify that the matrix dimensions are compatible and reshape the matrices into appropriate dimensions using the "reshape" function, if needed.
The following MathWorks documentation page contains more information about the usage of the "reshape" function in MATLAB:
In addition, using a debugger will be helpful in investigating such errors and analyzing the size and values of different variables while executing programs in MATLAB, as explained in the below documentation page.
Hope this helps,
Ishaan

Products

Community Treasure Hunt

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

Start Hunting!