You are on page 1of 41

MATLAB R2010a

Introduction, practice and Applications

Nikesh Bajaj
Dept. of Electronics Engineering Lovely Professional University Phagwara-144402

Basics of MATLAB

Basic Mathematic: Calculator Array and Array Operation

Indexing, Matrix Operations, Manipulations

Multidimensional Array Cell Array Character Strings Logical Operations Loops/switch-case/if else: PLOTS Script/M file- function and code
2

By Nikesh Bajaj

MATLAB Desktop

By Nikesh Bajaj

Command Window

>> prompt when ready for a command Busy in lower Can use arrow keys for command history and modify commands

By Nikesh Bajaj

Current Folder

Current Folder Window

Displays contents of the current working directory for script and function files Can modify path through MATLAB function or going under File>Set Path

By Nikesh Bajaj

Workspace and Command History

Workspace Window

Shows all currently defined variables Array dimensions Min, max values Shows all past commands Can copy and past commands into command window Double click will execute command
6

Command History

By Nikesh Bajaj

HELP Window

By Nikesh Bajaj

Basic Math/Calculator

Basic Math operations


>> 4+3+5 ans =9 >> 4*2+5*3+99*2 sin, cos, tan, exp, log, log10, log2 sqrt, e10, ^

Inbuilt Functions

sin(30), sqrt(4), log(10)


8

By Nikesh Bajaj

Math Functions

By Nikesh Bajaj

Variable Naming rule

Variable names are Case Sensitive

Xyz, xyz, XYZ, XyZ all are different

Variable name should start with character only Can be followed by digit _ Punctuation and space are not allowed Max length of name is 63 characters

10

By Nikesh Bajaj

Variable Naming rule

Reserve word list (Keyword)


for, end if else, elseif, function, case switch break isvarname( ) True(1), False(0) Can not be used as variable ans beep pi inf NaN i and j Can be used as varible

Special Variables

11

By Nikesh Bajaj

General Purpose Commands

12

By Nikesh Bajaj

Comments, Punctuations

%, ; ,

comments not displaying the result multiple commands continue to next line


13

a=1, c=2; b=3 X=(a+b)*3 +5


By Nikesh Bajaj

Mathematical Functions

Trigonometric

sin, sinh, asin, asind cos, tan, csc, cot, sec

--sin(30)

Powers

^ power, exp, log, log10, log2, sqrt,


round nearest int. floor -inf
14

Rounding Functions

fix 0 ceil +inf

By Nikesh Bajaj

Complex number

c1= 2-3i or 2-3j or 2 -3*sqrt(-1) 0r complex(2,-3) c2= sqrt(-2)=? c3=7+sin(0.5)j Execute it c4=(c1+c2)/c3 c5=real(c4) =imag(c4) =abs(c4) =angle(c4)
15

By Nikesh Bajaj

Floating Points

format short format long precision; eps(x), eps(1), eps(10)

PROBLEM: Execute the following


0.08 - 0.5 + 0.42 0.42-0.5+0.08 0.08 +0.42 -0.5


16

By Nikesh Bajaj

Number Theory

factor isprime prime gcd lcm

17

By Nikesh Bajaj

DATA Types

double, single uint8, uint16, uint32, uint64 int8, int16, int32 int64 class(x) %data type of x isinteger, isnumeric, isfloat char cell
18

By Nikesh Bajaj

Array (1D)

Construction

x=[ 1 2 3 4 5 6 7 8 9 10 ]; x=1:10 or 1:1:10 x=linspace(1,10,10); y=(0:0.1:1)*pi = ? y=linspace(0,pi,11); y=linspace(2,17,24); z=logspace(0,2,10)=?

19

By Nikesh Bajaj

Array (1D)

Indexing
x(0) error x(1), x(4) y=x(2:7) y=x(4:end) y=x(1:2:end) y=x(10:-1:1) y=x([ 3 2 1 9 10 ]) y=x([1 1 2 2 3 3 1 1 ]) x(3.4)=? 20 x(100)=?

By Nikesh Bajaj

Array (1D)

Creating an array from others z = [x y] % appending z =[y x] z= [x(1:2:10) 1 0 1 0] Transpose z=x z=x.
21

By Nikesh Bajaj

Array (2D) Matrix

Construction

x=[ 1 2 ;4 5]; x=[1 2 3; 2 3]=?

Indexing

x(1,1), x(1,2) y=x(1,:) y=x(:,1) y=x(2:3,1:5)

22

By Nikesh Bajaj

Array Operations

y=x-2 y=2*x-1 2*x/5+1 x+y; %element wise x-y; x.*y x./y Q: 1./x, x./y and x.\y x.^2

x and y of same size


23

By Nikesh Bajaj

Array Operations

1./x =? x.^y ones(r,c) zeros(r,c) x*y inv(x), rank(x), eig(x) x(2,3)=1 x(3:6, 5:8)=0
By Nikesh Bajaj

Matrix

Matrix Manipulation

24

Multidimensional Array

x(1,1,1) y(1,1,1,1,1, . . . .) Almost every operation of 2D Arrays can be extended to 3D 4D..nD

25

By Nikesh Bajaj

Problems

Do arithmetic operations with different data types and conclude it Check isinteger, isnumeric, isfloat with different values

26

By Nikesh Bajaj

Cell Array

Cell Array? Construction


variable should not exist before. A(1,1)={[ 2 3 ; 3 4]} A(2,2)={HI} A{1,1}=[2 3;3 4] A{2,2} =HI

celldisp(A), cell(2,3) indexing manipulation


27

By Nikesh Bajaj

Indexing: Array, matrix and Cell


x=[2 3; 3 4] x(:), x(1), x(4) x(:) y=reshape(x,n,m)

28

By Nikesh Bajaj

Character Strings

Construction

txt=Hi my name is Bajaj txt =[ hi hello how are you?] txt =[ hi How are you? ] txt= char(hi, how are you?)

Indexing and Manipulation


size(txt) txt(1), txt (2,10), txt(2:5) txt(3)=N


By Nikesh Bajaj

29

Character Strings

Operations

double(txt) char(x) txt t=int2str(524); num2str(34.3) str2int(32) str2num(32432.3);

30

By Nikesh Bajaj

Logical Operation

31

By Nikesh Bajaj

Loops/if-else

for i=1:10 end while i>10 i=i+1 end

32

if x==y elseif x==z end


By Nikesh Bajaj

find command

x=randint(1,10,3) i=find(x), find(x>0), find(x==1)

33

By Nikesh Bajaj

Plots (2D)

y=rand(1,10); x=1:10 plot(x,y) plot(y) title(sin wave) xlabel(time) ylabel(Amplitude)

34

By Nikesh Bajaj

plot(X1,Y1,LineSpec, ) plot(x,y,'-.or') plot(x,y,'d')

35

By Nikesh Bajaj

Plots (2D)

plot(x,y) stem(x,y) stairs(x,y) area bar, bar3, barh feather errorbar, rose

36

By Nikesh Bajaj

Plots (2D)

Multiple functions on same plot


plot(x1,y1,x2,y2,) hold on hold off

--grid on/off box off

axis([xmin xmax ymin ymax]) axis auto tight legend(x1 x2 )

37

By Nikesh Bajaj

Plots (2D)

Multiplots

subplot(2,2,2)

38

By Nikesh Bajaj

Plots 3D

plot3(x,y,z)

t=linspace(0,10*pi) plot3(sin(t), cos(t),t) x=sin(2*pi*t), y=x*x; [X Y]=meashgrid(1:101,1:101); plot3(X,Y,y) surf(y)

Meshgrid

39

By Nikesh Bajaj

M file/script/function

40

By Nikesh Bajaj

General Purpose Commands

clc, clear , clear all date, clock quit, exit size, length max, min who, whos disp

41

By Nikesh Bajaj

You might also like