Introduction To Matlab: Khan M.Nazir
Introduction To Matlab: Khan M.Nazir
Matlab
Khan M.Nazir
University of Management & Technology
Matlab is a high performance language
for technical computing.
The name matlab stands for matrix
laboratory.
It is a large software package, that has
many advanced features built-in, and it
has become a standard tool for many
working in science or engineering
disciplines.
As part of the undergraduate Electrical
Engineering program, you will be using Matlab to
solve problems in
Circuits
Communication systems
Digital signal processing
Control systems
Probability and statistics
In addition, you can use Matlab to build
Graphical User Interfaces (GUIs) so that you
can develop user-friendly custom software.
The Matlab software environment has a core
module (called Matlab) and associated with that
are a set of "Toolboxes" that perform
specialized computations
Double click the Matlab lab icon,
the window that opens is called
command window
The >> sign shows that it is waiting for
us to write a new command.
Type the following command in the
command window:
>>x = 7 ;
>> 2*x
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
complex conjugate transpose
Arithmetic Operators
>> a=7;
>> b=3;
>> x=a+b
To go back to any particular
line for making correction etc,
Use key
Use key, for doing subtraction,
multiplication etc
>> x=a-b
>> x=a*b
>> x=a^b
Order of precedence
Order of precedence.
Comments
Matrix
Matrices are 2 dimensional quantities and
are created similar to vectors. We can do
>> a = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
Array, Matrix
Matrix Transpose
Matrix Multiplication
Since we have already defined x matrix
earlier, so we can perform any operation
on it. Try
>> 2*x
Matrix multiplication
Matrix multiplication
More Operators (arithmetic)
.* element-by-element mult
./ element-by-element div
.^ element-by-element power
. transpose
Element by element multiplication
Element by element power
Long Array
>> t =1:10
t =
1 2 3 4 5 6 7 8 9 10
Long Array
>> k =2:-0.5:-1
k =
2 1.5 1 0.5 0 -0.5 -1
Matrix
>> B = [1:4; 5:8]
B =
1 2 3 4
5 6 7 8
help
Managing the workspace
Clear command window
CLC
CLC clears the
command window
and homes the
cursor.
Managing the workspace
Mathematical functions
Managing the workspace
Operators (relational, logical)
== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
& AND
| OR
~ NOT
Graph Plot Demo
Type in the following
>> [x,y] = meshgrid(-2:.2:2, -2:.2:2);
>> z = x .* exp(-x.^2 - y.^2);
>> surf(x,y,z)
Did you get the following figure
?
Creating simple plots
Creating simple plots.
Coloring the graph
Line
hold on, hold off
Label & Title
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function')
Grid on
Loops
The for loop (very similar to C expression)
is a simple command for setting up a loop.
Example: >> for i = 1:10;
>> a(i) = i*i;
>> end
>> a
a =
1 4 9 16 25 36 49 64 8
1 100
for x=1:10
y(x)=x^2+x;
end
y
Complex numbers
>> z1=3+4*j;
>> theta=(60/180)*pi;
>> z2=2*exp(j*theta);
>> z=z1+z2;
>> zmag=abs(z);
>> zangle=angle(z)*(180/pi);
>> z,zmag,zangle
plot
Same program with comments