Download Matlab PDF

TitleMatlab
File Size319.2 KB
Total Pages74
Document Text Contents
Page 1

INTRODUCTION TO MATLAB FOR
ENGINEERING STUDENTS

David Houcque
Northwestern University

(version 1.2, August 2005)

Page 2

Contents

1 Tutorial lessons 1 1

1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Basic features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3 A minimum MATLAB session . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.1 Starting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.2 Using MATLAB as a calculator . . . . . . . . . . . . . . . . . . . . . 4

1.3.3 Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.4 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.4.1 Creating MATLAB variables . . . . . . . . . . . . . . . . . . . . . . . 5

1.4.2 Overwriting variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.4.3 Error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.4.4 Making corrections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.4.5 Controlling the hierarchy of operations or precedence . . . . . . . . . 6

1.4.6 Controlling the appearance of °oating point number . . . . . . . . . . 8

1.4.7 Managing the workspace . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.4.8 Keeping track of your work session . . . . . . . . . . . . . . . . . . . 9

1.4.9 Entering multiple statements per line . . . . . . . . . . . . . . . . . . 9

1.4.10 Miscellaneous commands . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.4.11 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 Tutorial lessons 2 12

2.1 Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.1.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

i

Page 37

4 5 6 40 50 60

7 8 9 70 80 90

-1 -2 -3 1 0 0

-4 -5 -6 0 1 0

-7 -8 -9 0 0 1

2.5.13 Matrix generators

MATLAB provides functions that generates elementary matrices. The matrix of zeros, the
matrix of ones, and the identity matrix are returned by the functions zeros, ones, and eye,
respectively.

Table 2.4: Elementary matrices

eye(m,n) Returns an m-by-n matrix with 1 on the main diagonal
eye(n) Returns an n-by-n square identity matrix
zeros(m,n) Returns an m-by-n matrix of zeros
ones(m,n) Returns an m-by-n matrix of ones
diag(A) Extracts the diagonal of matrix A
rand(m,n) Returns an m-by-n matrix of random numbers

For a complete list of elementary matrices and matrix manipulations, type help elmat
or doc elmat. Here are some examples:

1. >> b=ones(3,1)
b =

1

1

1

Equivalently, we can define b as >> b=[1;1;1]

2. >> eye(3)
ans =

1 0 0

0 1 0

0 0 1

3. >> c=zeros(2,3)
c =

0 0 0

27

Page 38

0 0 0

In addition, it is important to remember that the three elementary operations of ad-
dition (+), subtraction (−), and multiplication (∗) apply also to matrices whenever the
dimensions are compatible.

Two other important matrix generation functions are rand and randn, which generate
matrices of (pseudo-)random numbers using the same syntax as eye.

In addition, matrices can be constructed in a block form. With C defined by C = [1
2; 3 4], we may create a matrix D as follows

>> D = [C zeros(2); ones(2) eye(2)]

D =

1 2 0 0

3 4 0 0

1 1 1 0

1 1 0 1

2.5.14 Special matrices

MATLAB provides a number of special matrices (see Table 2.5). These matrices have inter-
esting properties that make them useful for constructing examples and for testing algorithms.
For more information, see MATLAB documentation.

Table 2.5: Special matrices

hilb Hilbert matrix
invhilb Inverse Hilbert matrix
magic Magic square
pascal Pascal matrix
toeplitz Toeplitz matrix
vander Vandermonde matrix
wilkinson Wilkinson’s eigenvalue test matrix

28

Page 73

• MATLAB is interpreted (not compiled), errors are easy to fix
• MATLAB is optimized to be relatively fast when performing matrix operations
• MATLAB does have some object-oriented elements

C.3 Weaknesses

• MATLAB is not a general purpose programming language such as C, C++, or FOR-
TRAN

• MATLAB is designed for scientific computing, and is not well suitable for other appli-
cations

• MATLAB is an interpreted language, slower than a compiled language such as C++
• MATLAB commands are specific for MATLAB usage. Most of them do not have a

direct equivalent with other programming language commands

C.4 Competition

• One of MATLAB’s competitors is Mathematica, the symbolic computation program.
• MATLAB is more convenient for numerical analysis and linear algebra. It is frequently

used in engineering community.

• Mathematica has superior symbolic manipulation, making it popular among physicists.
• There are other competitors:

– Scilab

– GNU Octave

– Rlab

63

Page 74

Bibliography

[1] The MathWorks Inc. MATLAB 7.0 (R14SP2). The MathWorks Inc., 2005.

[2] S. J. Chapman. MATLAB Programming for Engineers. Thomson, 2004.

[3] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004.

[4] C. F. Van Loan. Introduction to Scientiflc Computing. Prentice Hall, 1997.

[5] D. J. Higham and N. J. Higham. MATLAB Guide. Siam, second edition edition, 2005.

[6] K. R. Coombes, B. R. Hunt, R. L. Lipsman, J. E. Osborn, and G. J. Stuck. Difierential
Equations with MATLAB. John Wiley and Sons, 2000.

[7] A. Gilat. MATLAB: An introduction with Applications. John Wiley and Sons, 2004.

[8] J. Cooper. A MATLAB Companion for Multivariable Calculus. Academic Press, 2001.

[9] J. C. Polking and D. Arnold. ODE using MATLAB. Prentice Hall, 2004.

[10] D. Kahaner, C. Moler, and S. Nash. Numerical Methods and Software. Prentice-Hall,
1989.

[11] J. W. Demmel. Applied Numerical Linear Algebra. Siam, 1997.

[12] D. Houcque. Applications of MATLAB: Ordinary Differential Equations. Internal
communication, Northwestern University, pages 1–12, 2005.

64

Similer Documents