MATLAB Answers

1

How do I run a simulation with a transfer function block and a multi dimensional input ?

Asked by Pham Dang on 7 Jul 2011
Hello everyone,
[Context] I want to simulate a complex system many times in order to calculate the average behaviour of the system and I want to avoid the many compilations that would occur in a loop containing the sim(mdl) command. Notice that the only thing changing between each simulation of the system is the input signal values...
[Situation] Let’s take a very simple example to be clear : Create a new model with a “Sine Wave” block with the following parameters :
Sine Type : Time-based (Default)
Time (t) : Use simulation Time (Default)
Amplitude : [ 1 1 1 ]
Bias : [ 0 0 0 ]
Frequency : [1 1 1 ]*2*pi*0.22
Phase : rand(1,3) %Here is the only param that changes between simulations
Sample Time: 0
[Uncheck] Interpret vector parameters as 1-D
Add a “Transfer Fcn” block with the following parameters (for example) :
Numerator : [1]
Denominator : [1 0 1]
(Others to default value)
Connect the “Sine Wave” block to the “Transfer Fcn” block and the output of the “Transfer Fcn” to a “Scope” block. No need to change Configuration Parameters of simulation (Stop time etc...)
[Aim] Instead of running the simulation many times with one sinewave I want to exploit the possibility of generating many (=3 here) sinewaves once and run only one simulation...
[Problem] The simulation throws errors : “Error in port widths or dimensions.[...]” I know that the “Transfer Fcn” block does not feature Scalar expansion so do you have a solution to this problem or a workaround that avoids the repetitive compilations of the model ?
Thank you

  0 Comments

Sign in to comment.

6 Answers

Answer by Paulo Silva
on 7 Jul 2011
 Accepted Answer

I tested a model and found problems doing what you want, my workaround is:
  1. Insert a sine wave block (f=2*pi*0.22 and phase=rand), tf block (your num and den)
  2. Insert one mux block (three inputs) and the scope
  3. Now copy and paste the sine wave block two times
  4. Do the same for the transfer function block (paste it two times)
  5. Connect the sine waves to each transfer function block
  6. Connect the transfer functions outputs to the mux and the mux to the scope
Now with just one simulation you get three waves

  3 Comments

Thank you for your idea.
I am going to try to apply it to my system programmatically because the number of waves I have to test is around 100 (instead of 3).
You can run the simulation 100 times with a loop and save each output, in the end plot the data, it's easy to do. If you have any new question please ask like you did for this one.
I tried to do the same just with MATLAB but for some reason I can't find the results aren't the same from simulink
syms s
Numerator=[1];
Denominator=[1 0 1];
sys=tf(Numerator,Denominator)
clf
axes
hold all
t=0:0.01:10;
ex=evalc('sys')
[a b] = strread(ex, '%s %s', 'delimiter',char(10));
num=sym(char(a(2)));
den=sym(char(a(3)));
ext=ilaplace(num/den);
ext=subs(ext,'t',t);
for n=1:3
plot(t,ext.*cos(2*pi*0.22*t+rand))
end
This is a good idea too.
The difference between Simulink and your code comes from a little mistake : the operation between "ext" and "cos(.)" should be a convolution.
Replacing ".*" with a convolution, I get similar results between Simulink and Matlab but they are not identical (though phase values are the same for both test)... I think this difference is out of scope of this topic.
But in the end, my original system is too complex to be written as a transfer function. So I can't have the "magic" ext vector.
The idea of Doug seems to be more appropiate to my special case.
Thank you very much for your help
Best regards

Sign in to comment.


Answer by Doug Eastman on 7 Jul 2011

Another option is to discretize the transfer function because a discrete transfer function does allow a vector input signal.
If you have the Control System Toolbox, you can automatically calculate the discrete form of the transfer function using C2D.

  1 Comment

Hi Doug, great idea !
It's the technique that provides the fastest simulations ! and above all, outputs are exactly the same as the reference continuous time testbench. Moreover, I can keep the original complex structure and I can benefit from scalar expansion.
Eventually, your answer is THE answer to my problem.
Thank you !
BR

Sign in to comment.


Answer by Arnaud Miege
on 7 Jul 2011

The transfer function block only supports scalar input signals, hence the error, see the documentation:
"The block can model single-input single-output (SISO) and single-input multiple output (SIMO) systems."
As Paulo suggests, make multiple copies of the block.
Arnaud

  0 Comments

Sign in to comment.


Answer by Pham Dang on 17 Sep 2011

Hi all, I finally found another workaround to speed up time compilation problem and time simulation : I use the Real Time Workshop with the "rsim" target. Using this technique, I only have to compile the model once, moreover the generated exectuable is very fast !
More information in Matlab
doc rtwdemo_rsim_batch_script
or
open rsimdemo1
I hope this may help others

  0 Comments

Sign in to comment.


Answer by Guy Rouleau
on 20 Sep 2011

Hi Pham, In R2011b, place the transfer function inside the For Each subsystem.
The For Each subsystem now support continuous time, so it can vectorize any algorithm very easily.

  0 Comments

Sign in to comment.


Answer by K E
on 3 Jan 2013

Would it be possible to make one input time series which has all the desired sine waves concatenated into 1 series, and then just run 1 long simulation? You would need to know the start/end of each sine wave in order to extract each segment in postprocessing. Just mentioning in case that makes implementation easier.

  0 Comments

Sign in to comment.