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

Post on 17-Dec-2015

222 views 6 download

transcript

MATLAB

Lecture Two (Part II)Thursday/Friday,

3 - 4 July 2003

Chapter 5

Applications

Linear Algebra

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

Cast in standard matrix form A x = b

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.

Gaussian Elimination

C = [ A b]; % augmented matrix

row reduced echelon formCr = rref(C);

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)

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.

Matrix Factorization

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

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

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.

Sparse Matrix

Seehelp sparfun

for detail

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]

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

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

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

Data Analysis and Statistics

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

Numerical Integration

Quad Adaptive Simpson's rule Quad8 Adaptive Newton-Cotes

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

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

Examples of ODE

Solve the first order differential equation

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

dx/dt = x + t, Example

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

dx/dt = x + t example

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

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

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)

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))];

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

Examples of Algebraic Equations

Solve transcendental equationsin x = exp(x) - 5

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

Chapter 6

Graphics

Simple Plotting

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

See Table on Chapter 6 for style options.

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

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

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)

Advanced Features

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

Chapter 8, What Else is There?

Symbolic Math and other Toolboxes

External Interface: Mex-files Graphics User Interface