Numerical Technique Ex-P1-2-3
Numerical Technique Ex-P1-2-3
Numerical Technique Ex-P1-2-3
Power point slides prepared by: Mohammed Abdul Kader, Assistant Professor, Dept. of EEE, IIUC
What is MATLAB?
• Stands for MATrix LABoratory developed by The Mathworks, Inc
(www.mathworks.com)
>> version % this will tell you the running MATLAB version
ans =
9.0.0.341360 (R2016a)
>> help % lists available packages/toolboxes on system.
>> help elfun % lists functions in elementary functions package
>> help sin % instructions on the sine function
>> lookfor sine % if you don’t know the function name …
>> doc % start matlab help documentation
>> doc sin % for full details of function
>> quit % to quit MATLAB
MATLAB Preview
Current Directory
Command Window
Workspace
Variables
Don’t have to declare type, is case sensitive
variable begins with a letter, e.g., A2z or a2z
can be a mix of letters, digits, and underscores (e.g.,
vector_A)
Variable name can be up to 63 characters
Don’t even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned 12
comment operator
>>A(3,2:3)
A=
ans = 3 2 1
1 7 5 1 0
>>A(:,2) 2 1 7
ans =
2
1
1
What’ll happen if you type A(:,:) ?
Manipulating Matrices
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
A= B=
3 2 1 1 3 1
5 1 0 4 9 5
2 1 7 2 7 2
Task: Create matrices A and B and try out the matrix operators in this slide
MATLAB Graphics
Line plot
Bar graph
Surface plot
Contour plot
MATLAB has 2D, 3D visualization tools as well as
other graphics packages.
MATLAB Graphics: Line Plot
>> t = 0:pi/100:2*pi;
>> y = sin(t);
>> plot(t,y)
MATLAB Graphics: Line Plot (Cont.)
>> xlabel(‘t’);
>> ylabel(‘sin(t)’);
>> title(‘The plot of t vs sin(t)’);
MATLAB Graphics: Line Plot (Cont.)
>> y2 = sin(t-0.25);
>> y3 = sin(t+0.25);
>> plot(t,y,t,y2,t,y3) % make 2D line plot of 3 curves
>> legend('sin(t)','sin(t-0.25)','sin(t+0.25',1)
Scripts
Matlab editor
Use scripts to execute a series of Matlab commands
function keyword