Workshop
Workshop
Workshop
CONTENTS
What Is MATLAB ?
The MATLAB Environment
Some useful MATLAB Commands
Generation of Vectors and Matrices
Generation of signals
Discrete signals
Continuous signals
MATLAB Graphics
2-Dimensional Plots
3-Dimensional Plots
ezplots
Flow Control
Scripts and Functions
Symbolic computations
GUIs
WHAT IS MATLAB ?
Starting MATLAB”
On a PC you access it via the Start menu, in Programs under
a folder such as MatlabR12 or Student MATLAB.
Alternatively, you may have an icon set up that enables you
to start MATLAB with a simple double-click.
THE MATLAB ENVIRONMENT
Workspace
Displays all the defined variables
Command Window
To execute commands in the
MATLAB environment
Command History
Displays record of the commands
used
Current Directory
Displays where the new files are
stored
Remember:
Another simple example:
t = 0:pi/4:2*pi;
EVERYTHING IN MATLAB
y = sin(t); IS A MATRIX !
plot(t,y)
1 2 3 4 5 6 7 8 9
2 0 0.707 1 0.707 0 - -1 - 0
0.70 0.70
7 7
A=[1 ;2; 3] 1
2
3
2 7 4
A = [2 7 4; 3 8 9]
3 8 9
OPERATIONS ON MATRICES
eye Identity matrix
linspace Vector with linearly spaced elements
ones Matrix of ones
rand Uniformly distributed random numbers and
arrays
randn Normally distributed random numbers and
arrays
zeros Matrix of zeros
: (colon) Vector with regularly spaced elements
X(1,3) Extracting Data form 1 row and 3 column
w=x(1,1:3) EXTRACTION OF A ROW FROM A MATRIX
z=x(1:3,2) EXTRACTION OF A COLUMN FROM A MATRIX
t(2,:)=[] DELETION OF A ROW FROM A MATRIX
u(:,2)=[] DELETION OF A COULOMN FROM A MATRIX
CONT….
Cat Concatenate arrays
diag Create or extract diagonal
fliplr Flip in left/right direction
flipud Flip in up/down direction
repmat Replicate and tile an array
reshape Change shape
rot90 Rotate 90◦
tril Extract lower tridiagonal part
triu Extract upper tridiagonal part
det Determinant
eig Eigenvalues and eigenvectors
expm Matrix exponential
inv Matrix inverse
poly Characteristic polynomial
rank Number of linearly independent rows or columns
rcond Condition estimator
trace Sum of diagonal elements
{}\ and / Linear equation solution
SPECIAL VARIABLES, CONSTANTS & FUNCTIONS
i or j √−1
Inf Infinity
NaN Not-a-Number
pi 3.14159 26535 897 …
calendar Calendar
clock Wall clock (complete date and time)
date You’d never guess
etime Elapsed time
tic, toc Stopwatch
weekday Day of the week
Basic Matlab
Functions in Matlab
function [output variables]=function_name (input variables)
out_1=in_1+in_2+in_3;
out_2=[ 'hello' ; 'world' ];
out_3=[1,2,3,4,5];
return;
ADDING TITLES, AXIS LABELS, AND ANNOTATIONS
t=0:0.1:2*pi;
x=sin(t);
y=cos(t);
plot(t,x,’*’)
text(3.2,0,'Sin wave')
hold on
plot(t,y,’-r’)
text(4,-0.8,'cos wave')
hold off
xlabel('time')
ylabel('amplitude')
title('cos & sine functions')
legend('sine function','cos function')
Symbol Color Symbol Line Style Symbol Marker
k Black − Solid + Plus sign
r Red −− Dashed o Circle
b Blue : Dotted ∗ Asterisk
g Green −. Dash-dot . Point
c Cyan none No line × Cross
m Magenta s Square
y Yellow d Diamond
v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
plot(t,x,'--rs','LineWidth',2, 'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
t = 0:pi/50:10*pi;
plot3(t,exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t))
xlabel('x-axis'), ylabel('y-axis'), zlabel('z-axis')
CONT….
x=0:0.1:10;
y=0:0.1:10;
z=exp(-(x.^2+y.^2));
plot3(x,y,z,'linewidth',4)
xlabel('x-variable'), ylabel('y-variable'),
zlabel('function f(x,y)')
MESH 3-D MESH SURFACE
contour(u)
clc
FLOW CONTROL clear all
a = input('Enter first number: ');
b = input('Enter second number: ');
Conditional statements:
if a < b
The most famous conditional statement in all disp( [ num2str(b) ' is larger.'] );
programming languages is the if-statement, elseif a > b
which usually has the form: disp( [ num2str(a) ' is larger.'] );
If condition else
Actions to be done if true condition disp( 'Numbers are equal.' );
end
elseif another_condition
clc
Actions to be done if true another_condition clear all
Else grade = input('enter the marks'); % example
if grade > 95
Actions to be done if both condition and disp('The grade is A');
another_condition are false elseif grade <= 95 & grade > 86
disp('The grade is B');
end elseif grade <= 86 & grade > 76
disp('The grade is C');
elseif grade <= 76 & grade > 66
disp('The grade is D');
elseif grade <= 66 & grade > 34
disp('The grade is E');
else
disp('Fails');
end
LOOPS
Push Button.
Toggle Button.
Check Box.
Radio Button.
Editable Text.
List Box.
Pop-up Menu.
Slider.
Frame.
Static Text
DISPLAYING DATE AND TIME IN GUI
SIMPLE COS, SIN, EXP PLOTS IN GUI
MY CALUCLATOR