Welcome to the MATLAB Central Simulink blog! The purpose of this blog is twofold: First, I want to share Simulink tips and tricks that I've learned over the years. At the same time, I hope to learn from you about your experiences with Simulink. Where is it good? How can we improve it?
I've been using Simulink for 9 years, first as a member of our technical support team, and then as a trainer. I still remember when I first learned about the power of Simulink as a newly hired support engineer back in 1998. During a training class I saw differential equations drawn on a whiteboard and then simulated with a few clicks of the mouse. I got excited by how clearly it all worked. Simulink provided me with a framework for thinking about systems and the relationships between their basic components.
Of course, Simulink is much more than a simple tool for solving differential equations. It has a richness and depth that will give me plenty of material to draw on. What kind of topics can you expect to read about here? We'll talk about applications of Simulink in controls, signal processing and communications system design. With your help, we'll talk about how Simulink is used in areas I don't even know about yet. We'll talk about Model-Based Design in general, and we'll get a peek at some of the internal machinery of Simulink. I look forward to posts about all of these:
- sample times
- model reference
- custom code
An example: batch simulation
Let me start off with a specific example about running simulations. Simulink models are built using block diagrams, and once they've been created most everyone runs them by clicking the run button on the toolbar.
If you have to run a sweep of parameters you might change the parameter and then click run, change it again and click run. But if you have to run hundreds or thousands of simulations while modifying a parameter you will want to write a script for batch simulation. You can run simulations directly from MATLAB with the sim command like so.
>> [t,x,y] = sim(sys);
This returns the root level outputs (y) and the internal states of the model (x) at each recorded time step (t). A batch script might look like this:
open_system('vdp_mu') muSweep = .5:.1:1.5; for i = 1:length(muSweep) mu = muSweep(i); [t,x,y] = sim('vdp_mu'); plot(t,y); hold on; end title('VDP States for mu=.5 to 1.5') xlabel('Time'); legend('x1','x2')
If you are already familiar with the sim command, then you probably know that some times you are only interested in the outputs (y) of the model, and not the states (x). For large models, or long simulations, those state variable outputs could be a waste of valuable memory. Using simset you can provide additional options to the sim command to specify that you only want time (t) and outputs (y). The state variable returned from sim will be empty.
[t,x,y] = sim('vdp_mu',,simset('OutputVariables','ty')); whos t x y
Name Size Bytes Class Attributes t 206x1 1648 double x 0x0 0 double y 206x2 3296 double
You can learn more about sim and simset in the Simulink documentation.
What do you think?
That's your first Simulink tip. Now it's your turn. What do you like about Simulink? What would you like to talk about in future posts? Please leave a comment below. I have learned from Doug that t-shirts get comments on your blog, so I'll be randomly selecting five commenters on this post to receive a MathWorks t-shirt!
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.