matlab lecture 4 solving mathematical problems.ppt

aaaaboud1 21 views 31 slides May 14, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

MATLAB


Slide Content

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];
b = [10; 20; 9];
x = A \b
Check solution
c = A*x
c should be equal to b.

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

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)

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
See
help sparfun
for detail

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 = [a
n, a
n-1, …, a
1, a
0]

Curve Fitting
y = polyval(a, x)
compute the value of
polynomial at value x
y = a(1) x
n
+ a(2) x
n-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)
methodis 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
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
Solve the first order differential
equation
dx/dt = x + t
with 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 pendulum
d
2
/dt
2
+ 
2
sin= 0
Convert into set of first-order
ODE with z
1 = , z
2 = d/dt,
dz
1/dt = z
2,
dz
2/dt = -
2
sin(z
1)

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

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

Examples of Algebraic
Equations
Solve transcendental equation
sin x = exp(x) -5
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--')
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
Tags