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

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

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

Google Online Preview   Download