How can I use the matlab function like fcn block
Show older comments
Hello guys,
Ex. when i write basic spring-damper equation, i got error. In fcn block, it would be written like (-1/m2)*(k1*(u(3)-u(1))+k2*(u(3)-u(5))+c1*(u(4)-u(2))+c2*(u(4)-u(6))). How can I use the matlab function like fcn block?
function y = fcn(u)
m2=40;
c1=1000;
c2=0;
k1=13000;
k2=220000;
y =(-1/m2)*(k1*(u(3)-u(1))+k2*(u(3)-u(5))+c1*(u(4)-u(2))+c2*(u(4)-u(6)));
end

5 Comments
Paul
on 16 Feb 2023
Hi daglar7,
What was the error? Can you copy/paste it here?
Also, it looks like you're trying to use the exact same equations for x1_dotdot and x2_dotdot. Is that correct?
What error are you encountering when you use that code as MATLAB Function Block ?
Or are you saying that it works for you in a MATLAB Function Block but not when you are at the MATLAB level?
testu = [17 3 -8 19 5 11]
result = fcn(testu)
function y = fcn(u)
m2=40;
c1=1000;
c2=0;
k1=13000;
k2=220000;
y =(-1/m2)*(k1*(u(3)-u(1))+k2*(u(3)-u(5))+c1*(u(4)-u(2))+c2*(u(4)-u(6)));
end
Sam Chak
on 17 Feb 2023
Hi @daglar7
From this equation
y = (- 1/m2)*(k1*(u(3) - u(1)) + k2*(u(3) - u(5)) + c1*(u(4) - u(2)) + c2*(u(4) - u(6)));
I noticed that the step function signals injected in these two terms:
Just wondering... What is the context for your mass-spring-damper system such that the step signals can be injected like that?
titor7
on 17 Feb 2023
titor7
on 17 Feb 2023
Accepted Answer
More Answers (4)
Paul
on 18 Feb 2023
Hi titor7,
The reason for the error in the original model show in this comment is because Simulink didn't have enough information to determine the dimensions of y outputs of the Matlab Function blocks.
This problem can be addressed in one of two ways. The "clean" way is to double click on the Matlab Function block, go to the function tab and click "Edit Data." On the Symbols pane click on 'y' and in the Property Inspector change the Size from - 1, which means inherited, to 1. Do the same for the other Matlab Function block.
The quick way is to add a line of code to the Matlab Function block to force the parser to recognize that y is a scalar. Like this
function y = fcn(u)
y = 0; % define y as a scalar
m2=40;
c1=1000;
c2=0;
k1=13000;
k2=220000;
y =(-1/m2)*(k1*(u(3)-u(1))+k2*(u(3)-u(5))+c1*(u(4)-u(2))+c2*(u(4)-u(6)));
end
function y = fcn(u)
y = 0; % define y as a scalar
m1=290;
c1=1000;
k1=13000;
%y = (-1/m1)[(k1*(u1-u3))+(c1*(u2-u4)];
y = (-1/m1)*(k1*(u(1)-u(3)))+(c1*(u(2)-u(4)));
end
You'll still have to deal how to approximate the derivative of the step function, but at least now the model will update and run.
Sulaymon Eshkabilov
on 17 Feb 2023
MATLAB Fcn block can be embedded - see attached Simulink model of your exercises with some modifications. It works perfectly fine. There is one importnat point is that has been overlooked in the MATLAB fcn syntax - see the following syntax of the MATLAB fcn block:
function y = fcn(u1, u2, u3, u4, u5, u6)
m2=40;
c1=1000;
c2=0;
k1=13000;
k2=220000;
y =(-1/m2)*(k1*(u3-u1)+k2*(u3-u5)+c1*(u4-u2)+c2*(u4-u6));
end
1 Comment
Sulaymon Eshkabilov
on 17 Feb 2023
Edited: Sulaymon Eshkabilov
on 17 Feb 2023
Welcome!
Categories
Find more on General Applications in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




