In this chapter, we'll learn how to build a Simulink model and run the first simulations.
We'll start with a short description of the MATLAB environment features that we need to know in order to start developing a Simulink model.
Then we'll build a simple model to introduce the basics of Simulink development; we'll learn what the Library Browser is and how to place blocks into the Model Editor.
Finally, we'll develop a more complex model implementing a not-so-simple nonlinear system. There will be a small part of theory detailing the physic equations describing the system before the implementation.
Simulink is not a standalone tool, but lies on MATLAB's shoulders; it's almost impossible to use Simulink without keeping an eye on MATLAB's main window, not to mention that you need to start MATLAB before even opening a model.
Simulink inherits from MATLAB the current working folder, the workspace, and the path and uses MATLAB's Command Window to report errors, warnings, and notes.
The first time you open MATLAB, you should see the default main window view with the following sections:
An example is shown in the following screenshot:
The Command Window panel is the place where Simulink will report most of the errors, warnings, and information—always keep an eye on it while debugging a model!
The Command Window panel also allows us to declare new variables that will be stored into the workspace. In short, the Command Window panel works very much like any interactive OS terminal shell, accepting commands written in the MATLAB language.
Try entering these two commands and see what happens:
new_string = 'hello world!';
new_number = 0.01;
The newly created variables have appeared in the Workspace panel and are ready to use.
It's worth noting that every command executed is stored in the Command History panel too; if you need to re-execute a command you entered earlier, simply drag-and-drop it into the Command Window panel.
The workspace holds the variables we create in the session, allowing you to edit and delete them through the Workspace panel.
Simulink is able to see the variables in the workspace and use them in the model. From Simulink's perspective, they belong to the base workspace.
Keep in mind that the workspace resides in volatile memory and is not persistent; as soon as MATLAB is closed, the workspace is lost.
To save the workspace into a MATLAB file (with the .mat
extension), click on the Save Workspace button in the HOME tab. MATLAB will ask you where you wish to store it, with the working folder as the default choice.
The working folder (called Current Folder in MATLAB's main window) is the folder that is currently open in MATLAB and all the files contained here are visible to MATLAB and Simulink. The exact path of the working folder is shown in the address bar right above the Command Window, the Workspace, and the Current Folder panels.
To execute a file, you can simply drag-and-drop the file from the Current Folder panel to the Command Window panel. This works with all MATLAB and Simulink files.
The path variable is the list of folders where MATLAB will look for libraries at startup (this is very similar to how the $PATH
variable is used in most operating systems). If you need to use a Simulink blockset (a library of ready-to-use blocks), you must place it into a folder listed in your MATLAB's path.
You can easily view and edit MATLAB's path by clicking on the Set Path icon in the HOME tab. A new window will appear, listing every folder present in MATLAB's path and giving you the option to add other folders and change the order in which they appear.
The display order is important; if a file with the same name is present in two or more folders listed in the path variable, MATLAB will use the one found in the folder nearest to the top of the search path.
The exact file where the path variable is stored is the pathdef.m
script, located in your MATLAB install folder (usually $MATLABROOT/toolbox/local
).
If you don't remember where you've installed MATLAB, just enter matlabroot
in the Command Window.
A great thing to have in the path is a customized startup script; if you need to execute a set of commands every time MATLAB starts, put them in a startup.m
script and save the script in one of the folders belonging to MATLAB's path.
If you've added too many folders to the path and something is going wrong, you can restore the default path with the restoredefaultpath
command.
This is all you need to know about the MATLAB environment.
Remember that Simulink is reading everything from MATLAB and you will save yourself a lot of trouble.