Error in Sliding Mode Controller Code

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'.

 Accepted Answer

Jack
Jack on 8 Mar 2025

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 Comments

Dynamo
Dynamo on 8 Mar 2025
Edited: Dynamo on 8 Mar 2025
Thanks for your comment. But I am not sure about what I am doing wrong here. I checked all dimensions of each vector or matrix I am calling in my function.
I tested both my simulink functions independently but when I connect them with each other it throws error. And I am not sure why this error is showing.

It sounds like the error may be occurring due to a hidden dimension mismatch when the blocks are connected, or possibly because the output dimensions aren’t being fully defined in all execution paths within your MATLAB Function block. Even if each function works independently, when you connect them the signal dimensions might not be properly propagated. Here are a few suggestions to help troubleshoot and resolve the issue: 1. Verify that all inputs coming into the MATLAB Function block have explicit dimension and data type definitions. You can set these in the block’s “Ports and Data Manager” so that Simulink knows exactly what sizes to expect. 2. Inside your MATLAB Function block, ensure that every code path assigns a value to the output variable with consistent dimensions. You might want to initialize your output at the beginning of the function to a preallocated array with the desired size. For example:

function output = yourFunction(C, f) % Preallocate output to a specific size (e.g., if output should be 3x1) output = zeros(3,1); % Then, perform the computation output = C * f; % Ensure that the dimensions of C and f match appropriately end

3. Double-check that the matrices C and f indeed have compatible dimensions at runtime. Sometimes, when integrating different blocks, the data type or dimension inference might change. You can add temporary assertions or use the disp() function (or coder.internal.assert if code generation is involved) to log their sizes. 4. If one of the matrices (say, f) is generated dynamically or passed through multiple blocks, consider using the Simulink Diagnostic Viewer to inspect the actual sizes during simulation.

By ensuring that the inputs have well-defined dimensions and that the output is consistently assigned, you should be able to resolve the error.

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

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
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

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Asked:

on 8 Mar 2025

Commented:

on 8 Mar 2025

Community Treasure Hunt

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

Start Hunting!