i keep receiving this error message in matlab
3 次查看(过去 30 天)
显示 更早的评论
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]
p_e=[sqrt2i/2;i/2]*[Fx13,Fy13]
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
% Solve for displacements using KU=P equation
U_A=K\P
3 个评论
回答(1 个)
Ishaan Mehta
2023-12-12
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.
Debug MATLAB Code Files: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
Hope this helps,
Ishaan
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structural Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!