# Guy and Seth on Simulink

## Creating Your Own Block With Continuous States (MATLAB S-Function)

In a previous post, I showed an example of a MATLAB S-Function with a discrete state

This week, we continue in the same direction and implement an S-Function with a continuous state.

Continuous States

To begin, let's go back to the question posted on MATLAB Answers by K E:

What are continuous and discrete states in Simulink?

and see what Kaustubha Govind mentionned about continuous states:

With continuous states however, Simulink asks the block to provide a derivative (dx/dt) of the state in the Derivatives() method and uses its ODE solver to compute the integral of dx/dt to obtain 'x'. This 'x' can then be accessed in the Outputs() function. For example, to implement an Integrator block...

Let's stop here and see how to implement an Integrator block step by step.

Setup

In an S-Function, the continuous states are declared in the setup method:

Continuous states can not be hidden, like discrete states. There are many reason for this, for example, they are used by the Simulink solver to compute steps taken by the variable time-step solver.

For details on the DirectFeedthrough flag, look at the previous post about discrete states.

Initialize Conditions

Like the discrete state, you need to initialize continuous states:

Outputs

In the Outputs method, you can read the value of the continuous state and assign it to the output port:

Derivatives

Here is the most important part. When you have continuous states, you are not supposed to write to block.ContStates.Data, except for initialization. Instead, you specify the derivative of the states and write that to block.Derivatives.Data. For an Integrator block, the input is the derivative of the output:

The next time the output method will be called, the solver will have integrated the value specified in block.Derivatives.Data and stored the result in block.ContStates.Data.

Conclusion

The important thing to realize about continuous states is that the block provides the derivative of the states to the solver. The solver takes care of the integration and gives back to the block the result of the integration.

For an example like that, look at the demo model msfcndemo_limintm.mdl

Now it's your turn

Share your S-Function experiences by leaving a comment here.

### 5 Responses to “Creating Your Own Block With Continuous States (MATLAB S-Function)”

1. Ashish Sadanandan replied on :

Do you really have a lot of users that use M-file S-Functions? IMHO, these articles would be a lot more interesting to read (and helpful) if you started writing about C S-Functions instead.

2. Guy Rouleau replied on :

@Ashish Sadanandan: Thank you for the comment. My plan is definitely to follow up with C S-Functions.

I decided to highlight a few basic concepts using MATLAB S-Function because, in my opinion, it is easier to understand and debug. But I agree with you that C S-Functions offer a lot more possibility and will be useful for more users.

3. usdb replied on :

hi,
i used sfunction (m-code)in my programm(extended kalman filter),i have not a good result;ihave 5 discrets states and plus the states equations we have error covariance Pe to calculate (reccurant equation) every itteration (in flag 2):when i used the debug i see that Pe calculated is not used in the after itteration but Pe initial.
i writed Pe initial before flag 0
thank you.

4. upul replied on :

How can I build an S function for more than one output and more than one input. As an example if it is required to compare three values (a, b, c) and if a=b then out1 shall be 1.If b=c then out2 shall be 1. otherwise outputs shall be 0. How to write an s-function for something like that.

5. AJ replied on :

This article really helped! Defo big thanks to you for this insightful information. Keep up the good work!

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 4 + 6 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Guy Rouleau and Seth Popinchalk are Application Engineers for MathWorks. They write here about Simulink and other MathWorks tools used in Model-Based Design.

These postings are the author's and don't necessarily represent the opinions of MathWorks.