- Shopping Bag ( 0 items )
Want a NOOK? Explore Now
MATLAB By Example guides the reader through each step of writing MATLAB programs. The book assumes no previous programming experience on the part of the reader, and uses multiple examples in clear language to introduce concepts and practical tools. Straightforward and detailed instructions allow beginners to learn and develop their MATLAB skills quickly.
The book consists of ten chapters, discussing in detail the integrated development environment (IDE), scalars, vectors, arrays, adopting structured programming style using functions and recursive functions, control flow, debugging, profiling, and structures. A chapter also describes Symbolic Math Toolbox, teaching readers how to solve algebraic equations, differentiation, integration, differential equations, and Laplace and Fourier transforms. Containing hundreds of examples illustrated using screen shots, hundreds of exercises, and three projects, this book can be used to complement coursework or as a self-study book, and can be used as a textbook in universities, colleges and high schools.
Chapter Outline
Lesson 1.1 Basics of the Matlab Integrated Development Environment 1
Lesson 1.2 Matlab Script Files 8
Lesson 1.3 Matlab Editor—Cell Mode 17
Answers to Selected Exercises 20
Lesson 1.1 Basics of the Matlab Integrated Development Environment
Objectives
To familiarize beginners in Matlab with its programming environment.
To learn how to use Matlab to execute simple commands, create variables, and display their values.
Topics
1.1.1 Matlab Integrated Development Environment
1.1.2 Creating Scalar Variables
1.1.3 Creating Vector Variables
1.1.4 Creating Array Variables
1.1.1 Matlab Integrated Development Environment
Matlab version 7.12 (R2011a) is used here to explain the integrated development environment (IDE). Even though this specific version of Matlab has been used here, other Matlab versions can still be used to follow the discussions presented in this book if you do not have this exact version.
First launch Matlab. When MATLAB opens you should see four windows: the Command Window, the Command History, the Workspace, and the Current Folder. If you do not see these windows, you can direct Matlab to display them by going to the Menu->Desktop and then click the window you would like to display, so that it has a tick
In addition to your ability to display the required windows, you can arrange them in any way you prefer. This can be achieved by clicking the title bar of any window, dragging it, and placing it in the required location within the Matlab main window.
The Command Prompt ">>" is displayed in the Command Window. When the Command Window is active, a blinking cursor appears to the right of the prompt. The cursor and the prompt signify that Matlab is ready to perform a computational operation.
The Workspace lists all the variables that you have created so far and their values. Other information is also displayed here, such as the maximum and minimum values of any variables that are created.
The Command History records all of the commands that you have previously typed at the Command Prompt. You can select one of these commands using the mouse and execute it in the Command Window by double-clicking it from within the Command History window.
Current Folder: You can direct Matlab to set up a folder of your choice to be a current folder. This will then be your working folder, where you can save your programs.
The main window that contains these four windows is called the Matlab Desktop.
As mentioned earlier, the Matlab Desktop contains four windows: the Command Window, the Command History, the Current Folder, and the Workspace. When one of these windows is active, the remaining windows become inactive, but the contents of the remaining windows may still be updated if a script is running. The title bar of the active window appears in a dark gray color, whereas the title bar of the inactive window appears in light gray color. In the preceding figure, the Command Window is active and the title bars of the other three windows are grayed out (inactive).
The Menu in the Matlab Desktop dynamically changes according to the window selected (the active window).
Click on the Workspace window and note the change in the Menu.
Click another time on the Command History window and note the change in the Menu.
1.1.2 Creating Scalar Variables
Matlab is a short name for Matrix laboratory. As the name indicates, Matlab is a matrix-based software package, which, infact, considers the scalar variable to be a 1 × 1 matrix. A scalar here means a number such as "2" or "-100."
Select Command Window by left-clicking on the Command Window. The Command Window prompt is ">>" or "EDU>>" for the educational version. To create a scalar variable, type "x = 1;" at the Command Prompt and then press Enter.
>> x = 1;
This command creates the scalar variable x and assigns the value "1" to it. Remember, the characters ">>" are just the command prompt. The semicolon ";" is used to direct Matlab not to display the value of the variable x in the Command Window. If you leave the semicolon off the end of a command, then it will be echoed straightaway to the display, which can sometimes be cumbersome for very large arrays. Alternatively, it sometimes can be useful to see the results displayed.
Note the changes that happened in the Command Window, the Command History, and the Workspace windows. The Workspace displays the x variable, its value, and the maximum and minimum values of the variable. The Command History records the x = 1; command.
Double-click the variable x in the Workspace. The Variable Editor pops up and shows the value of the variable. To change the value of the variable x, double-click on the cell indicated by the black arrow in the following figure and change it to another value, for example, by typing the numeral "2". Press Enter. Close the Variable Editor window by clicking the X at the top right of the Variable Editor window.
You should get the following figure.
1.1.3 Creating Vector Variables
To create a vector variable, type the Matlab command
>> y = [2,3,6,9,11,8,5,3,2, -1];
at the Command Prompt and then press Enter.
This command creates a vector variable with the values
y = [2,3,6,9,11,8,5,3,2,-1]:
The Matlab desktop responds to this command as shown in the following figure.
In order to draw the vector variable y, click on the y variable in the Workspace so that it is highlighted as in the following figure, then go to Menu -> Graphics -> plot(y). A pop-up window appears and plots the vector y.
1.1.4 Creating Array Variables
To create an array variable, type the following Matlab command
>> Z 5 [1,2;3,4];
at the Command Prompt and then press Enter.
Note and make sure that you include the two semicolons and the commas in the command.
This command creates an array variable with the following values:
Z = [1 2 3 4]
The Matlab desktop responds as shown in the following figure. In this book, we will use uppercase letters to name arrays.
To draw the array variable Z, click on the Z variable in the Workspace so as to highlight it (as shown in the following figure), then go to Menu->Graphics-> mesh(Z). A pop-up window appears and displays the array variable Z as a 3D mesh. An alternative to using the pop-up menu is to right-click on the Z array in the Workspace and select mesh(Z) from that menu.
Lesson 1.2 Matlab Script Files
Objectives
To learn how to create, name, save, and execute Matlab script files.
To learn how to make your code readable for others.
To learn how to comment Matlab code.
Topics
1.2.1 Creating a Script File
1.2.2 Naming a Script File
1.2.3 Saving a Script File
1.2.4 Executing a Script File
1.2.5 Matlab Code Readability
1.2.6 Commenting Matlab Code
1.2.1 Creating a Script File
An M-file is a text file that contains a collection of commands that Matlab executes in a sequential order. A script file has the following properties:
It has no arguments (input data) and it does not return any values (outputs).
The commands executed in the script file have the same effect as if these commands were executed in the Command Window.
The variables created by the script file are displayed in the Workspace window.
Suppose that we would like to create a script file that contains the following Matlab commands.
x = 1; y = 2;
To carry out this task, do the following.
Launch Matlab. Then go to Menu->File->New->Script.
The Matlab window Editor pops up. In this window, type the Matlab commands shown in the Editor.
1.2.2 Naming a Script File
Choose a name for this file. The extension for the file must be .m. The following restrictions must be taken into consideration when a script file is named.
The file name must not contain spaces or hyphens (-).
The file name must start with an alphabetical character (az or AZ).
The file name must contain only alphabetical characters (az or AZ), numbers (19) or underscores (_).
Punctuation characters such as commas (,) or apostrophes (') are not allowed, because many of them have special meanings in Matlab.
The file name must be neither a Matlab variable nor an existing Matlab function.
The use of a Matlab reserved word as a file name is not allowed. A list of Matlab reserved words are given below.
'name'
'node'
'output'
'parameter'
'setup'
'signal'
'source'
'terminal'
'through variable'
'variable'
'across_variable'
'build'
'description'
'descriptor'
'element'
'input'
'interface_input'
'interface_node'
'interface_output'
'item_type'
'local_variable'
The use of a Matlab keyword as a file name is not allowed. A list of Matlab keywords are given below.
'break'
'case'
'catch'
'classdef'
'continue'
'else'
'elseif'
'end'
'for'
'function'
'global'
'if'
'otherwise'
'parfor'
'persistent'
'return'
'spmd'
'switch'
'try'
'while'
To check that the file name you have chosen is not a Matlab keyword or a Matlab function, you can use Matlab help and search under your chosen file name. For example, let us choose cat.m as a file name. Type at the Command Prompt
>> help cat
Matlab responds and informs you that there is already a function called cat that concatenates arrays. So you should choose a different file name (and remember to check that this new name is not restricted in the same way too!). Matlab will warn against using names that are existing Matlab keywords, but it will not produce a warning when generating a script with certain function names, for example, the cat function above. Therefore, for this reason you should always test new user-defined names that you are unsure about by using the help facility, as shown previously, to avoid potential problems with name clashes when debugging.
Let's try a different name, such as cat1.m. Type at the Command Prompt
>> help cat1
Matlab responds and informs you that there is no function called cat1 as shown in the following window. So we can save the file with the name cat1.m.
Remember: It can be very helpful to use meaningful and descriptive file names.
1.2.3 Saving a Script File
The next step is to save the script file to a specific folder. Here we will create a folder called Lesson 2, and we will make it Matlab's current folder. To perform this task, click on the icon [??] (indicated by the arrow in the following screenshot) and choose the location where you would like to save the folder Lesson 2.
Click on Make New Folder. Change the name of the folder to Lesson 2. Click OK.
This creates a new folder called Lesson 2 and makes it the current folder for use by Matlab as indicated by the arrow shown in the following screenshot.
Alternatively, you can save your files to an existing folder. Navigate to a folder of your choice and set it to be the current folder for Matlab.
The last step is to save the file. In the Matlab Editor, go to Menu->File->Save As. Type the file name; in this case type cat1.m and click Save.
The file that has just been saved appears in the Current Folder window. In the following screenshot figure, an arrow points to this file as it appears on the Matlab Desktop.
1.2.4 Executing a Script File
There are two methods available for executing the script file cat1.m.
1.2.5 Matlab Code Readability
It is good programming/engineering practice to produce Matlab programs that are tidy and well commented. This makes the code easier to understand, both for you and for others. Sometimes we get involved in the process of writing our code and neglect to comment it, to make it tidy and well structured. If code is written in this manner, it will be difficult to understand when we read it on another occasion, perhaps at a much later date. At that point we will realize the full importance of good code readability. When writing a Matlab program, code readability should be one of our top priorities. Good programming practice is best learned by an example.
(Continues...)
Excerpted from Matlab by Example: Programming Basics by Munther Gdeisat Francis Lilley Copyright © 2013 by Elsevier Inc.. Excerpted by permission of ELSEVIER. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Chapter 1: Matlab Integrated Development Environment (IDE) Chapter 2: Scalars in Matlab Chapter 3: Vectors in Matlab Chapter 4: Arrays in Matlab Chapter 5: Matlab Functions Chapter 6: Conditional Statements in Matlab Chapter 7: Loop Statements in Matlab Chapter 8: Matlab Debugging, Profiling and Code Indentation Chapter 9: Structures in Matlab Chapter 10: Calculus in Matlab
Overview
MATLAB By Example guides the reader through each step of writing MATLAB programs. The book assumes no previous programming experience on the part of the reader, and uses multiple examples in clear language to introduce concepts and practical tools. Straightforward and detailed instructions allow beginners to learn and develop their MATLAB skills quickly.
The book consists of ten chapters, discussing in detail the integrated development environment (IDE), scalars, vectors, ...