1 / 31

MATLAB

MATLAB. Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003. Chapter 5. Applications. Linear Algebra. Solving a linear system 5x = 3y - 2z + 10 8y + 4z = 3x +20 2x + 4y - 9z = 9 Cast in standard matrix form A x = b. Linear Equations. A = [5 -3 2; -3 8 4; 2 4 -9];

dava
Download Presentation

MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Chapter 5 Applications

  3. Linear Algebra • Solving a linear system 5x = 3y - 2z + 10 8y + 4z = 3x +20 2x + 4y - 9z = 9 • Cast in standard matrix form A x = b

  4. Linear Equations A = [5 -3 2; -3 8 4; 2 4 -9]; b = [10; 20; 9]; x = A \ b • Check solution c = A*x • c should be equal to b.

  5. Gaussian Elimination C = [ A b]; % augmented matrix • row reduced echelon form Cr = rref(C);

  6. Eigenvalues and Eigenvectors • The problem A v =  v • With pencil-paper, we must solve for det (A - ) = 0 then solve the linear equation • MATLAB way [V, D] = eig(A)

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

  8. Matrix Factorization • QR factorization [Q, R] = qr(A); such that Q*R = A; Q is orthogonal matrix and R is upper triangular matrix.

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

  10. Sparse Matrix • See help sparfun for detail

  11. Curve Fitting • Polynomial curve fitting a=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]

  12. Curve Fitting • y = polyval(a, x) • compute the value of polynomial at value x y = a(1) xn + a(2) xn-1 + … + a(n) x + a(n+1) • x can be a vector

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

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

  15. Data Analysis and Statistics • mean average value • median half way • std standard deviation • max largest value • min smallest value • sum sum total • prod product

  16. Numerical Integration • Quad Adaptive Simpson's rule • Quad8 Adaptive Newton-Cotes • Use quad('your_func', a, b); or quad('your_func',a, b, opt…)

  17. Ordinary Differential Equations • General first-order ODE dx/dt = f(x, t) where x and f are vectors • MATLAB ODE solvers ode23 2nd/3rd Runge-Kutta ode45 4/5th Runge-Kutta

  18. Examples of ODE • Solve the first order differential equation dx/dt = x + t with the initial condition x(0)=0.

  19. dx/dt = x + t, Example function xdot = simpode(t,x) % SIMPODE: computes % xdot = x + t. xdot = x + t;

  20. dx/dt = x + t example tspan = [0 2]; x0; [t, x] = ode23('simpode', tspan, x0); plot(t, x) xlabel('t'), ylabel('x')

  21. Second Order Equations • Nonlinear pendulum d2 /dt2 + 2 sin = 0 • Convert into set of first-order ODE with z1 = , z2 = d/dt, dz1/dt = z2, dz2/dt = - 2 sin(z1)

  22. 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 value zdot = [z(2); - wsq*sin(z(1))];

  23. Nonlinear Algebraic Equations • Finding zeros of equation f(x) = 0 • MATLAB function x_sol = fzero('your_func', x0, tol, trace); where tol and trace are optional arguments

  24. Examples of Algebraic Equations • Solve transcendental equation sin x = exp(x) - 5 • Define function f(x) = sin(x) - exp(x) + 5

  25. Chapter 6 Graphics

  26. Simple Plotting • plot(x,y,'r') • plot(x,y,':', x2,y2, '+') • plot(x,y,'b--') • See Table on Chapter 6 for style options.

  27. Other Plotting Commands • xlabel, ylabel labels on axis • title title text • text text in graphics • legend legend • axis axis limits/scale • gtext text with local • located by mouse

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

  29. 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)

  30. Advanced Features • "Handle graphics" gives a better control of graphic output. It is a lower level graphics.

  31. Chapter 8, What Else is There? • Symbolic Math and other Toolboxes • External Interface: Mex-files • Graphics User Interface

More Related