How can I use the matlab function like fcn block

66 次查看(过去 30 天)
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 个评论
titor7
titor7 2023-2-17
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
titor7
titor7 2023-2-17
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.

请先登录,再进行评论。

采纳的回答

Sulaymon Eshkabilov
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-')

更多回答(4 个)

Sam Chak
Sam Chak 2023-2-17
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 个评论
titor7
titor7 2023-2-17
I know how to do this type modelling actually. I just want to do this using matlab function also. Thank you much for anyway.

请先登录,再进行评论。


Paul
Paul 2023-2-18
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
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

titor7
titor7 2023-2-17
编辑:titor7 2023-2-17
thank you for your helps.

类别

Help CenterFile Exchange 中查找有关 Interactive Model Editing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by