14571_basics of matlab 1
TRANSCRIPT
-
7/28/2019 14571_Basics of MATLAB 1
1/41
MATLAB R2010aIntroduction, practice and Applications
Nikesh BajajDept. of Electronics Engineering
Lovely Professional University
Phagwara-144402
-
7/28/2019 14571_Basics of MATLAB 1
2/41
-
7/28/2019 14571_Basics of MATLAB 1
3/41
MATLAB Desktop
3By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
4/41
Command Window >> prompt when ready for a
command
Busy in lower Can use arrow keys for
command history andmodify commands
4By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
5/41
Current Folder Current Folder Window
Displays contents of the
current working directory for script and function files
Can modify path throughMATLAB function or going
under File>Set Path
5By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
6/41
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 intocommand window
Double click will execute command
6By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
7/41
HELP Window
7By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
8/41
Basic Math/Calculator Basic Math operations
>> 4+3+5
ans =9 >> 4*2+5*3+99*2
Inbuilt Functions
sin, cos, tan, exp, log, log10, log2
sqrt, e10, ^
sin(30), sqrt(4), log(10)
8By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
9/41
-
7/28/2019 14571_Basics of MATLAB 1
10/41
Variable Naming ruleVariable 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
10By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
11/41
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
11By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
12/41
General Purpose Commands
12By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
13/41
Comments, Punctuations %, comments
; not displaying the result
, multiple commands
continue to next line
a=1, c=2; b=3 X=(a+b)*3
+5
13By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
14/41
Mathematical Functions Trigonometric
sin, sinh, asin, asind --sin(30)
cos, tan, csc, cot, sec Powers
^ power, exp, log, log10, log2, sqrt,
Rounding Functions
round nearest int. fix 0
floor -inf ceil +inf
14By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
15/41
Complex number c1= 2-3i or2-3j or2 -3*sqrt(-1) 0rcomplex(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
-
7/28/2019 14571_Basics of MATLAB 1
16/41
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
16By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
17/41
Number Theory
factor
isprime
prime
gcd
lcm
17By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
18/41
DATA Types
double, single
uint8, uint16, uint32, uint64
int8, int16, int32 int64
class(x) %data type of x
isinteger, isnumeric, isfloat
char cell
18By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
19/41
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)=?
19By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
20/41
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)=?
x(100)=?20 By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
21/41
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.
21By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
22/41
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)
22By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
23/41
Array Operations
y=x-2
y=2*x-1
2*x/5+1
x and y of same size
x+y; %element wise
x-y; x.*y x./y
Q: 1./x, x./y and x.\y
x.^223 By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
24/41
Array Operations
1./x =?
x.^y
Matrix ones(r,c) zeros(r,c)
x*y
inv(x), rank(x), eig(x)
Matrix Manipulation
x(2,3)=1 x(3:6, 5:8)=0
24By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
25/41
Multidimensional Array
x(1,1,1)
y(1,1,1,1,1, . . . .)
Almost every operation of 2D Arrayscan be extended to 3D 4D..nD
25By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
26/41
Problems
Do arithmetic operations with different datatypes and conclude it
Check isinteger, isnumeric, isfloat withdifferent values
26By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
27/41
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
27By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
28/41
Indexing: Array, matrix andCell
x=[2 3; 3 4]
x(:), x(1), x(4)
x(:) y=reshape(x,n,m)
28By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
29/41
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
29By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
30/41
Character Strings
Operations
double(txt)
char(x) txt
t=int2str(524); num2str(34.3)
str2int(32) str2num(32432.3);
30By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
31/41
Logical Operation
31By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
32/41
Loops/if-else
for i=1:10
end
while i>10
i=i+1
end
if x==y
elseif x==z
end32
By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
33/41
find command
x=randint(1,10,3)
i=find(x), find(x>0), find(x==1)
33By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
34/41
Plots (2D)
y=rand(1,10);
x=1:10
plot(x,y) plot(y)
title(sin wave)
xlabel(time) ylabel(Amplitude)
34By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
35/41
plot(X1,Y1,LineSpec, )
plot(x,y,'-.or')
plot(x,y,'d')
35By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
36/41
Plots (2D)
plot(x,y)
stem(x,y)
stairs(x,y)
area
bar, bar3, barh
feather
errorbar, rose
36By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
37/41
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 )
37By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
38/41
Plots (2D)
Multiplots
subplot(2,2,2)
38By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
39/41
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)
39By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
40/41
M file/script/function
40By Nikesh Bajaj
-
7/28/2019 14571_Basics of MATLAB 1
41/41
General Purpose Commands
clc, clear , clear all
date, clock
quit, exit size, length
max, min
who, whos
disp