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
3
By Nikesh Bajaj
Command Window
>> prompt when ready for a command “Busy” in lower Can use arrow keys for command history and modify commands
4
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
5
By Nikesh Bajaj
Workspace and Command History
Workspace Window
Shows all currently defined variables Array dimensions Min, max values
Command History
Shows all past commands Can copy and past commands into command window Double click will execute command 6
By Nikesh Bajaj
HELP Window
7
By Nikesh Bajaj
Basic Math/Calculator
Basic Math operations
Inbuilt Functions
>> 4+3+5 ans =9 >> 4*2+5*3+99*2 sin, cos, tan, exp, log, log10, log2 sqrt, e10, ^
sin(30), sqrt(4), log(10) 8
By Nikesh Bajaj
Math Functions
9
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
Special Variables
ans beep pi inf NaN i and j Can be used as varible
11
By Nikesh Bajaj
General Purpose Commands
12
By Nikesh Bajaj
Comments, Punctuations
13
%, ; , …
comments not displaying the result multiple commands continue to next line
a=1, c=2; b=3 X=(a+b)*3… +5 By Nikesh Bajaj
Mathematical Functions
Trigonometric
--sin(30)
Powers
sin, sinh, asin, asind cos, tan, csc, cot, sec
^ power, exp, log, log10, log2, sqrt,
Rounding Functions
round nearest int. floor -inf 14
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
22
x(1,1), x(1,2) y=x(1,:) y=x(:,1) y=x(2:3,1:5)
By Nikesh Bajaj
Array Operations
y=x-2 y=2*x-1 2*x/5+1
x and y of same size 23
x+y; %element wise x-y; x.*y x./y Q: 1./x, x./y and x.\y x.^2
By Nikesh Bajaj
Array Operations
Matrix
1./x =? x.^y ones(r,c) zeros(r,c) x*y inv(x), rank(x), eig(x)
Matrix Manipulation 24
x(2,3)=1
x(3:6, 5:8)=0 By Nikesh Bajaj
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‟
29
By Nikesh Bajaj
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
if x==y elseif x==z end
32
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)
34
y=rand(1,10); x=1:10 plot(x,y) plot(y) title(„sin wave‟) xlabel(„time‟) ylabel(„Amplitude‟)
By Nikesh Bajaj
plot(X1,Y1,LineSpec, …) plot(x,y,'-.or') plot(x,y,'d')
35
By Nikesh Bajaj
Plots (2D)
36
plot(x,y) stem(x,y) stairs(x,y) area bar, bar3, barh feather errorbar, rose
By Nikesh Bajaj
Plots (2D)
Multiple functions on same plot
--grid on/off box off
37
plot(x1,y1,x2,y2,……) hold on hold off
axis([xmin xmax ymin ymax]) axis auto tight legend(„x1‟ „x2‟ )
By Nikesh Bajaj
Plots (2D)
Multiplots
38
subplot(2,2,2)
By Nikesh Bajaj
Plots 3D
plot3(x,y,z)
t=linspace(0,10*pi) plot3(sin(t), cos(t),t)
Meshgrid
x=sin(2*pi*t), y=x‟*x; [X Y]=meashgrid(1:101,1:101); plot3(X,Y,y) surf(y)
39
By Nikesh Bajaj
M file/script/function
40
By Nikesh Bajaj
General Purpose Commands
41
clc, clear , clear all date, clock quit, exit size, length max, min who, whos disp
By Nikesh Bajaj