Intoduction To Atlab
Intoduction To Atlab
MATLAB
• MATLAB - developed by Math Works Inc.
• http://www.mathworks.com
• MATLAB - acronym for MATrix LABoratory
• Matrices and arrays - the heart of MATLAB
• Offers programming features - similar to other languages
• Offers GUI tools
• Provides a variety of graphic output displays:
• linear
• log-log
• semilog
• polar
• bar chart, and
• contour plots
• 2-D and 3-D views
• Provides extensive numerical resources
• Over 200 reliable, accurate mathematical subprograms
• The subprograms provide solutions to broad range of
mathematical problems including:
• matrix algebra
• complex arithmetic
• differential equations
• nonlinear systems, and
• many special functions
• A Provides extensive numerical resources
• Over 200 reliable, accurate mathematical subprograms
• The subprograms provide solutions to broad range of
mathematical problems including:
• matrix algebra
• complex arithmetic
• differential equations
• nonlinear systems, and
many special functions vailable in all operating systems:
DOS
Windows9.x/NT
Unix
Macintosh
Same syntax for all platforms
Open system environment - access to source code
Allows - to mix MATLAB with FORTRAN or C
6
Simple Math
» 2+2.5+106
ans =
110.5000
» 4*25 + 2^3
ans =
108
»
MATLAB Variables
»D=2
D=
»v=3
v=
3
MATLAB Variables
» rho = 1000;
» mu = 0.001;
» NRe = D*v*rho/mu
NRe =
6000000
»
MATLAB Workspace
To recall the variable
»D
D=
D NRe mu rho v
»
Variable Naming Rules
• Variable names are case sensitive. For example NRe and
nRe are different MATLAB variables
pi Value of
i, j i = j = -1
To clear a variable
» who
D ans rho
NRe mu v
» clear D
» who
»
To clear variables
» who
» clear
» who
»
Complex Numbers
»i
ans =
0 + 1.0000i
» c1 = 2+3i
c1 =
2.0000 + 3.0000i
»
Mathematical Functions
» x=sqrt(2)/2
x=
0.7071
» y=sin(x)
y=
0.6496
»
Built-in Functions
T r ig o n o m e t r ic s in , c o s , ta n , s in , a c o s , a ta n ,
f u n c t io n s s in h , c o s h , ta n h , a s in h ,
a c o s h , a ta n h , c s c , s e c , c o t,
acsc, …
E x p o n e n t ia l e x p , lo g , lo g 1 0 , s q rt
f u n c t io n s
C o m p le x a b s , a n g le , i m a g , re a l, c o n j
f u n c t io n s
R o u n d in g a n d f lo o r, c e il, r o u n d , m o d , r e m ,
R e m a in d e r s ig n
f u n c t io n s
Using Script M-files
» what
frictionfactor
» frictionfactor
Dia in meter =
Using Script M-files
» frictionfactor
Dia in meter = 2
Velocity in m/s = 3
NRe =
6000000
0.0016
»
Saving Data
» clear
» D = 2;
» v = 3;
» rho = 1000;
» mu = 0.001;
» NRe = D*v*rho/mu;
» f = 0.076*NRe^(-0.25);
»
» save data1 D v rho mu NRe f
Loading Data
» clear
» load data1
» who
D f rho
NRe mu v
»D
D=
2
Array Operations
» x = [1 2 3 4 5 6 7 8 9 10]
x=
1 2 3 4 5 6 7 8 9 10
» a = 1:2:10
a=
1 3 5 7 9
»
Array Operations
» y = sin(x)
y=
Columns 1 through 7
Columns 8 through 10
»
Array Addressing
» y(3)
ans =
0.1411
» y(1:5)
ans =
»
Array Orientation
» z = [1; 2; 3; 4]
z=
1
2
3
4
» z'
ans =
1 2 3 4
»
Array Manipulation
» A = [1 2; 3 4; 5 6]
A=
1 2
3 4
5 6
» B = [1 2 3; 4 5 6];
» A+B
??? Error using ==> +
Matrix dimensions must agree.
»
Array Manipulation
» A*B
ans =
9 12 15
19 26 33
29 40 51
Significance of .
» clear
» A = [1 2; 3 4];
» B = [1 1; 2 2];
» A.*B
ans =
1 2
6 8
»
» A./B
Significance of .
ans =
1.0000 2.0000
1.5000 2.0000
» A/B
Warning: Matrix is singular to working precision.
ans =
Inf Inf
Inf Inf
»
Matrix Operations
» [A B]
ans =
1 2 1 1
3 4 2 2
» ans-1
ans =
0 1 0 0
2 3 1 1
»
Matrix Operations
» C = [A B] » C(:,2)
C= ans =
1 2 1 1 2
3 4 2 2 4
» C(1,:) » C(:,2:end)
ans = ans =
1 2 1 1 2 1 1
4 2 2
»
»
Matrix Functions
» size(C) »A
ans = A=
2 4 1 2
3 4
» determ(A)
» inv(A)
ans =
ans =
-2
-2.0000 1.0000
» 1.5000 -0.5000
»
Matrix Functions
Some functions: determ,
» [a b] = eig(A)
inv, diag, triu, tril, rank,
eig, size
a=
» eig(A)
-0.8246 -0.4160
0.5658 -0.9094
ans =
-0.3723
b=
5.3723
-0.3723 0
»
0 5.3723
»
Standard Arrays
» eye(2)
Other such arrays:
ans =
ones(n), ones(r, c)
1 0
zeros(n), zeros(r, c)
0 1
rand(n), rand(r,c)
» eye(2,3)
ans =
1 0 0
0 1 0
»
Plots
» x = 1:2:50;
» y = x.^2;
» plot(x,y)
»
Plots
» plot(x,y,'*-')
» xlabel('Values of x')
» ylabel('y')
»
Plots
» P = logspace(3,7);
» Q = 0.079*P.^(-0.25);
» loglog(P,Q, '.-')
» grid
Relational Operators
<
>
<=
>=
==
~=
Control Flow Staements
for n=1:10
…
end
while expression
…
end
if expression
…
else
...
end
Function m-files
flow.m
Re1 =
405000
f1 =
0.0031
»
Polynomials
Representing Polynomials:
x4 - 12x3 + 25x + 116
» P = [1 -12 0 25 116];
» roots(P)
ans =
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i
» r = ans;
Polynomials from the Roots
» PP = poly(r)
PP =
»
Polynomial Multiplication
a = x3 + 2x2 + 3x + 4
b = 4x2 + 9x + 16
» a = [1 2 3 4];
» b = [4 9 16];
» c = conv(a,b)
c=
4 17 46 75 84 64
»
Evaluation of a Polynomial
a = x3 + 2x2 + 3x + 4
» polyval(a, 2)
ans =
26
»
Polynomial Curve Fitting
» x = [1 3 7 21];
» y = [2 9 20 55];
» polyfit(x,y,2)
ans =
»
Symbolic Math
» syms x
» int('x^3')
ans =
1/4*x^4
» eval(int('x^3',0,2))
ans =
»
Symbolic Math
» syms a b
» [a, b] = solve('a+b=7', 'a-b=1')
a=
b=
»
Solving Nonlinear Equation
fcn1.m
function y = fcn1(x)
% To solve f(x) = x^2 - 2x - 3 = 0
y = x^2 - 2*x - 3;
» y1 = fzero('fcn1', 0)
Zero found in the interval: [-1.28, 0.9051].
y1 =
-1
»
Solving Nonlinear Equations
nle.m
function f = nle(x)
% To solve
% f1(x1,x2) = x1^2 - 4x1^2 - x1x2 = 0
% f2(x1,x2) = 2x^2 - x2^2 + 3x1x2 = 0
f(1) = x(1) - 4*x(1)*x(1) - x(1)*x(2);
f(2) = 2*x(2) - x(2)*x(2) + 3*x(1)*x(2);
» x0 = [1 1]';
» x = fsolve('nle', x0)
x=
0.2500
0.0000
Solving ODEs
twotnk.m
» lookfor nonlinear
FMINBND Scalar bounded nonlinear function minimization.
FMINSEARCH Multidimensional unconstrained nonlinear
minimization (Nelder-Mead).
FZERO Scalar nonlinear zero finding.
D1ODE Stiff problem, nonlinear with real eigenvalues (D1 of
EHL).
PDENONLIN Solve nonlinear PDE problem.
PDERESID Residual for nonlinear solver
»
References
• Mastering MATLAB
Duane Hanselman, Bruce Littlefield
The MATLAB Curriculum Series, Prentice Hall Inc.
• http://www.mathworks.com
•
http://www.svce.ac.in/~msubbu/MathematicalSoft