introduction to matlab and matlab programming

87
Introduction to MATLAB and MATLAB Programming #1

Upload: tallis

Post on 23-Feb-2016

288 views

Category:

Documents


11 download

DESCRIPTION

Introduction to MATLAB and MATLAB Programming. #1. Outline. Who are we and why are we here? (to paraphrase James Stockdale, 1992) Who am I and how did I get here? Who are you and why are you here? MATLAB. Started programming (BASIC) in 1981 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to MATLAB and MATLAB Programming

Introduction to MATLAB and MATLAB Programming

#1

Page 2: Introduction to MATLAB and MATLAB Programming

Outline

• Who are we and why are we here? (to paraphrase James Stockdale, 1992)

• Who am I and how did I get here?• Who are you and why are you here?• MATLAB

Page 3: Introduction to MATLAB and MATLAB Programming
Page 4: Introduction to MATLAB and MATLAB Programming
Page 5: Introduction to MATLAB and MATLAB Programming
Page 6: Introduction to MATLAB and MATLAB Programming

• Started programming (BASIC) in 1981• Started imaging algorithm and software

development (Pascal) in 1983• Cambridge Instruments Quantimet 900– 1 MB memory– Booted from 8” floppy disk– $100,000

Page 7: Introduction to MATLAB and MATLAB Programming
Page 8: Introduction to MATLAB and MATLAB Programming

• BCC ASET (1983), AA (1986)– BASIC, Pascal, FORTRAN, x86 Assembler

• TSU BSEE (1986)• NCSU MSEE (1988)– MATLAB– C/C++– APL, ProLog

• NCSU PhD (1996)

Page 9: Introduction to MATLAB and MATLAB Programming

3D ICUS

• 3D Intracoronary Ultrasound • Lumen and medial-adventitial boundary

estimation

Page 10: Introduction to MATLAB and MATLAB Programming

3D Imaging Geometry

C C

u

v

uv

1

~ vu

x

1

~ vu

x

1

~

w

w

w

zyx

X

wz

wx

wy

Page 11: Introduction to MATLAB and MATLAB Programming

TIPS

Page 12: Introduction to MATLAB and MATLAB Programming

Surgical Planning

Page 13: Introduction to MATLAB and MATLAB Programming

MRA Vessel Extraction

Page 14: Introduction to MATLAB and MATLAB Programming

Objectives

• Develop a basic working knowledge of MATLAB (notation, syntax, built-in functions)

• Learn to use the command line (MATLAB as a calculator)

• Learn to develop scripts and functions• Learn basic programming skills• Learn to use the debugger to troubleshoot

scripts and functions

Page 15: Introduction to MATLAB and MATLAB Programming

Objectives

• Generate 2D and 3D data plots• Read, write, and manipulate numeric data

(e.g. text files, binary files, Excel)• Read, write, manipulate, and display image

data• Learn the basics of linear algebra• Learn about the FFT and complex variables

Page 16: Introduction to MATLAB and MATLAB Programming

What is MATLAB?

• MATLAB - Matrix Laboratory• What is a matrix?– Fundamental data type in MATLAB– An image• What is an image?

– A rectangular array of numbers– A point in a high-dimensional space

– A representation of a system of equations

Page 17: Introduction to MATLAB and MATLAB Programming

What is MATLAB?

• What is a matrix?– A linear mapping from Rn to Rm (or Cn to Cm)** • What’s a linear mapping?• Lines map to lines. Origin maps to the origin.

– A collection of vectors– What is a vector?

» Magnitude and direction (velocity, force, etc.)» An n-tuple of numbers

Page 18: Introduction to MATLAB and MATLAB Programming

What is MATLAB?

• History– Netlib (netlib.org)

• Large software library written in FORTRAN (FORmula TRANslator)

– Cleve Moler (EISPACK, LINPACK, cofounder of MathWorks)

• Computing environment for technical computation, visualization, design, simulation, and implementation in a wide range of application areas.

Page 19: Introduction to MATLAB and MATLAB Programming

Common MATLAB Applications

• Numerical Methods– Linear algebraic equations– Roots and optimization– Curve fitting– Integration and differentiation– Differential equations

• MATLAB toolboxes– Statistics, optimization, image processing,

computer vision, bioinformatics, …

Page 20: Introduction to MATLAB and MATLAB Programming

Why MATLAB?

• Pros:– Easy to start using MATLAB with little or no prior

programming experience– Relatively easy to learn– Can be used as a calculator or as a programming

language– Can be used to test simple algorithms (command

line or scripts) prior to coding in a higher-level language

Page 21: Introduction to MATLAB and MATLAB Programming

Why MATLAB?

• Pros:– MATLAB has a large library of optimized, robust

mathematical functions– MATLAB has a large collection of very powerful

toolboxes (e.g. image processing, statistics)– Easy to produce 2D and 3D graphs

Page 22: Introduction to MATLAB and MATLAB Programming

Why Not MATLAB?

• Cons:– Cost associated with ease of use• Interpreted language• Slow (10-100x) compared to some other programming

languages (e.g. C, C++, FORTRAN, Pascal)• May be too slow to solve large problems

– Fairly expensive (free alternatives available)– Designed for scientific computing• Not always the best solution for other applications

Page 23: Introduction to MATLAB and MATLAB Programming

2-by-2 System of Equations

• Can solve graphically• Can add/subtract equations

40

x yx y

2 42

xx

2 42

yy

Page 24: Introduction to MATLAB and MATLAB Programming

2-by-2 System of Equations

• Can solve with the cross or outer product– cross ()

1 1 4 22

1 1 4 22

4 0 2 1

Page 25: Introduction to MATLAB and MATLAB Programming

n-by-n System of Equations

• What about higher dimensions? • Cross product only works in 3D.• Harder (impossible?) to visualize.– Circuit analysis example • Graph theory (non-planar) and linear algebra• 6-by-6 system of equations

Page 26: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• The entry (or element) in row i and row j of a matrix, A, is denoted by aij

• The entry in row i of a vector (i.e. matrix with only one column) v is denoted by vi.

11 12 13 14

21 22 23 24

31 32 33 34

a a a aa a a aa a a a

A

1

2

3

4

vvvv

v

Page 27: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Two matrices are equal if: – They are the same size– All corresponding entries are equal

• Two matrices can be added together if they have the same size (i.e. the same number of rows and columns)

ij ij ijc a b C = A+B

Page 28: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Multiplication by a scalar

• Matrix-vector multiplication

– If A is m-by-n and x is n-by-1, b is m-by-1.

11 12 11 12

21 22 21 22

a a ca cac c

a a ca ca

A

1

n

i ij jj

b a x

b Ax

Page 29: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Matrix-vector multiplication

111 12 13 14 11 1 12 2 13 3 14 4 1

221 22 23 24 21 1 22 2 23 3 24 4 2

331 32 33 34 31 1 32 2 33 3 34 4 3

4

xa a a a a x a x a x a x b

xa a a a a x a x a x a x b

xa a a a a x a x a x a x b

x

Ax = b

Page 30: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Matrix-matrix multiplication

– If A is m-by-r and B is r-by-n, C is m-by-n.1

n

ij ik kjk

c a b

C AB

Page 31: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Matrix-matrix multiplication– To find cij (i.e. the element of C in row i and

column j) multiply corresponding entries in row i of A and column j of B, and add them together.

11 12 11 12 11 11 12 21 11 12 12 22 11 12

21 22 21 22 21 12 22 21 21 12 22 22 21 22

a a b b a b a b a b a b c ca a b b a b a b a b a b c c

C AB

Page 32: Introduction to MATLAB and MATLAB Programming

Matrix Operations

A B B AA+ B+C = A+B +C

A BC = AB C

A B+C = AB+AC

B+C A = BA+CAAB BA

Page 33: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Zero matrix– A matrix where each element is 0– zeros()

4

0 0 0 00 0 0 00 0 0 00 0 0 0

0

Page 34: Introduction to MATLAB and MATLAB Programming

Matrix Operations

• Identity matrix– Square (i.e. m=n)– Diagonal entries equal to 1; all others are 0.– Multiplication by the identity matrix doesn’t

change the matrix– eye()

4

1 0 0 00 1 0 00 0 1 00 0 0 1

I

Page 35: Introduction to MATLAB and MATLAB Programming

Row Reduction

• Elementary row operations– Multiply a row by a scalar.– Add a scalar multiple of one row to another.– Exchange two rows.

• Solve Ax=b– Row reduction with augmented matrix

11 12 13 1 1

21 22 23 2 2

31 32 33 3 3

1 0 00 1 00 0 1

a a a b xa a a b xa a a b x

Page 36: Introduction to MATLAB and MATLAB Programming

2-by-2 cont’d

• Back to 2-by-2 example– Rewrite as a matrix equation

– Form an augmented matrix

1 1 41 1 0

xy

40

x yx y

1 1 41 1 0

Page 37: Introduction to MATLAB and MATLAB Programming

2-by-2 cont’d

• Solve by row reduction– Make left side look like an identity matrix using

only elementary row operations

1 1 40 1 2

1 0 20 1 2

1 1 41 1 0

1 1 40 2 4

Page 38: Introduction to MATLAB and MATLAB Programming

Rank

• r=rank(A)• Number of leading 1’s in the row-reduced

matrix• From previous example, rank(A)=2

1 1 4 1 0 21 1 0 0 1 2

Page 39: Introduction to MATLAB and MATLAB Programming

Matrix Inverse

• Matrix inverse– A must be square and full rank– If a matrix can be found such that AB=In, then B is

called the inverse of A.

Page 40: Introduction to MATLAB and MATLAB Programming

2-by-2 cont’d

• Computing the inverse

1

0.5 0.5 4 20.5 0.5 0 2

x A b

-1 A I I A

1 1 1 01 1 0 1

1 1 1 00 2 1 1

1 1 1 00 1 0.5 0.5

1 0 0.5 0.50 1 0.5 0.5

Page 41: Introduction to MATLAB and MATLAB Programming

Matrix Inverse

• If A is n-by-n and rank(A)=n, then A-1exists.– Implies there is a solution for every b• b is in the range of A

– inv()• If A and B are both invertible,

11

1 1 1

A A

AB B A

Page 42: Introduction to MATLAB and MATLAB Programming

2-by-2 cont’d

• Inconsistent (no solution)– n=2, Rank=1– Inverse doesn’t exist

1 1 41 1 5

1 1 40 0 1

xy

45

x yx y

Page 43: Introduction to MATLAB and MATLAB Programming

2-by-2 cont’d

• Consistent (infinite solutions)– n=2, Rank=1– Inverse doesn’t exist

1 1 42 2 8

1 1 40 0 0

xy

42 2 8x yx y

Page 44: Introduction to MATLAB and MATLAB Programming

Matrix Transpose

• Transpose (‘ in MATLAB)– Swap rows and columns

TT

T T T

T T

T T T

c c

A A

A B A +B

A A

AB B A

T

ij jib a

B A

1 21 3 5

3 42 4 6

5 6

T

Page 45: Introduction to MATLAB and MATLAB Programming

Scalar Product

• Also called inner or dot product– Vectors must have the same length (size)– Multiply corresponding elements and add

1 1 2 21

n

i i n ni

u v u v u v u v

u v =

Page 46: Introduction to MATLAB and MATLAB Programming

Scalar Product

• Inner (dot) product

– Also written as: cosT u v u v u v

1 1

2 2 1 1 2 2 3 3

3 3

T

u vu v u v u v u vu v

u = v u v

Page 47: Introduction to MATLAB and MATLAB Programming

Scalar Product

• Properties

0 0k k k

u v v uu v w u v u w

u v u v u vv v v v v = 0

Page 48: Introduction to MATLAB and MATLAB Programming

Norms

• Vector norms– 1-norm

– 2-norm (Euclidean norm or length)

– Infinity-norm

11

n

ii

x

x

1 2max , , , nx x x

x

22

1

nT

ii

x

x x x

Page 49: Introduction to MATLAB and MATLAB Programming

Norms

• Matrix Norm– 1-norm (column sum)

– 2-norm – later

– Infinity norm (row sum)

– Frobenius norm

1 1 1

maxm

ijj n i

a

A

1 1

maxn

iji m j

a

A

2

1 1

m n

ijFi j

a

A

Page 50: Introduction to MATLAB and MATLAB Programming

Norms

• Vector norm properties

• Matrix norm properties0

A

A A

A+B A B

AB A B

0 0

x x x 0

x x

x + y x y

Page 51: Introduction to MATLAB and MATLAB Programming

Vector Product

• Also called the cross product– 3D only

2 3 3 2

3 1 1 3

1 2 2 1

u v -u vu v u v -u v

u v -u v

Page 52: Introduction to MATLAB and MATLAB Programming

Vector Product

• Properties

k k k

u

u v u vu v w u v u wu v w u w v wu v u v u v

u 0 0 0u u 0

Page 53: Introduction to MATLAB and MATLAB Programming

Unit Vectors

• Unit vector

• Projections

ˆ

ˆ 1

vuv

u

2

T

u vw vv

Page 54: Introduction to MATLAB and MATLAB Programming

Noise

• Always present• Quantization noise• Finite word length (data type differences (intn,

uintn, float, double)– Catastrophic cancellation

• Electronic noise, photon noise, etc.• When is it a problem? Depends on the problem.• Be aware of effects of noise!

Page 55: Introduction to MATLAB and MATLAB Programming

Ill-Conditioning

2 42 3.999 7.999x yx y

1 2 42 3.999 7.999

xy

Page 56: Introduction to MATLAB and MATLAB Programming

Ill-Conditioning

• Results are sensitive to noise• Loss of precision• cond() – condition number

2 4.0012 3.999 7.998x yx y

1.001 2.001 42.001 3.998 7.999

x yx y

Page 57: Introduction to MATLAB and MATLAB Programming

Ill-Conditioning

• Trefethen least-squares ill-conditioned example (14th-order polynomial fit)

• Solution by different methods– \ - matrix divide– inv() – matrix inverse– LU() – LU decomposition– QR() – QR decomposition– SVD() – Singular value decomposition

Page 58: Introduction to MATLAB and MATLAB Programming

MATLAB Workspace

• Using MATLAB as a calculator• Command window• Command history• Current directory• Workspace

Page 59: Introduction to MATLAB and MATLAB Programming

MATLAB Workspace

• Help browser• diary()• Saving/loading the workspace• Quitting MATLAB

Page 60: Introduction to MATLAB and MATLAB Programming

Variables, Assignments, and Keywords

• Datatypes - scalars, vectors ([1, 2, 3, 4], [1:4]), matrices ([m,n], [1, 2; 3, 4]), uintn, intn, double, char, structs, cells, classes

• Vector – magnitude and direction, n-tuple• Matrix – mapping from Rn to Rm (or Cn to Cm),

collection of vectors, representation of an image, representation of a system of equations

Page 61: Introduction to MATLAB and MATLAB Programming

Variables, Assignments, and Keywords

• Entering variables, vectors and matrices• Incrementation and overwriting of variables• Recalling expressions and making corrections• Addressing vector and matrix elements – v(m:n), v(m:step:n), v([1 3 5 2 4 6])– A(:,j), A(i,:), A(i,j:end)

Page 62: Introduction to MATLAB and MATLAB Programming

Matrix Properties

• size(), length()• Manipulating vectors and matrices• reshape(), transpose operator(‘)• Transpose algebra • Inverse algebra• Row/column vectors• Matrices from vectors• Semicolons – index generator• Vector and matrix norms (1, 2, infinity, Frobenius)

Page 63: Introduction to MATLAB and MATLAB Programming

Special Matrices

• Square, symmetric, skew-symmetric, diagonal, identity, upper/lower triangular, banded or Toeplitz, circulant, Vandermonde

• eye(), zeros(), ones(), diag(), toeplitz(), rand(), randn(), etc.

Page 64: Introduction to MATLAB and MATLAB Programming

Notation and Operators

• Colon notation• Indexing• Conformance• Expressions, operators (normal and element-

wise), and operator precedence (+, -, *, /, ^, :, ;, ,, …, %, ‘, =, (), [], .+, .-, .*, ./, .^, .\, .’, < <=, >, >=, ==, ~=, &, |, &&, ||)

• Operator Precedence

Page 65: Introduction to MATLAB and MATLAB Programming

Built-In Functions

• cd, clc, clear, clear x, dir, pwd, exist, type, who, which

• linspace• disp, length, ndims, numel, size• cross, diag, dot, end, kron, max, min, prod,

reshape, sort, sum, size• det, inv, linsolve, lu, norm, null, orth, rank, rref,

trace• cond – condition number and loss of precision

Page 66: Introduction to MATLAB and MATLAB Programming

Operators

• Calculations with vectors and matrices (+, -, *, ^, .*, .^)

• Operators and element-wise operators• Solving linear systems (inv, LU, QR, SVD,

normal equations, pseudo-inverse)• Inconsistent• Consistent, full rank• Consistent, rank-deficient

Page 67: Introduction to MATLAB and MATLAB Programming

Operators

• Least squares fitting• Robust fitting• Matrix inverse• Matrix functions (det, diag, eig, inv, norm,

rank)

Page 68: Introduction to MATLAB and MATLAB Programming

Special Variables

• ans, eps, i, j, NaN, pi, Inf, and –Inf

Page 69: Introduction to MATLAB and MATLAB Programming

Programming• Writing your own functions (implies input and output

arguments, variables in local workspace)• Functions

– [output_argument_list]=function function_name(input_argument_list)

• Scripts (implies no input or output arguments, variables in global workspace)– Variables in the global workspace may be overwritten– Script execution can be affected by variables in the global

workspace– Advisable to use functions for large and/or complicated applications

Page 70: Introduction to MATLAB and MATLAB Programming

Programming cont’d

• Comments (%)• Recursion• Pretty print• Operation types• Sequential (commands executed in order)• Conditional (if-then-else, switch-case)• Iterative (for loops, do loops)• Structured programing• Reusable code

Page 71: Introduction to MATLAB and MATLAB Programming

Programming - Flow Control

• Looping– for-end– Indexing (also reverse indexing)– while-end– break– continue– return

Page 72: Introduction to MATLAB and MATLAB Programming

Programming - Flow Control

• Conditional statements– if-elseif-else-end– Relational operators (>, <, >=, <=, ==, ~=, &, |, ~)– Boolean algebra (truth tables, Karnaugh maps,

DeMorgan’s Law) – bitor, bitand, bitxor, xor, and, or, not– switch-case-end

Page 73: Introduction to MATLAB and MATLAB Programming

Debugging• Breakpoints• Conditional breakpoints• Step into• Step over• Return• Examining variables• dbquit• disp()• End debugging from menu• Function keys

Page 74: Introduction to MATLAB and MATLAB Programming

Help System

• Help command• Product Help

Page 75: Introduction to MATLAB and MATLAB Programming

Editor

• Editor window• Pretty print (smart indent, ctrl-i)• Comments - % (ctrl-r, ctrl-t)

Page 76: Introduction to MATLAB and MATLAB Programming

I/O – Plotting and Graphics

• 2D/3D Plots• Plotting points and lines• plot(), plot3(), surf(), mesh()• Line color and style (color, linestyle)• Marker color and style

Page 77: Introduction to MATLAB and MATLAB Programming

General I/O

• To/from screen and files• Binary and text file I/O• format (format long, format short, etc.)• Numerical format• load, save, fopen, fscanf, fprintf, sprintf, fclose

Page 78: Introduction to MATLAB and MATLAB Programming

Image I/O

• imread, imwrite, imshow, imagesc, colormap• Image types• Image manipulation• Image display (imshow, imscale)

Page 79: Introduction to MATLAB and MATLAB Programming

Fourier

• Complex variables• Complex algebra• Complex exponential• Fourier series• Fourier transform• Fast Fourier transform• Sampling

Page 80: Introduction to MATLAB and MATLAB Programming

GUI Development

• Event-driven programming• Design and implementation

Page 81: Introduction to MATLAB and MATLAB Programming

Matrix Decompositions

• LU, Cholesky, QR, SVD• Condition number, accuracy, FLOPS

Page 82: Introduction to MATLAB and MATLAB Programming

Equation Solving

• Matrix division• inv(), \, /• Pseudo-inverse• LU decomposition• QR decomposition• SVD• Operation counts versus robustness

Page 83: Introduction to MATLAB and MATLAB Programming

Least-Squares

• Normal equations• Pseudo-inverse solution• Ill-conditioning• SVD solution• Camera calibration• 3D measurement

Page 84: Introduction to MATLAB and MATLAB Programming

Eigenvalues

• eig()

Page 85: Introduction to MATLAB and MATLAB Programming

Performance

• Vectorization• Preallocation of variables• Timing (tic, toc)• Profiler

Page 86: Introduction to MATLAB and MATLAB Programming

Toolbox Overview

• Signal Processing• Image Processing• Statistics• Optimization• Parallel Processing

Page 87: Introduction to MATLAB and MATLAB Programming

Image Processing and Image Analysis

• Morphology• Filtering• Convolution