This week I want to introduce guest blogger Steve Miller to talk about the Simscape language.
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.
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:
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:
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:
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.
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).
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:
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:
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.
4 CommentsOldest to Newest
I am trying to create the dynamic masking of the simscape system. can you give a example on that how to create the dynamic masking in simscape.
I’ll explain clearly: suppose consider a finding surface area of the cylinder, rectangle. If user selected Cylinder, it should ask only for Length and radius of the cylinder. Similarly if user selected rectangle, it should prompt for entering the length and width only. Please help me in this problem.
Thanks and regards
I wrote a code with simspace language for my custom component.
But I don’t know how should I convert it to a component that I can use it in my model.
Please send me a simple example.
Hi Ali — use the command ssc_build to create a component model that you can use in a Simulink diagram.
A colleague linked me to this website. Thanks for the details.