1 basic matlab ผศ. ดร. อนันต์ ผลเพิ่ม anan phonphoem anan...

25
1 Basic MATLAB ผผ.ผผ.ผผผผผผ ผผผผผผผ Anan Phonphoem http://www.cpe.ku.ac.th/ ~anan [email protected]

Upload: conrad-tate

Post on 26-Dec-2015

252 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

1

Basic MATLAB

ผศ.ดร.อนั�นัต์ ผลเพิ่��มAnan Phonphoem

http://www.cpe.ku.ac.th/[email protected]

Page 2: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

2

Outline

History of MATLAB(modified from Prof. James Brucker’s slide)

Elements Operators

Page 3: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

3

Fortran and Scientific Computing

Engineering and scientific need numerical computing In the past, main language was FORTRAN

First "high level" programming language Here's a Fortran code to solve a x2 + b x + c = 0:

C Solve a quadratic equation (this is a comment). DESC = B*B - 4*A*C IF ( DESC .LT. 0.0 ) GOTO 10 DESC = SQRT(DESC) X1 = (-B + DESC)/(2.0*A) X2 = (-B - DESC)/(2.0*A) WRITE(6,*) "SOLUTIONS ARE ",X1," AND ", X2 RETURN 10 WRITE(6,*) "EQUATION HAS COMPLEX ROOTS" RETURN

Page 4: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

4

Problems using FORTRAN

loss of precision and inaccurate results:X = 0.1Y = 1.0 - 10*X Y "should" equal 0, but probably does not!

underflow and overflow: X = 1.0E20, X*X --> too big!

efficient coding of algorithms not always obvious DO 10 N=1,100000 10 Y(N) = SQRT(2.0)*X(N) <-- inefficient!

programming errors!

Page 5: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

5

Solving a Linear System in Fortran

From equation: b = A*x, solve for x It doesn't check for degeneracy or zeros.C Solve B = A*X for X. C N is dimension of vectors and matrixC Does not use row interchange, scaling. SUBROUTINE LINSYS(N, A, X, B, TMP) INTEGER N DOUBLE PRECISION A(N,N), X(N), B(N) DOUBLE PRECISION TMP(N), RATIOC... Forward elimination DO 13 J=1,N-1 DO 12 I=J+1,N RATIO = -A(I,J)/A(J,J) A(I,*) = A(I,*) +RATIO*ROW(J,*) DO 11 K=J+1,N 11 A(I,K) = A(I,K) + RATIO*A(J,K) A(I,J) = 0.0 X(I) = X(I) + RATIO*X(J) 12 CONTINUE 11 CONTINUE Continued...

C... Backwards substitution X(N) = X(N)/A(N,N) DO 21 I=N-1,1,-1 TMP = X(I) DO 20 J=I+1,N 20 TMP = TMP - A(I,J)*X(J) X(I) = TMP/A(I,I) 21 CONTINUE RETURN END

This is just a small example.

A full program may be 1000's of lines long.

Page 6: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

6

Need for Numerical Libraries

The U.S. government notices that many engineers write the same algorithms.

Need a good quality algorithms for common tasks.

Make it as "libraries" of subroutines Libraries are available at: www.netlib.org

Page 7: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

7

Examples of Numerical Libraries

BLAS (Basic Linear Algebra Subroutines): Vector operation (adding vectors, dot product)

LINPACK: linear algebra subroutines Vector-matrix operations (factoring, inverting a matrix) replaced by LAPACK.

EISPACK: compute eigenvalues and eigenvectors of matrices.

Example: solve A*x = b using LINPACKC.... factor the A matrix CALL SGEFA(A, N, N, IPVT, INFO)C.... copy B vector into X vector CALL SCOPY(N, B, 1, X, 1)C.... solve the system of equations CALL SGESL(A, N, N, IPVT, X, 0)

Page 8: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

8

Still Not Easy Enough!

Cleve Moler (mathematician, C.S. Professor, Co-author of LINPACK) thought this is still too much work: write FORTRAN, compile, debug, compile, run...

He wrote MATLAB ("Matrix Laboratory"). interactive easy input, output operations on a whole vector or matrix at once

Example: solve b = A*x in Matlab...x = A \ b

Page 9: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

9

Immediate Popularity!

MATLAB quickly became quite popular and used for both teaching and research. It was also free.

An engineer, Jack Little, saw Matlab during a lecture by Cleve Moler at Stanford University.

He saw the commercial potential and (with permission) rewrote Matlab in C added "M-files" (stored programs) many new features and libraries founded The Mathworks to market it.

Page 10: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

10

Software principles...

Matlab illustrates some useful design concepts for software.

FORTRAN Compiler

Linear Algebra Libraries

Matlab

Matlab "Toolkits"Matlab "M-Files"

Standard base platform

Modular, reusable software components

Extensible using "Toolkits" or user-contributed programs called M-files.

Interactive user interface; hides boring details

Page 11: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

11

Matlab Today

Millions of users! A standard tool in both professional and academic use "Toolboxes" providing functions for many

applications: control systems identification neural networks bio-informatics statistics and time-series analysis

Can do symbolic mathematics, too. Simulink: GUI based simulation tool

Page 12: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

12

Pascal Programming

Program Calculate;{ This program calculates how much you need to pay }const

Unit_Price = 20;var

cost, total : integer;begin

write (‘How many pieces do you want ? ’);readln(total); { read total number that you want }

cost := total * Unit_Price;writeln (‘It costs ’,

cost, ‘ Baht’);end.

Page 13: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

13

MATLAB Programming

% Basic Matrix Operations% Show how easy MATLAB isa = [1 2 3 4 6 4 3 4 5];b = a + 2;plot(b);grid on;xlabel('Sample #');ylabel('Pounds');

Page 14: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

14

Outline

History of MATLAB(modified from Prof. James Brucker’s slide)

Elements Operators

Page 15: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

15

Elements

Letter (A…Z, a…z) Digit (0…9) Special character (+ - * / _ = ! <> []{}) Number (1, 1.1, -1.1212, 23e+2) String (‘Hello’, ‘Today is Monday’, etc.) Matrix and Vector Operator

Page 16: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

16

Matrix

1 2 3 4 2 3 4 9 12 0 2 7

row 1row 2row 3

col 1 col 2 col 3col 4

4 x 3 Matrix

Scalar

Page 17: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

17

Vector

1 9 0 3

Vector A4 X 1

13 -2 8

Vector B1 X 3

Page 18: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

18

Symbols

Symbol Description

. Decimal point

.. Parent directory

... Continuation

( ) Parentheses and subscripting

[ ] Brackets

{ } Braces and subscripting

: Colon

, Separator

; Semicolon

% Comment

Page 19: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

19

Operators

Arithmetic operators Relational operators Logical operators

Page 20: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

20

Arithmetic OperatorsOperator Description Example

+ Plus S + T

+ Unary plus +3

- Minus S – T

- Unary minus -C

* Matrix Multiply S * T

.* Array Multiply (member) S .* T

/ Left Matrix Divide (S* INV(T))

S / T

./ Left Array Divide S ./ T

\ Right Matrix Divide

(INV(S *) T) S \ T

.\ Right Array Divide S .\ T

^ Matrix Power S ^ 2

.^ Array Power S .^ 3

Page 21: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

21

Relational Operators

Operator Description Example

== Equal S == T

~= Not Equal S ~= T

< Less than S < T

> Greater than S > T

<= Less than or equal S <= T

>= Greater than or equal S >= T

Page 22: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

22

Logical Operators

Operator Description Example

&& Logical AND S && T

|| Logical OR S || T

& Element-wise logical AND S & T

| Element-wise logical OR S | T

~ Logical NOT ~S

Page 23: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

23

Arithmetic Expression

45.00 (width + length) (12*(top – bottom)) 4 – 2 = ? 2+15/3*2 = ? (2+15)/(3*2) (2 + (15/3)) * 2 2 + ((15/3) * 2)

Page 24: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

24

Precedence rules for arithmetic operators

1. ( ) parentheses2. Unary + and –3. *, /4. + –5. If equal precedence, left to rightExamples

-a+k/-w = (-a) + (k / (-w))

C*23/6+23*2 = ((C*23)/6) + (23*2)

Page 25: 1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

25

Assignment statement

A = 12 C = A + B Ans = width + length Result = 2+15/3*2