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

Intoduction To Atlab

This document provides an introduction to MATLAB. It discusses that MATLAB is a matrix laboratory developed by MathWorks for matrix manipulations and calculations. It offers programming features and GUI tools. MATLAB can generate various plots and graphs. It provides over 200 mathematical functions and solves problems involving matrices, complex numbers, differential equations, and more. MATLAB runs on various operating systems and has the same syntax across platforms.

Uploaded by

Maria Jafar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Intoduction To Atlab

This document provides an introduction to MATLAB. It discusses that MATLAB is a matrix laboratory developed by MathWorks for matrix manipulations and calculations. It offers programming features and GUI tools. MATLAB can generate various plots and graphs. It provides over 200 mathematical functions and solves problems involving matrices, complex numbers, differential equations, and more. MATLAB runs on various operating systems and has the same syntax across platforms.

Uploaded by

Maria Jafar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Intoduction to

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=

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 Default variable name used for resuts

pi Value of 

inf Stands for infinity (e.g., 1/0)

NaN Stands for Not-a-Number (e.g., 0/0)

i, j i = j = -1
To clear a variable
» who

Your variables are:

D ans rho
NRe mu v

» clear D
» who

Your variables are:

NRe ans mu rho v

»
To clear variables
» who

Your variables are:

NRe ans mu rho v

» 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

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

0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794


0.6570

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

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 =

»
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

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

You might also like