Error in Sliding Mode Controller Code

조회 수: 5 (최근 30일)
Dynamo
Dynamo 2025년 3월 8일
댓글: Dynamo 2025년 3월 8일
I am facing error in my matlab simulink mode for sliding mode controller using reaching law and boundary layer function to reduce chattering.
I am not sure what I am doing wrong. I crossed checked my equations and implementations but they all look right to me. My simulink file is attached in the post. it has all functions necessary to run the model independently.
The error is:
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.
Function 'MATLAB Function' (#32.427.430), line 21, column 30:
"C*f"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Block ''clutch_SlidingModeControl/MATLAB Function'' does not fully set the dimensions of output 'output'.

채택된 답변

Jack
Jack 2025년 3월 8일

The error arises because the dimensions of C and f are not compatible for matrix multiplication. In MATLAB, when you use the * operator, the number of columns in C must equal the number of rows in f. If you intended to multiply the elements individually, you should use elementwise multiplication (.*) instead.

Double-check the sizes of your matrices inside the MATLAB Function block—ensure that C and f are defined with matching dimensions if you really want to use matrix multiplication. Also, note that the warning about the output not being fully set means you should explicitly assign the dimensions of your output variable, possibly by preallocating it or ensuring every code path sets it.

Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.

  댓글 수: 4
Sam Chak
Sam Chak 2025년 3월 8일
Hi @Jack, from a control expert's perspective, if there is no dimensional error, have you thoroughly verified whether the sliding mode control law has been correctly implemented?
function output = Sliding_Mode(e, edot, eta, K, C, phi, A, B, x, u)
% Sliding Surface
s = C*e;
% Boundary Layer Function to reduce chattering
if (abs(s) > phi)
theta = 1;
elseif (abs(s) <= phi)
theta = 1/phi;
else
theta = -1;
end
% Exponential Reaching Law
h = - eta*theta - K*s;
g = B*u;
f = A*x;
% Control Output
output = (C*g)\(C*edot - C*f + h);
end
Dynamo
Dynamo 2025년 3월 8일
Hello @Sam Chak and @Jack,
I solved this problem by using MATLAB R2024b version which has in-build Sliding Mode Controller with Reaching Law in Simulink.
Using that I figured my matrix mismatch issue as well and it works now. Thank you for your help.
Thanks
Junaid

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Test Execution에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by