Intoduction To Atlab: M. Subramanian
Intoduction To Atlab: M. Subramanian
M. Subramanian
[email protected] www.msubbu.com
23-Oct-2003
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
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
Simple Math
2+2.5+106 ans = 110.5000 4*25 + 2^3 ans = 108
MATLAB Variables
D=2 D= 2 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= 2
MATLAB Workspace
List of variables in the workspace
who Your variables are: D NRe mu rho v
Special variables
ans pi inf NaN i, j
Default variable name used for resuts Value of p Stands for infinity (e.g., 1/0) Stands for Not-a-Number (e.g., 0/0) i = j = -1
To clear a variable
who Your variables are: D NRe ans mu rho v
To clear variables
who Your variables are: NRe clear who ans mu rho v
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
Trigonometric functions
sin, cos, tan, sin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, csc, sec, cot, acsc, exp, log, log10, sqrt abs, angle, imag, real, conj floor, ceil, round, mod, rem, sign
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 Your variables are:
D NRe
D D= 2
rho mu v
Array Operations
x = [1 2 3 4 5 6 7 8 9 10]
x=
1 2 3 a = 1:2:10 a= 1 3 5 7 9 4 5 6 7 8 9 10
Array Operations
y = sin(x)
y=
Columns 1 through 7
0.8415 0.6570
0.9093
Array Addressing
y(3)
ans =
0.1411
y(1:5)
ans =
0.8415
0.9093
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 3 5 2 4 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 6 2 8
Significance of
A./B ans = 1.0000 1.5000 2.0000 2.0000
ans =
Inf Inf Inf Inf
Matrix Operations
[A B] ans = 1 3 ans-1 ans = 0 2 1 3 0 1 0 1 2 4 1 2 1 2
Matrix Operations
C = [A B] C= 1 3 2 4 1 2 1 2 C(:,2)
ans =
2 4 C(:,2:end) ans = 2 1 1 2 4 1 2 1 2
C(1,:) ans = 1
Matrix Functions
size(C) ans = 2 4 A
A=
1 3 2 4
-2
-2.0000 1.0000 1.5000 -0.5000
Matrix Functions
Some functions: determ, inv, diag, triu, tril, rank, eig, size eig(A) ans = -0.3723 5.3723 [a b] = eig(A)
a=
-0.8246 -0.4160 0.5658 -0.9094
b=
-0.3723 0 0 5.3723
Standard Arrays
eye(2) ans = 1 0 0 1 Other such arrays:
ones(n), ones(r, c)
zeros(n), zeros(r, c)
eye(2,3) ans = 1 0 0 1 0 0
rand(n), rand(r,c)
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
< > <= >= == ~=
Function m-files
flow.m
function [NRe, f] = flow(D,v,rho,mu) % flow(Diamter, Velocity, Density, % Viscosity) % Outputs NRe and f % Done on 22-Feb-2002 NRe = D*V*rho/mu; f = 0.079*NRe^(-0.25);
Calling a Function
[Re1, f1] = flow(3, 1.5, 900, 0.01) 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;
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
Symbolic Math
syms x int('x^3') ans =
1/4*x^4
eval(int('x^3',0,2))
ans =
4
Symbolic Math
syms a b [a, b] = solve('a+b=7', 'a-b=1') a=
b= 3
y1 =
-1
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
twotnk.m
Solving ODEs
function hdot = twotnk(t,h) % To solve % dh1/dt = 1 - 0.5*sqrt(h1-h2) % dh2/dt = 0.25*sqrt(h1-h2) - 0.25*sqrt(h2) hdot = zeros(2,1); % a column vector hdot(1) = 1- 0.5*sqrt(h(1)-h(2)); hdot(2) = 0.25*sqrt(h(1) - h(2)) - ... 0.25*sqrt(h(2));
Solving ODEs
[t, h] = ode45('twotnk', [0 100], [12 7]'); plot(t, h)
Getting Help
help sin
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. MATLAB Programming for Engineers Stephan J. Chapan Brooks/Cole, Australia Process Dynamics - Modeling, Analysis and Simulation B. Wayne Bequette Prentice Hall http://www.mathworks.com http://www.svce.ac.in/~msubbu/MathematicalSoftwares/