Basic MATLAB - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Basic MATLAB

Description:

Fortran and Scientific Computing. Engineering and scientific need ... BLAS (Basic Linear Algebra Subroutines): Vector operation (adding vectors, dot product) ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 26
Provided by: ANAN54
Category:
Tags: matlab | basic | blas

less

Transcript and Presenter's Notes

Title: Basic MATLAB


1
Basic MATLAB
  • ??.??.?????? ???????
  • Anan Phonphoem
  • http//www.cpe.ku.ac.th/anan
  • anan_at_cpe.ku.ac.th

2
Outline
  • History of MATLAB
  • (modified from Prof. James Bruckers slide)
  • Elements
  • Operators

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 BB - 4AC IF (
DESC .LT. 0.0 ) GOTO 10 DESC
SQRT(DESC) X1 (-B DESC)/(2.0A)
X2 (-B - DESC)/(2.0A) WRITE(6,)
"SOLUTIONS ARE ",X1," AND ", X2 RETURN
10 WRITE(6,) "EQUATION HAS COMPLEX ROOTS"
RETURN
4
Problems using FORTRAN
  • loss of precision and inaccurate resultsX
    0.1Y 1.0 - 10X
  • Y "should" equal 0, but probably does not!
  • underflow and overflow X 1.0E20, XX --gt too
    big!
  • efficient coding of algorithms not always
    obvious DO 10 N1,100000 10 Y(N)
    SQRT(2.0)X(N) lt-- inefficient!
  • programming errors!

5
Solving a Linear System in Fortran
  • From equation b Ax, solve for x
  • It doesn't check for degeneracy or zeros.

C Solve B AX 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 J1,N-1 DO 12 IJ1,N
RATIO -A(I,J)/A(J,J) A(I,) A(I,)
RATIOROW(J,) DO 11 KJ1,N 11
A(I,K) A(I,K) RATIOA(J,K) A(I,J)
0.0 X(I) X(I) RATIOX(J) 12
CONTINUE 11 CONTINUE Continued...
C... Backwards substitution X(N)
X(N)/A(N,N) DO 21 IN-1,1,-1 TMP
X(I) DO 20 JI1,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.
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

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 Ax 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)
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 Ax in Matlab...
  • x A \ b

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.

10
Software principles...
  • Matlab illustrates some useful design concepts
    for software.

Extensible using "Toolkits" or user-contributed
programs called M-files.
Matlab "Toolkits"
Matlab "M-Files"
Interactive user interface hides boring details
Matlab
Linear Algebra Libraries
Modular, reusable software components
FORTRAN Compiler
Standard base platform
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

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.

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

14
Outline
  • History of MATLAB
  • (modified from Prof. James Bruckers slide)
  • Elements
  • Operators

15
Elements
  • Letter (AZ, az)
  • Digit (09)
  • Special character ( - / _ ! ltgt )
  • Number (1, 1.1, -1.1212, 23e2)
  • String (Hello, Today is Monday, etc.)
  • Matrix and Vector
  • Operator

16
Matrix
4 x 3 Matrix
17
Vector
18
Symbols
Symbol Description
. Decimal point
.. Parent directory
... Continuation
( ) Parentheses and subscripting
Brackets
Braces and subscripting
Colon
, Separator
Semicolon
Comment
19
Operators
  • Arithmetic operators
  • Relational operators
  • Logical operators

20
Arithmetic Operators
Operator 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 (SINV(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
21
Relational Operators
Operator Description Example
Equal S T
Not Equal S T
lt Less than S lt T
gt Greater than S gt T
lt Less than or equal S lt T
gt Greater than or equal S gt T
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
23
Arithmetic Expression
  • 45.00
  • (width length)
  • (12(top bottom))
  • 4 2 ?
  • 215/32 ?
  • (215)/(32)
  • (2 (15/3)) 2
  • 2 ((15/3) 2)

24
Precedence rules for arithmetic operators
  • ( ) parentheses
  • Unary and
  • , /
  • If equal precedence, left to right
  • Examples

-ak/-w (-a) (k / (-w)) C23/6232
((C23)/6) (232)
25
Assignment statement
  • A 12
  • C A B
  • Ans width length
  • Result 215/32
Write a Comment
User Comments (0)
About PowerShow.com