Seth on Simulink

September 25th, 2009

Custom Components in Physical Models

This week I want to introduce guest blogger Steve Miller to talk about the Simscape language.

Steve Miller - Physical Modeling Expert

Introduction to the Simscape Language

I’ve been working with MATLAB and Simulink since my college days, and one of the things I liked most about these tools is that I’m free to create just about anything with the commands and blocks.  Whenever my boss asked me, “Do you think you could use MATLAB to...,” the enthusiastic “Yes!” was halfway out of my mouth before he even reached the end of the sentence.

When I started working with the physical modeling products at The MathWorks, I was hoping that I’d have the same freedom there.  Well, starting with Release 2008b, I do!  Using the Simscape language (an enhancement to Simscape, which was first released in R2007a), I can create my own physical modeling blocks just like I created cool MATLAB scripts and Simulink subsystems to solve all those tricky problems my boss threw at me.

What is the Simscape language?

The Simscape language is a MATLAB-based, object-oriented language for modeling physical systems.  The big difference here is that I can use this textual language to create components that have physical connections, such as hydraulic ports on valves and electrical terminals on resistors, instead of inputs and outputs like I’m used to doing in Simulink.  Why is this important?  Well the models built with physical connections are much easier to understand – models of electrical systems look like an electrical schematic.  And, if I create something useful and I want to reuse it in another model, it is much easier, for I don’t have to worry about routing inputs and outputs -- I just plug my new electrical component into the circuit and try it.

Here is an example of a component I have defined using the Simscape language.  It’s a DC motor.  You’ll see that it has four physical ports (not inputs and outputs) – two electrical and two mechanical.  These physical ports let me plug the component into a physical network.

Simscape - Custom DC Motor library

How does it work?

You can use the Simscape language to define physical domains, components, and libraries of components.  It’s usually easiest to look at an example, so let’s look at the definition of a DC motor  created in the Simscape language.

Here we see the first 11 lines of the file.  This is where we define the component name, add a description, and define the physical ports:

Simscape Component Definition of Nodes

You can see that we have two electrical (+ and – terminals) and two mechanical rotational ports (motor shaft and motor housing).  These are reflected in the component shown above. 

In the next 6 lines of the file, we define the parameters (including units!).  These are the values that I or another user may want to change:

Simscape parameters section

You’ll notice that in the final component, the prompt, the default value, and the units all show up in the dialog box shown here: 

Simscape Custom DC Motor Parameters dialog box

Just like most Simscape blocks, there a link to the source code!  In lines 18-23 of the file, we define the variables associated with the physical domains (electrical and mechanical) we need for the equations we will define in the equations section.

Simscape variables section

In the setup section, we set up the connections between the variables to the physical ports.  Remember Kirchoff’s laws?  Well, we’re essentially applying them to both the electrical and mechanical domains (yep, they work in other physical domains, too, if you define them properly). 

Simscape function setup section

You’ll also notice that we have used MATLAB to make sure that the value of the resistance in the dialog box is not less than zero.  I can use MATLAB code in this section to do other things like calculate values or initialize variables.  All that MATLAB I learned is not going to waste!

In the last lines of the file, I define the equations for the motor.  Take a look:

Simscape equations section

See the “==” symbol there?  I’ve used that to define an implicit equation.  This means I have set up a relationship between these quantities (including time derivatives – see the .der?). I’m not assigning values, nor does this represent a Boolean expression.  I could have defined them as shown below and the results would be the same:

Simscape equations section - alternative

And there you have it!  Once I have defined the file, I execute ssc_build and it produces a Simscape component I can connect to other Simscape components in my physical model.

Videos of Simscape language in action

I have made a video demo of modeling a nonlinear spring using the Simscape language.  Take a look to see the blocks in use and the build process.

For a more complete experience, watch this webinar on the Simscape language.

Now it’s your turn

Want a closer look at this example?  Download it from the MATLAB Central File Exchange.

Are you using the physical modeling tools? Have you tried the Simscape language? Post a comment here and tell us about it.

Leave a Reply

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


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • Mohamamd: Hi Suth, I try to simulate a load tap-changing transformer in simulink but its control part has to be...
  • Han Geerligs: Hello Guy, thanks for the clarificaton and link. However in the documentation I am missing the...
  • Guy: @Han, you probably already know, but I think it is good to share with everyone. To zoom in use the key...
  • Han Geerligs: Hi Seth, Once again I’d like to point out that my biggest accelerator is using mouse and keyboard...
  • XaL: Hi, thanks for the tips. As someone wrote in http://blogs.mathwor ks.com/seth/2009/03/ 13/new-%C2%A0rele...
  • Uba osy: Hi, in the introductory example for fuzzy logic toolbox it was noted that using non fuzzy means, you could...
  • Prashant: How can I have same example but instead AC(1 to 10V 50 or 60Hz) and DC(0.5 to 10 V) then adding AC+DC but...
  • adrian chavarro: Great tool, for educational and sicentific, simulation. I would like to know where can i place a...
  • Ashish Sadanandan: @wei, I was talking about the case where the compiler would perform the ‘model_Xdim...
  • wei: @Ashish, I agree with your observation on compiler optimization but fail to see why Han’s code would be...

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