Guy and Seth on Simulink

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

Posted by Guy Rouleau,

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:

Declaring continuous state

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:

Initialization of continuous state

Outputs

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

Output of method using continuous state

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:

Update of method using continuous state

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 CommentsOldest to Newest

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.

@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.

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.

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.

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

Add A Comment

What is 1 + 2?

Preview: hide

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