Sie sind auf Seite 1von 8

Mathematical Applications

MATLAB WORKSHOP

MATLAB
An Introductory Workshop
Tutorial 3

Mathematical Applications

Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Vector Products and Norms


% cross product
u = ai + bj + ck
v = di + ej + fk

v x u = (bf - ce) i + (cd - af) j + (ae - bd) k


a = [1 2 3];
b = [4 5 6];
c = cross(a,b)

% Vector and Matrix Norms


v = [2 0 -1];
[norm(v,1) norm(v) norm(v,inf)]

% dot product

Euclidean Norm

v = ai + bj
w = ci + dj
v.w = ac + bd

a = [1 2 3];
b = [4 5 6];
c = dot(a,b)
Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Inverse, Determinant & SVD


% Matrix Inverse
inv(A)
% Determinant of Matrix
det(A)
Example
A = [1 2 3; 4 5 6; 7 8 9]
d= det(A)
d = 27
% Singular Values Decomposition
A = [1 2; 3 4; 5 6; 7 8];
S = svd(A)
[U,S,V] = svd(A)

Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Linear Algebra
% Solving a Linear System
5x = 3y - 2z + 10
8y + 4z = 3x + 20
2x+ 4y -9z = 9

% Rearrange equation
5x - 3y + 2z = 10
-3x + 8y + 4z = 20
2x + 4y - 9z = 9

% Write equation in matrix form


[A]{x} = {b}
x = unknown vector
A = coefficients of unknown vector
b = known constant vector

% Solution
A = [5 -3 2; -3 8 4; 2 4 -9];
b = [10;20;9];
x = b/A
x = A\b
x = inv(A)*b

% Reminder
Ax = b
x-?
x = b/A
x = inv(A)*b

% Check the solutions


>> c = A*x

Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Finding eigenvalues and eigenvectors


Enter a matrix A
>>A =[5 -3 2; -3 8 4; 4 2 -9];
>>[V,D] = eig(A)
V=?
D=?

% Eigen value Problem

Av v
A 0
V = matrix of eigen vector of A column wise
1st column of V = 1st eigen vector
D = matric eigen values of A on its diagonal
% extract what you want
v2 = V(:,2) % 2nd Column of vector
lam2 = D(2,2)
% 2nd eigen value
A*v2 - lam2*v2
% difference between A*v and lambda*v

Applications
Real Symmetric Matrix
A = gallery('lehmer',4) % symmetric positive definite matrix
Nonsymmetric Matrix
A = gallery('circul',3) % ciculant matrix
a Matrix Whose Elements Differ Dramatically in Scale

Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Curve Fitting
% Curve fitting
1. No magical function give you relationship
2. Idea about the function
3. Fitting by toolbox

% Compare two functions


>> x = 0:pi/30:pi/3;
>> y = sin(x) + rand(size(x))/100;
>> plot(x,y,'o')

% Straight line fit


x = [5 10 20 50 100]
y = [15 33 53 140 301]
plot(x, y, o)
Open basic fitting toolbox

Quadratic and Cubic fit


Curve fitting by command line
>> a = polyfit(x,y, n)
n = 1 % Straight-line

% user defined function


>> x = 0:pi/30:pi/3;
% x data
>> y = sin(x) + rand(size(x))/100;
% ydata
>> fx = @(a,x) a(1)*x.^2+a(2).*x + a(3);
% predicted function
>> a0 = [2;2;2];
% initial guess of coefficients
>> A = lsqcurvefit(fx,a0,x,y);
>> [ahat,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(fx,a0,x,y);

Tutorial 3

% Caution
ployfit of higher order
Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

Matrix Functions

det
diag
eig
inv
norm
rank

Determinant
Diagonal matrices and diagonals of a matrix
Eigenvalues and eigenvectors
Matrix inverse
Matrix and vector norms
Number of linearly independent rows or columns

Tutorial 3

Akhilesh Shende

Mathematical Applications

MATLAB WORKSHOP

User defined Functions


% function y = f(x)
fx = @(x) x^2 sin(x)
fx(5)

fxy = @(x,y) x^2 + y^2


fxy(2,3)
% Create function (.m) file
1. Open function file
function y = my_fun (x)
y = x^2 sin(x);
end
2. Save function file by name my_fun.m
3. Add the path of function file

fx = @(x) x.^2 sin(x)


x = [0:0.1:pi/2];
plot(x,fx(x))

fx = @(x) x^2 sin(x)


x = [0:0.1:pi/2];
plot(x,fx(x))

% find Minima for function


p = fminsearch(@my_fun,.5) % guess 0.5 near to minima
% value of minima
my_fun(p)
Tutorial 3

Akhilesh Shende

Das könnte Ihnen auch gefallen