web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyftpsites/o…  · web...

35
PART I: INTRODUCTION TO MATLAB Starting MATLAB On Windows platform, start MATLAB by double-clicking the MATLAB shortcut icon on your Windows desktop. Once you double click the MATLAB icon, a window similar to the following one will appear on your computer: Com m and W indow Com m and H istory EnterM ATLA B functions at the com m and window prom pt TheCom m and H istory m aintains a record ofthe M ATLA B functions you ran If you do not see a similar setting , then - Click Desktop - Select Desktop Layout - Select Default

Upload: hoangcong

Post on 08-Apr-2018

235 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

PART I: INTRODUCTION TO MATLABStarting MATLABOn Windows platform, start MATLAB by double-clicking the MATLAB shortcut icon on your Windows desktop.Once you double click the MATLAB icon, a window similar to the following one will appear on your computer:

Command Window Command History

Enter MATLAB functions at the command window prompt

The Command History maintains a record of the MATLAB functions you ran

If you do not see a similar setting , then - Click Desktop- Select Desktop Layout- Select Default

Page 2: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Entering MatricesTo enter the matrix ¿ [1 2 3

4 5 6 ] , in the command window, type:

once you hit enter, MATLAB replies with:

Let A=[1 23 4 ] and B=[2 3

1 0] be two matrices. We illustrate some of the very basic operations that can be performed with MATLAB.Addition of matrices: +To perform the sum of the matrices A and B, type

, MATLAB replies with Multiplication of matrices: *

Page 3: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

, MATLAB replies with Element-by-element multiplication: .*This is not the usual matrix multiplication; it multiplies the entries of two matrices component-wise.

Element-by-element division: ./It divides the matrix A by the matrix Bcomponent-wise.

. Note what happen when a division by zero had occured.The Colon Operator: The colon, :, operator is one of the most important MATLAB operators.Typing 2:8 produces a row-vector of integers from 2 to 8

Page 4: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

We can also specify an increment, for instance with an increment of 0.95 we obtain

ExampleSuppose z=f (x , y )=x2+ y is a function of two variables. We can calculate the values of z=f (x , y ) for several values of x and y as follows:

Note that x2 does not make any sense therfore it is essential that we use element-by-element multiplication or otherwise MATLAB returns an Error.SubscriptsLet A=[1 2 3

4 5 67 8 9]. The entry in the i-th row and j-th column is dented by A(i , j).

So if you type

MATLAB responds with

Page 5: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

We can change the entries of the matrix A; for instance if we want to change the entry in the 1st row and 3rd column to 98 we type

Occasionally we want MATLAB to display the output of an entire row or a column; this can be done as follows.If we wish to display the 2nd row , then we type

Similarly we can ask MATLAB to display a specific column; in this example we display the 3rd column:

Generating MatricesThe function eye(n) returns the n-by-n identity matrix

Page 6: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

and The functions zeros and ones return zeros and ones respectively

andNumbersInfinity is denoted by Inf ; try to type 1

0 :

The imaginary unit is denoted by i or j; so avoid using i and j within a loop or an if statement.

andNot-a-Number is denoted by NaN. Try to type ∞−∞:

Page 7: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

The quantity eps is a floating point relative precision ; that is, eps is a very small quantity.

but eps is not equal to zero: Changing the numeric display settingTo change the numeric display setting (the way numbers appear) do one of the following.- To make a temporary change i.e. for a single MATLAB session use the format function:

Format long displays: while formal short:

- To make a permanent change use the Preferences dialog box (accessible from the MATLAB File menu): FileàPreferencesàNumeric display

Page 8: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

M-filesAn M-file is a text file that contains a MATLAB code. As an example let us create an m-file to add the two matrices A=[1 2

3 4 ] and B=[5 67 8 ].

Step 1 Click FileàNewàBlank M-file; you will see a blank text file similar to this one:

Page 9: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Step 2Type your code in the text file (m-file):

Step 3Save the m-file: FileàSave Asàmatrix.m

Page 10: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Now you have an m-file whose name is matrix. To run the file, do one of the following.- From the MATLAB Command Window: just type the file's name namely matrix

- From the m-file itself: DebugàRun matrix.m

Page 11: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

The output will be displayed in the MATLAB Command Window.Notebook /M-bookNotebook allows you to access MATLAB from Microsoft Word. To create a new M-book, type notebook within the MATLAB Command Window:

At this point, Notebook starts Microsoft Word and creates a new M-book called Document1

Note that Microsoft Word adds the expression (Compatibility Mode) immediately after the document's name (see the above picture).Also a Notebook menu will be added to the Microsoft menu bar. To see this, click Add-Ins:

Page 12: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Inside Notebook, type the following three lines:

Nothing special with the first two lines; you can type anything you want. The third line is a MATLAB code used to draw a surface. Again nothing special with this code, you can type any MATLAB script.Highlight the cell that contains a MATLAB code (that is ezsurf('x^2+y^2','circ') ) and then right click the mouse to select Evaluate Cells :

Page 13: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

MATLAB will run the script and inserts a figure in your notebook.

You can edit the above document the same way you edit your Microsoft documents.

Page 14: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Saving Work SpaceOccasionally, we would like to save our variables for other MATLAB sessions. Saving the work space is one of the several available options.At this point perhaps it is a good idea that you delete all variables in the workspace window by typing clear in the command window. Ttype clc to clear the command window as well. Your MATLAB window should look like this one:

Now type something in the command window and hit enter:

All variables typed in the command window now appear in the Workspace area. Any variable that appears in the Workspace area can be recalled and used at any time. To save the variables for another session, click FileàSave Workspace As and assign a file name for the file. In this example, we save the work space to the file workspace1.FileàSave Workspace Asà workspace1

Page 15: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

MATLAB will save the file as workspace1.mat; that is with the extension .mat. Now type clear to clear all variables from the workspace area and hit enter. Your workspace window is now empty.To reload the variables to the work space area, and hence be able to use them, type

All saved variables will appear in the work space area and can be used for subsequent computations.Symbolic ObjectsA String is a set of characters enclosed in single quotes such as 'abcd', 'Ma280', and 'sqrt(2)'.

Page 16: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

The command sym takes a string as an argument and tells MATLAB to interpret this string as a symbolic object. As an example,

creates a symbolic object a that prints as π /2.To illustrate the difference between a standard data type and the corresponding symbolic object, type:

The first answer is not 0 because MATLAB used 1.5708 as an approximation of π2 to calculate cos ( π2 ). So sym('pi/2') refers to the exact symbolic number π2 .Symbolic Variables and ExpressionsAs an example, a=sym('ma280') creates a symbolic variable a that prints as ma280:

Page 17: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Frequently we would like to create a symbolic variable x that prints as x:

or simply by typing

To create symbolic expressions, first create the symbolic variables then define the symbolic expression:

We can substitute a numerical value for a symbolic variable in a symbolic expression using the subs command:

Page 18: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

If you do not specify a variable to substitute for, MATLAB chooses the letter closest to x. If there are two letters equally close to x, MATLAB chooses the one that comes later in the alphabet. Inline functionsThe fundamental difference between an inline function and a symbolic expression is that an inline function can be called with arguments and a symbolic expression cannot. Let us use the inline command to create an inline function of two variables:

Now we can use the inline function f(x, y) the same way we are used to do:

, ,

Page 19: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

It is a good idea that you create m-files for functions that you tend to use more frequently. Save those m-files in a specific folder, say, Functions_Folder.As an example we create a function, called expsqrt, defined by the formula: expsqrt ( x , y )=e√x2+ y2+ 1.We begin by creating a new m-file:

Next, save the m-file in the folder Functions_Folder under the name expsqrt.m:

Now make sure that you are using the right folder and, in the command window, type:

Converting symbolic expressions into inline functionsRecall that the inline command requires a string as an input. Consider the following symbolic expression G:

Page 20: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

We can use the command char to turn G from a symbolic expression into the string 'x^2+y^2' and then use the inline command to define an inline function that has the same rule as G:

Using inline functions to evaluate vectorsInstead of a symbolic variable x, we can let an inline function accept a symbolic vector as an argument.

Observe that we used x. instead of x that is x. is a vector. Now let us do some computations with the vector function G:

Page 21: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Functional Operations To see some of the operations that can be performed with MATLAB, click the arrow under the x that appears in the command window: .

For instance, the derivative of f ( x )=x5+cos (x ) is

Page 22: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

The 4th derivative of f ( x )=x5+cos (x ) is

The limit limx⟶∞

2 x+13 x−2 is

The integral ∫−∞

∞ 1x2+1

dx is

Page 23: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Plotting FunctionsIn this example we plot the function f ( x )=sin (x ) for −π ≤x ≤π.We begin by creating a vector of x-values ranging from – π to π:

Next we ask MATLAB to compute sin(x) for each of the specified x values:

Finally we plot the function y= f ( x )=sin (x):

Page 24: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

If you wish to edit Figure 1, then you need to enable Plot Tools by clicking the box under help.

Page 25: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

RemarkWe could just type

Page 26: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

RemarkSome care should be exercised when plotting a function such as f ( x )=x2+1.

We should use element-by element exponent: x.^2 instead of x:

Parametric Plots In this example we plot the ellipse x=2 cos (t ) , y=5sin (t).

Page 27: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

The ellipse appears as a circle simply because the axis are not equal:

Multiple Plots

Page 28: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

In this example we plot the two functions y=x2+1, y=−x2+2 and display the result on the same figure.

Let us a title to the figure; this can be done in terms of the command title.

Let's assign different colors to the two curves.

Page 29: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Finally we label the axis.

The ezplot MATLAB Function

Page 30: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

As the name indicates, the ezplot command provides a quick way to plot functions. For instance, the graph of f ( x )=x2 cos (x) for −10≤x ≤10 can be generated as follows.

ezplot accepts symbolic expressions as well; we could type the following sequence of commands to produce the same graph:

Plot Types Before you start this section it is desirable that you clear the Command Window, History, and Workspace. In the command window enter the vectors x=[2 3 6 8 9] and y=[4 9 19 21 30]

Once you enter the vectors, the variables x and y appear in the workspace command.

Page 31: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Take a careful look at the MATLAB tool bar; it does not have the icon Graphics. Now use the mouse and the shift key to select the variables x and y listed in the workspace.

Once you select variables from the workspace, the icon Graphics appears on the MATLAB toolbar.Click Graphics and select More Plots , then select any of the listed choices(click on the image to the left of the function's name). Let's select quiver(x,y).

As soon as a selection has been made, MATLAB displays the appropriate graph. In this case:

Page 32: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Plot3 and ezplot3 As an example we plot the space curve of the vector function

r ( t )=¿ sin (t ) ,cos ( t ) ,t>,0≤ t ≤4 π

that is the space curve x=sin ( t ) , y=cos (t ) , z=t .

We could enter the expression as a string without declaring x as a symbolic variable:

Page 33: web4students.montgomerycollege.eduweb4students.montgomerycollege.edu/facultyFTPSites/o…  · Web viewPART I: INTRODUCTION TO MATLAB. Starting MATLAB. On Windows platform, start

Alternatively, we could use the function plot3 to obtain the same graph: