matlab lecture two (part ii) thursday/friday, 3 - 4 july 2003

31
MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Upload: mae-harris

Post on 17-Dec-2015

223 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

MATLAB

Lecture Two (Part II)Thursday/Friday,

3 - 4 July 2003

Page 2: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Chapter 5

Applications

Page 3: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Linear Algebra

Solving a linear system5x = 3y - 2z + 108y + 4z = 3x +202x + 4y - 9z = 9

Cast in standard matrix form A x = b

Page 4: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Linear Equations

A = [5 -3 2; -3 8 4; 2 4 -9];b = [10; 20; 9];x = A \ b

Check solutionc = A*x

c should be equal to b.

Page 5: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Gaussian Elimination

C = [ A b]; % augmented matrix

row reduced echelon formCr = rref(C);

Page 6: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Eigenvalues and Eigenvectors

The problem A v = v With pencil-paper, we must solve

fordet (A - ) = 0

then solve the linear equation MATLAB way

[V, D] = eig(A)

Page 7: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Matrix Factorizations

LU decomposition[L, U] = lu(A);

such that L*U = A, L is a lower triangular matrix, U is an upper triangular matrix.

Page 8: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Matrix Factorization

QR factorization[Q, R] = qr(A);

such that Q*R = A; Q is orthogonal matrix and R is upper triangular matrix.

Page 9: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Matrix factorization

Singular Value Decomposition[U, D, V] = svd(A);

such that UDV = A, where U and V are orthogonal and D is diagonal matrix.

Page 10: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Sparse Matrix

Seehelp sparfun

for detail

Page 11: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Curve Fitting

Polynomial curve fittinga=polyfit(x,y,n)do a least-square fit with polynomial of degree n. x and y are data vectors, and a is coefficients of the polynomial, a = [an, an-1, …, a1, a0]

Page 12: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Curve Fitting

y = polyval(a, x) compute the value of polynomial

at value xy = a(1) xn + a(2) xn-1 + …

+ a(n) x + a(n+1) x can be a vector

Page 13: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Example-1: Straight-line fit:

Problem: Fit the set of data, and make a plot of it.

Step-1: find the coefficients Step-2: Evaluate y at finer scale Step-3: Plot and see

Page 14: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Interpolation

Find a curve that pass through all the points

ynew = interp1(x,y,xnew,method) method is a string. Possible

choices are 'nearest', 'linear', 'cubic', or 'spline'.

Page 15: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Data Analysis and Statistics

mean average value median half way std standard deviation max largest value min smallest value sum sum total prod product

Page 16: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Numerical Integration

Quad Adaptive Simpson's rule Quad8 Adaptive Newton-Cotes

Usequad('your_func', a, b);or quad('your_func',a, b, opt…)

Page 17: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Ordinary Differential Equations

General first-order ODEdx/dt = f(x, t)

where x and f are vectors MATLAB ODE solvers

ode23 2nd/3rd Runge-Kuttaode45 4/5th Runge-Kutta

Page 18: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Examples of ODE

Solve the first order differential equation

dx/dt = x + twith the initial condition x(0)=0.

Page 19: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

dx/dt = x + t, Example

function xdot = simpode(t,x)% SIMPODE: computes% xdot = x + t.xdot = x + t;

Page 20: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

dx/dt = x + t example

tspan = [0 2]; x0;[t, x] = ode23('simpode', tspan, x0);

plot(t, x)xlabel('t'), ylabel('x')

Page 21: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Second Order Equations

Nonlinear pendulumd2 /dt2 + 2 sin = 0

Convert into set of first-order ODE with z1 = , z2 = d/dt,

dz1/dt = z2,

dz2/dt = - 2 sin(z1)

Page 22: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Pendulum Function File

Function zdot = pend(t, z)%Inputs : t = time% z = [z(1); z(2)]%Outputs: zdot = [z(2); -w^2 sin (z1)]wsq = 1.56; % omega valuezdot = [z(2); - wsq*sin(z(1))];

Page 23: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Nonlinear Algebraic Equations

Finding zeros of equationf(x) = 0

MATLAB functionx_sol = fzero('your_func', x0, tol, trace);where tol and trace are optional arguments

Page 24: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Examples of Algebraic Equations

Solve transcendental equationsin x = exp(x) - 5

Define functionf(x) = sin(x) - exp(x) + 5

Page 25: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Chapter 6

Graphics

Page 26: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Simple Plotting

plot(x,y,'r') plot(x,y,':', x2,y2, '+') plot(x,y,'b--')

See Table on Chapter 6 for style options.

Page 27: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Other Plotting Commands

xlabel, ylabellabels on axis title title text text text in graphics legend legend axis axis limits/scale gtext text with local located by mouse

Page 28: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Specialized 2D Plots

semilogx log x vs y semilogy x vs log y loglog log x vs log y polar polar plot bar bar chart hist histogram pie pie plot

Page 29: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

3D Plots

plot3(x, y, z, 'style-option')

Example:t=linspace(0, 6*pi, 100);x=cos(t); y=sin(t); z = t;plot3(x,y,z)

Page 30: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Advanced Features

"Handle graphics" gives a better control of graphic output. It is a lower level graphics.

Page 31: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003

Chapter 8, What Else is There?

Symbolic Math and other Toolboxes

External Interface: Mex-files Graphics User Interface