How can I use the matlab function like fcn block
26 views (last 30 days)
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

Accepted Answer
Sulaymon Eshkabilov
on 17 Feb 2023
There was one missing block from the step input signal that is a derivative block. Note that using the derivative block is not recommended to use that might lead to unstable results.
Another important point is that the damping coefficient c2 =0, and if c2 =c1, then the system response will become damped otherwise it vibrates for a quite long time. You should adjust stiffness and adamping coefficient values in both MATLAB Fcn blocks.
The relative tolerance was also tightened (1e-4) in the model settings.
See the attached model with two corrections.
All the best.
Here are the simulation results:
S = sim('M_Fcn_Simulink.slx');
plot(S.ScopeData{1}.Values.Time, S.ScopeData{1}.Values.Data, 'r-')
0 Comments
More Answers (4)
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
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.
0 Comments
See Also
Categories
Find more on Event Functions 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!