Basics of MATLAB 1
Basics of MATLAB 1
Nikesh Bajaj
Dept. of Electronics Engineering Lovely Professional University Phagwara-144402
Basics of MATLAB
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
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 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
>> 4+3+5 ans =9 >> 4*2+5*3+99*2 sin, cos, tan, exp, log, log10, log2 sqrt, e10, ^
Inbuilt Functions
By Nikesh Bajaj
Math Functions
By Nikesh Bajaj
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
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
12
By Nikesh Bajaj
Comments, Punctuations
%, ; ,
comments not displaying the result multiple commands continue to next line
13
Mathematical Functions
Trigonometric
--sin(30)
Powers
Rounding Functions
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
By Nikesh Bajaj
Number Theory
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
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
Construction
Indexing
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
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
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
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
By Nikesh Bajaj
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?)
29
Character Strings
Operations
30
By Nikesh Bajaj
Logical Operation
31
By Nikesh Bajaj
Loops/if-else
32
find command
33
By Nikesh Bajaj
Plots (2D)
34
By Nikesh Bajaj
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)
37
By Nikesh Bajaj
Plots (2D)
Multiplots
subplot(2,2,2)
38
By Nikesh Bajaj
Plots 3D
plot3(x,y,z)
Meshgrid
39
By Nikesh Bajaj
M file/script/function
40
By Nikesh Bajaj
clc, clear , clear all date, clock quit, exit size, length max, min who, whos disp
41
By Nikesh Bajaj