InteractiveMatlabCourse - University of Notre Dame

Interactive Matlab Course

2009-2010 March 22, 2010

Contents

1 Basic elements of MATLAB

4

1.1 What is MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2 Starting and stopping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.3 Commands in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.4 Help facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.6 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.7 Script m-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

1.8 User defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.9 Saving and loading data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2 The numerical toolbox

24

2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.2 Graph of a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.3 Zeroes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.4 Extrema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.5 Numerical integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.6 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

3 The symbolic toolbox

29

3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.2 Expressions with variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3.3 Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

3.4 Differentiation and integration . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3.5 Numerical values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3.6 Manipulation of expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3.7 Symbols, strings and numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

1

4 Linear algebra

37

4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

4.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

4.2.1 Entering matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

4.2.2 Matrices with symbolic elements . . . . . . . . . . . . . . . . . . . . . 38

4.2.3 Entering vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

4.2.4 Special matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

4.2.5 Indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

4.3 Matrix operations in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . 42

4.4 Solving sets of linear equations . . . . . . . . . . . . . . . . . . . . . . . . . . 46

4.4.1 The row reduced echelon form . . . . . . . . . . . . . . . . . . . . . . 47

4.4.2 Solving sets of equations with the Symbolic Toolbox . . . . . . . . . . 49

4.5 Numerical aspects of the use of MATLAB . . . . . . . . . . . . . . . . . . . . 49

5 Differential equations

51

5.1 Differential equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

5.2 Solving differential equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

5.2.1 Solving differential equations numerically . . . . . . . . . . . . . . . . 54

5.2.2 Sets of differential equations . . . . . . . . . . . . . . . . . . . . . . . . 55

5.2.3 The direction field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

5.2.4 Plotting of integral curves . . . . . . . . . . . . . . . . . . . . . . . . . 57

5.2.5 Example of numerical solution . . . . . . . . . . . . . . . . . . . . . . 57

5.3 Solving differential equations symbolically . . . . . . . . . . . . . . . . . . . . 59

5.3.1 First order differential equations . . . . . . . . . . . . . . . . . . . . . 59

5.3.2 Sets of first order differential equations . . . . . . . . . . . . . . . . . . 61

5.3.3 Higher order differential equations . . . . . . . . . . . . . . . . . . . . 61

6 Programming in MATLAB

62

6.1 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

6.2 Some remarks about variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

6.3 Writing programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

6.4 Programming language constructs . . . . . . . . . . . . . . . . . . . . . . . . 65

6.4.1 For-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

6.4.2 If statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6.4.3 While-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

6.5 Creating programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

2

6.6 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.6.1 Structure variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

7 Simulink

75

7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

7.2 Creating a block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

7.2.1 Example of a block diagram . . . . . . . . . . . . . . . . . . . . . . . . 76

7.3 Constructing a Simulink model . . . . . . . . . . . . . . . . . . . . . . . . . . 77

7.4 Running a simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

7.5 Example: Neuron model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

A Exercises

83

A.1 Exercises chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

A.2 Exercises chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

A.3 Exercises chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

A.4 Exercises chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

A.5 Exercises chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

A.6 Exercises chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

A.7 Exercises chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

B Answers

119

B.1 Answers chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

B.2 Answers chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

B.3 Answers chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

B.4 Answers chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

B.5 Answers chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

B.6 Answers chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

B.7 Answers chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

3

Chapter 1

Basic elements of MATLAB

1.1 What is MATLAB?

MATLAB is a computer program designed for technical calculations. Its name is an abbreviation of "Matrix Laboratory". In this program, matrix computations are implemented in a straightforward manner. However, many other pre-programmed routines are available. In addition, it is possible to implement user-defined calculation routines. MATLAB allows users to implement calculations in relatively short programming time. When these calculations have been performed, they can be visualised by means of several plot-routines. MATLAB can be extended with so called toolboxes. In general, these are collections of MATLAB functions that are tailored for special classes of problems. The `(Extended) Symbolic Math Toolbox' is an exception. To a great extent, this toolbox consists of ingredients from the computer algebra package MuPAD. MATLAB performs calculations with the aid of matrices. These are rectangular schemes of numbers, which are also called arrays. The `Symbolic Math Toolbox' is quite different from MATLAB itself, since symbolic calculations hardly make any use of arrays. For this reason it is important to know whether a function used is a MATLAB function or a `Symbolic Math Toolbox' function. This will not be completely clear at the outset, but it will become clearer after regular use of MATLAB.

1.2 Starting and stopping

MATLAB can be started by clicking on the selecting the "MATLAB" option in your startup tree or possible on your Windows desktop. When MATLAB is started, a window similar to the one below should appear: The main window in MATLAB is the command window, positioned at the center, in which commands can be typed after the MATLAB prompt:

>>

Commands are entered with the `return' key. The command is then executed by MATLAB. If MATLAB is ready, after possible outputs a new prompt appears.

4

The window on the left is the "Current directory" window. The "Current directory" window depicts the files in the current directory. This is the first user-governed directory where MATLAB will look for files or functions. On the top right you will find the "Workspace" window, in which all declared variables are shown. The bottom right window is the "Command history", where you can find all commands you have (recently) entered. By double-clicking on such a command, MATLAB reuses the command. Stopping MATLAB can also be done in different ways:

? By means of the command:

>> quit

? Via the menu File\Exit MATLAB ? Clicking on the cross in the upper right of your window.

1.3 Commands in MATLAB

By clicking on the "Command Window", after the MATLAB prompt >>, a command can be typed. After having pushed the `return' key, the command is executed, and possibly the result appears on the screen. A command and its result looks like this:

>> a = 1 + 2 + 3 a=

6

The result of 1 + 2 + 3 is assigned to the variable a. You can now use this variable in succesive operations, such as:

5

>> b = 2*a + a/3 b=

14

In MATLAB, variables are introduced by assigning a value. Note, that the value can be numerical values, matrices (called arrays), or other types. You can use the variable, and later possibly assign a new value to the variable. It is not possible to perform calculations with variables that have not been assigned a value. A command does not need to start with an assignment of the form variable =. In such a case, the result is automatically assigned to the variable ans. You can then use ans further.

>> 4*a + 1 ans =

25 >> ans*ans ans =

625

If you now want to know what the value of a is, the following command suffices:

>> a a=

6

Normally, MATLAB displays the output below the command. The output is suppressed by ending the command with a semicolon. After having entered the command s = 1 + 2, the screen looks as follows

>> s = 1 + 2;

The value 3 has been assigned to the variable s. If you want to know the value of this variable, you can either look in the " Workspace" window on the top left of your MATLAB window, or request the value of s as follows

>> s s=

3

If a command is longer than one line, you can end the line with three dots, and then press the `return' key. You can then continue on the following line. After completing the command, the screen looks as follows:

>> s = 1 + 2 + ... 3+4 s=

10

6

MATLAB a+b a-b a*b a/b a^b

Standard

a+b a-b

ab

a b

ab

Table 1.1: Standard operations

MATLAB Standard

sin(x) sqrt(x)

sin(x) x

cos(x) exp(x)

cos(x) ex

tan(x)

tan(x)

log(x) asin(x)

ln(x) sin-1(x)

log10(x) acos(x)

log10(x) cos-1(x)

abs(x) atan(x)

|x| tan-1(x)

sign(x)

sign(x)

mean(x)

mean(x)

std(x)

standard deviation

min(x)

min(x)

max(x)

max(x)

rand(x,y) returns x ? y array of random numbers, distributed

uniformly in [0,1] *

randn(x,y) returns x ? y array of random numbers, distributed

normally with mean 0 and variance 1*

round(x) Rounding to nearest integer

floor(x) Rounding to smaller integer

ceil(x)

Rounding to larger integer

* Omitting y results in x ? x array and

omitting x,y yields a single number

Table 1.2: Mathematical operations

Mathematical expressions

You cannot enter mathematical expressions literally. The Tables 1.1-1.2 make clear how mathematical expressions can be entered. The variables in these tables are to be interpreted as numbers.

Remark: The order of operations in MATLAB is the standard order: first raising to a power,

then multiplication and division, and after that addition and subtraction. To deviate from

this sequence, parentheses `(' and `)' need to be used to define the order of calculations, e.g.

1 exp(3)+1

is

obtained

by

running

1/(exp(3)+1).

7

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download