MATLAB Answers

Use Mfile's user defined function in the Simulink's Matlab Function Block.

3 views (last 30 days)
Josh Ueda
Josh Ueda on 14 Apr 2020
Answered: Mark McBroom on 22 Apr 2020
Hello,
I have created the function called "RhosatLiq" in M File "RhoSatLiq.m" like following.
I want to use this function in the Simulink's Matlab Function Block, so I write the code inside this function block, but this gives me error message.
Is there any way I can do this?
I saved these Mfile = "RhoSatLiq.m" and simulink model "Cycle.slx" in the same folder. See attached pictures for simulink model and file saved location.
Code in M script, "File Name = RhoSatLiq.m"
function RhosatLiq = fcn(Tsat)
%Calculate the Saturation Liquid Density
%Input: Saturation Temperature [C]
%Output: Sat Liq Density [kg/m3]
if Tsat < 0
a50 = -1.33757324173412
a51 = 6.27523526615539E-03
a52 = 1.55039063531916E-03
a53 = 1.5170417999686E-04
a54 = 9.20121648717646E-06
a55 = 3.51106181076951E-07
a56 = 8.15745534494223E-09
a57 = 1.05066118901585E-10
a58 = 5.73695819656322E-13
a59 = 528.74969814242
RhosatLiq = a50 * Tsat ^ 1 + a51 * Tsat ^ 2 + a52 * Tsat ^ 3 ...
+ a53 * Tsat ^ 4 + a54 * Tsat ^ 5 + a55 * Tsat ^ 6 + a56 * Tsat ^ 7 + a57 * Tsat ^ 8 + a58 * Tsat ^ 9 + a59
else Tsat >= 0
b50 = -1.36642983072481
b51 = -3.32243599941902E-03
b52 = -6.404596153815E-05
b53 = 4.6414232001962E-06
b54 = -2.2416417011433E-07
b55 = 5.77062047904382E-09
b56 = -8.49071019214081E-11
b57 = 6.63742469936704E-13
b58 = -2.18292931512865E-15
b59 = 528.712032903846
RhosatLiq = b50 * Tsat ^ 1 + b51 * Tsat ^ 2 + b52 * Tsat ^ 3 ...
+ b53 * Tsat ^ 4 + b54 * Tsat ^ 5 + b55 * Tsat ^ 6 + b56 * Tsat ^ 7 + b57 * Tsat ^ 8 + b58 * Tsat ^ 9 + b59
end
Simulink Block's Matlab Function Block's Code
function Comp = fcn(Tevp)
Comp = RhosatLiq(Tevp)
end

  0 Comments

Sign in to comment.

Answers (1)

Mark McBroom
Mark McBroom on 22 Apr 2020
Your function isn't named RhosatLiq, it is named fcn. Change it to be :
function RSL = RhosatLiq(Tsat)
%Calculate the Saturation Liquid Density
%Input: Saturation Temperature [C]
%Output: Sat Liq Density [kg/m3]
if Tsat < 0
a50 = -1.33757324173412
a51 = 6.27523526615539E-03
a52 = 1.55039063531916E-03
a53 = 1.5170417999686E-04
a54 = 9.20121648717646E-06
a55 = 3.51106181076951E-07
a56 = 8.15745534494223E-09
a57 = 1.05066118901585E-10
a58 = 5.73695819656322E-13
a59 = 528.74969814242
RSL = a50 * Tsat ^ 1 + a51 * Tsat ^ 2 + a52 * Tsat ^ 3 ...
+ a53 * Tsat ^ 4 + a54 * Tsat ^ 5 + a55 * Tsat ^ 6 + a56 * Tsat ^ 7 + a57 * Tsat ^ 8 + a58 * Tsat ^ 9 + a59
else Tsat >= 0
b50 = -1.36642983072481
b51 = -3.32243599941902E-03
b52 = -6.404596153815E-05
b53 = 4.6414232001962E-06
b54 = -2.2416417011433E-07
b55 = 5.77062047904382E-09
b56 = -8.49071019214081E-11
b57 = 6.63742469936704E-13
b58 = -2.18292931512865E-15
b59 = 528.712032903846
RSL = b50 * Tsat ^ 1 + b51 * Tsat ^ 2 + b52 * Tsat ^ 3 ...
+ b53 * Tsat ^ 4 + b54 * Tsat ^ 5 + b55 * Tsat ^ 6 + b56 * Tsat ^ 7 + b57 * Tsat ^ 8 + b58 * Tsat ^ 9 + b59
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by