0% found this document useful (0 votes)
88 views

Intoduction To Atlab: M. Subramanian

MATLAB is a matrix laboratory developed by MathWorks for matrix manipulation and programming. It allows matrix and array operations, data analysis, visualization, and algorithm development. MATLAB contains signal and image processing tools, mathematical and technical functions, and a development environment for creating scripts and function files. It can be used for both numerical analysis and general-purpose programming.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Intoduction To Atlab: M. Subramanian

MATLAB is a matrix laboratory developed by MathWorks for matrix manipulation and programming. It allows matrix and array operations, data analysis, visualization, and algorithm development. MATLAB contains signal and image processing tools, mathematical and technical functions, and a development environment for creating scripts and function files. It can be used for both numerical analysis and general-purpose programming.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

Intoduction to MATLAB

M. Subramanian
[email protected] www.msubbu.com

23-Oct-2003

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

Available 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

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

Use arrow keys for scrolling through previous commands

MATLAB Workspace
List of variables in the workspace
who Your variables are: D NRe mu rho v

Variable Naming Rules


Variable names are case sensitive. For example NRe and nRe are different MATLAB variables Variable name can contain upto 31 characters. Start with a letter, followed by letters, numbers or underscores. For example: NRe_2_the_power2by3

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

clear D who Your variables are: 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

Exponential functions Complex functions Rounding and Remainder 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

Using Script M-files


what

M-files in the current directory C:\WINDOWS\Desktop\Matlab-Tutorials


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 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

0.1411 -0.7568 -0.9589 -0.2794

Columns 8 through 10 0.9894 0.4121 -0.5440

Array Addressing
y(3)

ans =
0.1411

y(1:5)
ans =

0.8415

0.9093

0.1411 -0.7568 -0.9589

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

A/B Warning: Matrix is singular to working precision.

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

determ(A) inv(A) ans = ans =

-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
< > <= >= == ~=

Control Flow Staements


for n=1:10 end while expression end if expression else ... end

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;

Polynomials from the Roots


PP = poly(r) PP = 1.0000 -12.0000 -0.0000 25.0000 116.0000

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 = -0.0251 3.1860 -0.8502

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

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

nle.m

Solving Nonlinear Equations

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/

You might also like