简体   繁体   中英

How to get a transfer function from a Simulink model into Matlab

I would like to plot a transfer function from a Simulnik file model.mdl where I used blocks From Workspace (simin) and To Workspace (simou) to start a simulation from Matlab.

And now comes the problem. I simply can't find a way to run this Simulink model in Matlab.

Here is my code;

sim('model');  


h=simout/simin;

[H,omega]=freqz(h);
%plot

I get this errors;

Error evaluating parameter 'VariableName' in 'model/From
Workspace'

Caused by:
    Error using sigma (line 8)
    Undefined function or variable 'simin'.

What am I doing wrong?

This is my model:在此处输入图像描述

Thank you so much.

Do you actually know the transfer function? Is implemented correctly in Simulink? Then the error suggests that you haven't assigned the correct variable name to your from workspace block, you just kept the default VariableName . For the frequency response you need to define your transfer function outside Simulink with tf , then use bodeplot to plot it. (Look at the end of these answer)


But it appears to me, that you have some inputdata and some outputdata and you'd like to estimate the transfer function and finally get the frequency response of that transfer function. There is no need for Simulink to do that. Once you found your transfer function you could implement it into Simulink using the Transfer function block, feed the simulation with the From Workspace Block and display the results with Scope . But first you need the transfer function.

Assuming you have the variables inputdata and outputdata you first need to create a transfer function dataset:

% prepare data for tftest, 100 is a random chosen sampling time
tfdata = iddata(data(:,1),data(:,2),100);

then you can use tfest to estimate the transfer function with a chosen number of poles:

N = 5;     % Number of poles
sys = tfest(tfdata,N);

The frequency response you get eg with bodeplot :

bodeplot(sys)

The function FREQZ you intended to use is just for digital filters, not for transfer functions.

Finally you can test your model with Simulink:

在此处输入图片说明

where the numerator of the Transfer Fcn Block is sys.num{1} and the denominator sys.den{1} (for my example).

If the graph displayed in Scope (be aware that you should disable "Limit Data points to last 5000" in the scope Settings) is similar to your outputdata the estimation was successful.


I made a testrun for your model: (fixed step solver, no continuous states, 0.0001 steptime).

在此处输入图片说明

Then the following code:

tfdata = iddata(inputdata,outputdata,0.0001);
N = 5;
sys = tfest(tfdata,N);
bodeplot(sys)

returning:

sys =

  From input "u1" to output "y1":

  -2068 s^4 + 2.89e06 s^3 + 7.017e10 s^2 + 5.205e13 s - 8.931e15

  -------------------------------------------------------

  s^5 + 1.034e04 s^4 + 4.552e07 s^3 + 1.114e11 s^2  + 8.337e13 s + 8.931e15

and:

在此处输入图片说明

or convert it to discrete:

sysd = c2d(sys,0.0001)

sysd =

  From input "u1" to output "y1":

  -0.0995 z^-1 + 0.4644 z^-2 - 0.7491 z^-3 + 0.5061 z^-4 - 0.1219 z^-5  

  -------------------------------------------------------

  1 - 4.042 z^-1 + 6.554 z^-2 - 5.332 z^-3 + 2.176 z^-4  - 0.3556 z^-5 

I can't help you more, and it's a homework anyway, right? So the rest is up to you. And honestly, calculate it by hand! It will be much more exact!

Well, the error message should be self-explanatory: you haven't defined your variable simin in the base workspace. Define it (as a function of time) and then your model will run.

BTW, a much better way to obtain the transfer function from your Simulink model is use dlinmod , as other people have already pointed out, or linearize if you have Simulink Control Design. Your approach is prone to noise and not very robust. It also relies on your input having all the frequency components of interest, which is unlikely. Simulink Control Design also provides frestimate to estimate the frequency response function of your model.

看一下dlinmod :提取工作点周围的离散时间线性状态空间模型(来自Simulink模型)。

The following code helps me:

[A B C D]=linmod2('FileNameWithoutExtension');
tf_you_want_to_get=tf(minreal(ss(A,B,C,D)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM