You are on page 1of 43

Sana Zahid

Today's Topics

Books for MATLAB Introduction to MATLAB

MATLAB BOOKS

Our main book

INTRODUCTION TO MATLAB 7 FOR ENGINEERS

by WILLIAM J. PALM III

Introduction 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

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

14

Default Appearance

Simple Math
Scalar arithmetic operations
Exponentiation Multiplication Right division Left division Addition Subtraction ^ * / \ + -

Simple Math
2+2.5+106 ans = 110.5000 4*25 + 2^3 ans = 108

Order of Precedence

[{()}] from innermost ^ from left to right * & / from left to right + & - from left to right

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

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

det(A) inv(A) ans = ans =

-2
-2.0000 1.0000 1.5000 -0.5000

Matrix Functions & Arrays


Some functions: det, inv, diag, triu, tril, rank, eig, size,
Other such arrays: ones(n), ones(r, c) zeros(n), zeros(r, c) rand(n), rand(r,c) eye(n), eye(r,c)

29. Oktober 2013 /

43

You might also like