MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.
-
Upload
mae-harris -
Category
Documents
-
view
222 -
download
6
Transcript of MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.
![Page 1: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/1.jpg)
MATLAB
Lecture Two (Part II)Thursday/Friday,
3 - 4 July 2003
![Page 2: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/2.jpg)
Chapter 5
Applications
![Page 3: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/10.jpg)
Sparse Matrix
Seehelp sparfun
for detail
![Page 11: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/25.jpg)
Chapter 6
Graphics
![Page 26: MATLAB Lecture Two (Part II) Thursday/Friday, 3 - 4 July 2003.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/26.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/27.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/28.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/29.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/30.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649d015503460f949d3e1c/html5/thumbnails/31.jpg)
Chapter 8, What Else is There?
Symbolic Math and other Toolboxes
External Interface: Mex-files Graphics User Interface