Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem"— Presentation transcript:

1

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

3 2 Outline History of MATLAB (modified from Prof. James Brucker ’ s slide) Elements Operators

4 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 x 2 + 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

5 4 Problems using FORTRAN loss of precision and inaccurate results: X = 0.1 Y = 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!

6 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 matrix C 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), RATIO C... 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.

7 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

8 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 LINPACK C.... 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)

9 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

10 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.

11 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

12 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

13 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.

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

15 14 Outline History of MATLAB (modified from Prof. James Brucker ’ s slide) Elements Operators

16 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

17 16 Matrix 1 2 3 4 2 3 4 9 12 0 2 7 row 1 row 2 row 3 col 1col 2col 3 col 4 4 x 3 Matrix Scalar

18 17 Vector 1 9 0 3 Vector A 4 X 1 13 -2 8 Vector B 1 X 3

19 18 Symbols SymbolDescription.Decimal point..Parent directory...Continuation ( )Parentheses and subscripting [ ]Brackets { }Braces and subscripting :Colon,Separator ;Semicolon %Comment

20 19 Operators Arithmetic operators Relational operators Logical operators

21 20 Arithmetic Operators OperatorDescriptionExample +Plus S + T +Unary plus+3 -MinusS – T -Unary minus-C *Matrix MultiplyS * T.*Array Multiply (member)S.* T /Left Matrix Divide (S*INV(T))S / T./Left Array DivideS./ T \ Right Matrix Divide (INV(S)*T) S \ T.\Right Array DivideS.\ T ^Matrix PowerS ^ 2.^Array PowerS.^ 3

22 21 Relational Operators OperatorDescriptionExample ==Equal S == T ~=Not EqualS ~= T <Less thanS < T >Greater thanS > T <=Less than or equalS <= T >=Greater than or equalS >= T

23 22 Logical Operators OperatorDescriptionExample &&Logical AND S && T ||Logical ORS || T &Element-wise logical ANDS & T |Element-wise logical ORS | T ~Logical NOT~S

24 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)

25 24 Precedence rules for arithmetic operators 1.( ) parentheses 2.Unary + and – 3.*, / 4.+ – 5.If equal precedence, left to right Examples -a+k/-w = (-a) + (k / (-w)) C*23/6+23*2 = ((C*23)/6) + (23*2)

26 25 Assignment statement A = 12 C = A + B Ans = width + length Result = 2+15/3*2


Download ppt "1 Basic MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem"

Similar presentations


Ads by Google