Friday, February 3, 2023
HomeMatlabOperate Handles and Submit-Simulation Features » Man on Simulink

Operate Handles and Submit-Simulation Features » Man on Simulink


Till not too long ago, I all the time had bother when coping with MATLAB operate handles. I can not actually clarify why, there was simply one thing I couldn’t wrap my head round.

To assist a person, I lastly took the time to demystify that in my head and determined to share what I realized with you right this moment.

In Simulink, the use instances requiring operate handles that I run essentially the most regularly into are:

In right this moment’s submit, I’ll use a post-simulation callback for sim for example.

The Instance

For this instance, I’ll use this easy mannequin the place I output indicators x and v utilizing root-level Outport blocks:

mdl = ‘massSpringDamper’;

in = Simulink.SimulationInput(mdl);

out.yout

A Easy Submit Simulation Callback

In its easiest kind, configuring a post-simulation callback is simple. Here’s a screenshot from the documentation:

The enter func is a deal with to a operate that receives the unique SimulationOutput object as enter, and returns a construction with the fields to be included in a brand new SimulationOutput object. For instance, I outline this operate:

operate newout = myPostSim(out)

mySignal = out.yout.get(‘x’).Values;

newout.imply = imply(mySignal);

newout.max = max(mySignal);

newout.min = min(mySignal);

finish

Then to specify that this operate must execute after the simulation, I cross to setPostSimFcn a deal with to myPostSim utilizing the “@” character:

mdl = ‘massSpringDamper’;

in = Simulink.SimulationInput(mdl);

in = in.setPostSimFcn(@myPostSim);

out = sim(in)

One factor to notice, the SimulationOutput object returned on this case doesn’t include the unique sign yout, it solely accommodates the brand new fields created within the post-simulation operate, together with the SimulationMedata and the ErrorMessage fields. That is particularly helpful in case your simulation is producing quite a lot of information, however you’re solely all in favour of post-processed statistics, like on this case the imply, min and max.

If you wish to hold the unique information, you possibly can merely re-assign it to the output. For the above instance, this appears like:

operate newout = myPostSim2(out)

newOut = out;

mySignal = out.yout.get(‘x’).Values;

newout.imply = imply(mySignal);

newout.max = max(mySignal);

newout.min = min(mySignal);

finish

When simulating, the SimulationOutput object accommodates the unique yout and the newly computed values

mdl = ‘massSpringDamper’;

in = Simulink.SimulationInput(mdl);

in = in.setPostSimFcn(@myPostSim2);

out = sim(in)

Further Enter Arguments

When scrolling via the documentation for setPostSimFcn, we additionally see this syntax:

This syntax is known as an nameless operate. To study extra on these, I like to recommend going via this documentation web page. This syntax means that you can cross inputs to the post-simulation operate. For instance, let’s add two inputs to our earlier operate:

operate newout = myPostSim3(out,bias,acquire)

mySignal = out.yout.get(‘x’).Values;

newout.meanWithBias = acquire* (imply(mySignal) + bias);

finish

With these further inputs, you first create an nameless operate that may obtain the Simulation Output object out as enter, and cross it to myPostSim3 together with values for bias and acquire:

mdl = ‘massSpringDamper’;

myAnonymousFcn = @(out) myPostSim3(out,biasValue,gainValue);

in = Simulink.SimulationInput(mdl);

in = in.setPostSimFcn(myAnonymousFcn);

out = sim(in)

Functionally, this one line of code defining myAnonymousFcn is equal to create a wrapper operate round myPostSim3 that might appear to be this:

operate newout = myNotAnonymousFcn(out)

biasValue = 5;

gainValue = 10;

newout = myPostSim3(out,biasValue,gainValue)

finish

Right here is a further picture that I hope will assist differentiate the argument for which the worth will probably be handed to the operate when will probably be known as on the finish of the simulation (Specified utilizing @(argumentName) in entrance of the operate signature), versus the arguments for which the worth is handed instantly:

Specifying a technique of an App or MATLAB Class as Callback Operate

For example this, I added two variables x0 and v0 specifying the preliminary place and velocity of the Second Order Integrator block.
To maintain issues easy, I can’t use an App Designer app, however a easy class simulating the mannequin a number of occasions with completely different preliminary values. The identical precept would apply to an App developed in App Designer.

Here’s what my easy class does:

  • Within the constructor, I retailer the title of the mannequin to be simulated and use rng to initialize the random quantity generator
  • Within the runSim technique, I outline a random worth x0 that will probably be used as the identical preliminary place for all simulations
  • I outline completely different random values v0 for use in every simulation
  • I outline the category technique thePostSimFcn as post-simulation callback and I cross to it the values of x0 and v0 utilizing further arguments
  • Within the thePostSimFcn, I retailer x0 and v0 within the simulation output object for future use

classdef myClass

properties

mdl % the title of the mannequin to simulate

finish

 

strategies

operate obj = myClass(mdl)

rng(‘shuffle’);

obj.mdl = mdl;

finish

 

operate out = runSim(obj,N)

x0 = rand; % Initialize all runs with identical place

in(1:N) = Simulink.SimulationInput(obj.mdl);

for i = 1:N

v0 = 10*rand; % Initialize runs with completely different velcity

in(i) = in(1).setVariable(‘x0’,x0);

in(i) = in(1).setVariable(‘v0’,v0);

in(i) = in(i).setPostSimFcn(@(out) obj.thePostSimFcn(out,x0,v0));

finish

out = sim(in,‘ShowProgress’,‘off’);

finish

 

operate newOut = thePostSimFcn(obj,out,x0,v0)

newOut = out;

% Retailer the ICs within the output for the document

newOut.x0 = x0;

newOut.v0 = v0;

finish

finish

finish

Issues to notice listed below are:

  • The primary argument of the post-simulation operate thePostSimFcn is obj, the article itself.
  • When configuring the post-simulation callback, I discuss with it as obj.thePostSimFcn as a result of it’s a technique of this class.

I can then use my easy class with this code:

myObj = myClass(‘massSpringDamper’);

plot(out(i).yout.get(‘x’).Values);

plot(out(i).yout.get(‘v’).Values);

legendTxt{i} = [‘Run ‘ num2str(i) ‘ – x0=’ num2str(out(i).x0) ‘ – v0=’ num2str(out(i).v0)];

subplot(2,1,1); legend(legendTxt)

Now it is your flip

I hope these examples are helpful for you. If you find yourself utilizing post-simulation capabilities, I additionally suggest visiting this earlier submit the place I present tips to deal with errors correctly.

Do you will have different use instances for operate handles and nameless capabilities in a Simulink context? Tell us within the feedback beneath.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments