Error in Sliding Mode Controller Code

2 次查看(过去 30 天)
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