example level 2 matlab code for an Euler integrator s-function

5 views (last 30 days)
Hi all.
I'm trying to understand how a "level 2 Matlab s-function" works, and so I want to try the following simple example.
I would like to construct an example of the Matlab code behind a "level 2 Matlab s-function" designed to integrate using Euler's method. The resulting block should behave pretty much just like the standard integrator (1/s) block in the simulink library, except without any bells and whistles (e.g. no saturation limits or zero crossing detection---I want to keep it as simple as possible), and also that it will always use Euler with fixed step size instead of the global simulink solver setting.
I need it to take one input du/dt and spit back one output u=u(t). It should have at least two parameters u_0 and dt, where u_0 is the value of u at the simulation start time, and dt is the fixed step size. I'm not sure if it needs other parameters also.
Here's what I've got so far:
function dvolume(block)
setup(block);
function setup(block)
% Register the number of ports.
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
% Set up the port properties to be inherited or dynamic.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override the input port properties.
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
% Override the output port properties.
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
% Register the parameters.
block.NumDialogPrms = 1;
block.DialogPrmsTunable = { 'u_0', 'dt' };
% Register the sample times.
block.SampleTimes = [dt offset];
% Specify if Accelerator should use TLC or call back to the
% MATLAB file
block.SetAccelRunOnTLC(false);
% Specify the block simStateCompliance.
block.SimStateCompliance = 'DefaultSimState';
%%Register methods
block.RegBlockMethod('euler', @euler);
function euler(block)
block.OutputPort(1).Data = u + dt * block.InputPort(1).Data;
yeah, so as you can see, this thing can't possibly run because it doesn't know what to do about u. I'm sure there are other problems too. This leads me to some questions.
(*) The symbol 'u' is supposed to denote the computed value of u(t) at the beginning of the time step, so that
block.OutputPort(1).Data
gives me the computed value of u(t+dt). How do I write that in code?
() Is the line
block.SampleTimes = [dt offset];
correct?
(*) How do I tell it to use u_0 as the initial value of u ?
(**) What other mistakes have I made?
Any help would be much appreciated. Thanks in advance guys!

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!