How can I use the matlab function like fcn block

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

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]
testu = 1×6
17 3 -8 19 5 11
result = fcn(testu)
result = 79225
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
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?
hi @Paul, x1_dotdot is different.
function y = fcn(u)
m1=290;
c1=1000;
k1=13000;
y = (-1/m1)[(k1*(u1-u3))+(c1*(u2-u4)];
end
I'm modelling quarter car suspension system. Step signal is the force to move car to foward.
Normally, if i would with fcn function and scope u1(displacement of x1), it would be like this.

Sign in to comment.

 Accepted Answer

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');
Warning: An error occurred while evaluating "loc_createToolchain" in "/MATLAB/toolbox/coder/autosar/rtwTargetInfo.p": Unrecognized function or variable 'autosarroot'.
This custom registration is not loaded.
Warning: Solver is encountering difficulty in simulating model 'M_Fcn_Simulink' at time 1.0000000000000038. Simulink will continue to simulate with warnings. Please check the model for errors.
Warning: Solver was unable to reduce the step size without violating minimum step size of 3.55271E-15 for 1 consecutive times at time 1. Solver will continue simulation with the step size restricted to 3.55271E-15 and using an effective relative error tolerance of 0.0135238, which is greater than the specified relative error tolerance of 0.0001. This usually may be caused by the high stiffness of the system. Please check the system or increase the solver Number of consecutive min steps violation parameter.
plot(S.ScopeData{1}.Values.Time, S.ScopeData{1}.Values.Data, 'r-')

More Answers (4)

I think this one should be .
In this blog, there is an article on the "Quarter Car Modeling" by João Marabisa.
You can refer the blocks used in the modeling.
Also check out this tutorial:

1 Comment

I know how to do this type modelling actually. I just want to do this using matlab function also. Thank you much for anyway.

Sign in to comment.

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

It's worked kind of but i want to scope each u1,u2,u3... and it should be like this.

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Products

Asked:

on 16 Feb 2023

Answered:

on 18 Feb 2023

Community Treasure Hunt

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

Start Hunting!