Professional Documents
Culture Documents
Matlab 4
Matlab 4
Applications
Linear Algebra
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.
Gaussian Elimination
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
See
help sparfun
for detail
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
Example-1:
Straight-line fit:
Problem: Fit the set of data,
and make a plot of it.
Use
quad('your_func', a, b);
or quad('your_func',a, b, opt…)
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
Examples of ODE
plot(t, x)
xlabel('t'), ylabel('x')
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)
Pendulum Function File
Define function
f(x) = sin(x) - exp(x) + 5
Chapter 6
Graphics
Simple Plotting
plot(x,y,'r')
plot(x,y,':', x2,y2, '+')
plot(x,y,'b--')
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