Sie sind auf Seite 1von 40

Introduction to Matlab

By:
Amrit Lal Saha
OUTLINE:
 What is Matlab?
 Matlab Screen
 Variables, array, matrix, indexing
 Operators (Arithmetic, relational, logical )
 Matrices Operations
 Equations and Polynomials
 Flow Control
 Use of M-File
 Writing User Defined Functions
 Display Facilities
 THE END
WHAT IS MATLAB?

 Matlab is basically a high level language which


has many specialized toolboxes for making things
easier for us.
 How high?

Matlab

High Level
Languages such as
C, Pascal etc.

Assembly
 MATLAB is an interactive system
whose basic data is a matrix that
does not require dimensioning.

 Stands for MATrix LABoratory

 Everything is a matrix - easy to do linear


algebra
VARIABLES:
 No need for types. i.e., all variables are
created with double precision unless
specified and they are matrices.
ARRAY, MATRIX
A Vector:
X=[1 2 3 4]
X=
1 2 3 4
 Equally spaced one-dimensional array using colon Y= 1:5
Y=
1 2 3 4 5
 In steps of two Z= 1:2:10
Z=
1 3 5 7 9
 In steps of negative two A= 10: -2: 1
A=
10 8 6 4 2
 Transpose
X=[1 2 3 4]
X=
1 2 3 4

Y=X’
Y=
1
2
3
4

 2-D Array
A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
GENERATING VECTORS FROM FUNCTIONS
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0

ones(M,N) MxN matrix of ones x = ones(1,3)


x =
1 1 1

rand(M,N) MxN matrix of x = rand(1,3)


uniformly x =
distributed random 0.9501 0.2311 0.6068
numbers on (0,1)
eye(M,N) Identity matrix with x = eye(1,3)
1’s on diagonal other- x =
wise 0 1 0 0

magic(N) NxN matrix constructed x = magic(3)


from the integers 1 X =
through N^2 with equal 8 1 6
row, column & diagonal 3 5 7
sum. 4 9 2
MATRIX INDEX:
 The matrix indices begin from 1 (not 0)
 The matrix indices must be positive integer

A(-2), A(0)

Error: ??? Subscript indices must either be real positive integers or


logicals.

A(4,2)
Error: ??? Index exceeds matrix dimensions.
OPERATORS :
ARITHMETIC:
+ addition
- subtraction
* multiplication
/ division
^ power

ELEMENT-BY-ELEMENT
.* element-by-element multiplication
./ element-by-element division
.^element-by-element power
MATRICES OPERATIONS
>>A = [1 2 3; 4 5 6; 7 8 9] >>B = [3 5 2; 5 2 8; 3 6 9]
Given A and B: A= B=
1 2 3 3 5 2
4 5 6 5 2 8
7 8 9 3 6 9

Addition Subtraction Product Transpose


>>X=A+B >>Y=A-B >>Z=A*B >>T=A’
X= Y= Z= T=
4 7 5 -2 -3 1 22 27 45 1 4 7
9 7 14 -1 3 -2 55 66 102 2 5 8
10 14 18 4 2 0 88 105 159 3 6 9
MATRICES OPERATIONS
A = [1 2 3; 5 1 4; 3 4 -1]
A=
1 2 3
5 1 4
3 4 -1

x = A(1,:) y = A(3 ,:) b = x .* y c=x./y d = x .^2

x= y= b= c= d=
1 2 3 3 4 -1 3 8 -3 0.33 0.5 -3 1 4 9

K= x^2
Error:
??? Error using ==> mpower Matrix must be square.
B=x*y
Error:
??? Error using ==> mtimes Inner matrix dimensions must agree.
OPERATORS (RELATIONAL , LOGICAL)

 == Equal to
 ~= Not equal to

 < Strictly smaller

 > Strictly greater

 <= Smaller than or equal to

 >= Greater than equal to

 & And operator

 | Or operator
MATRICES OPERATIONS
>>A = [1 2 3; 5 1 4; 3 4 -1]
A=
1 2 3
5 1 4
3 4 -1

Strictly Smaller Strictly Greater Equal to


>> A<3 >> A >2 >> A ==1
A= A= A=
1 1 0 0 0 1 1 0 0
0 1 0 1 0 1 0 1 0
0 0 1 1 1 0 0 0 0

Smaller than or equal to Greater than or equal to Not equal to


>> A<=3 >> A >=2 >> A ~=1
A= A= A=
1 1 1 0 1 1 0 1 1
0 1 0 1 0 1 1 0 1
1 0 1 1 1 0 1 1 1
MATRICES OPERATIONS
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9

90 degree CCW rotation Flip up down Flip left right


>> X = rot90(A) >> Y = flipud(A) >> Z = fliplr(A)
X= Y= Z=
3 6 9 7 8 9 3 2 1
2 5 8 4 5 6 6 5 4
1 4 7 1 2 3 9 8 7

Lower Triangular matrix Upper Triangular matrix


>> M = tril(A) >> K = triu(A)
M= K=
1 0 0 1 2 3
4 5 0 0 5 6
7 8 9 0 0 9
SOLVING LINEAR EQUATIONS:
Let the equations be:
 2x – 3y = 1

 x+y=4

In matrix form

2  3  x  1 
1 1   y  =  4 
     
>> A = [2 -3 ; 1 1];
>> C = [1 ; 4];
>> format short
>> B = A/C
>> B
B=
B=
13/5
2.6
7/5
1.4
POLYNOMIALS AND ROOTS:
 Finding roots of equation:
let the equation be 𝑥 2 + 5𝑥 + 6
>>coef = [1 5 6]
coef =
1 5 6
>>roots(coef)
ans =
-3.0000
-2.0000

 If roots are known, polynomial can be find as


>>roots = [3 4]
roots=
3 4
>>poly(roots)
ans =
1 -7 12
TRANSFER FUNCTION:

Defining Transfer function :

Method 1 Method 2

>> s = tf(‘s’) >>G= tf([1,4],[1,6,9])


s=
s G=
>> Sys = 1/(s+3)^3 𝒔+𝟒
𝒔𝟐 +𝟔𝒔+𝟗
Sys =
𝟏
𝒔𝟑 +𝟑𝒔𝟐 +𝟑𝒔+𝟏
TRANSFER FUNCTION AND SOME
RESPONSE:

>>G= tf([1,4],[1,6,9])

G=
𝒔+𝟒
𝒔𝟐 +𝟔𝒔+𝟗

>> step(G)

>> impulse(G)
FLOW CONTROL
 if
 for
 while
 break
 ….
CONTROL STRUCTURES:
Some Examples
If Statement Syntax if ((a>3) & (b==5))
if (Condition_1) Some Matlab Commands;
Matlab Commands end
elseif (Condition_2) if (a<3)
Matlab Commands
Some Matlab Commands;
elseif (Condition_3) elseif (b~=5)
Matlab Commands Some Matlab Commands;
end
else
Matlab Commands
if (a<3)
end Some Matlab Commands;
else
Some Matlab Commands;
end
CONTROL STRUCTURES

Some Examples
For loop syntax for i=1:100
Some Matlab Commands;
end
for i = Index_Array
for j=1:3:200
Some Matlab Commands;
Matlab Commands end

for m=13:-0.2:-21
end Some Matlab Commands;
end

for k=[0.1 0.3 -13 12 7 -9.3]


Some Matlab Commands;
end
CONTROL STRUCTURES

While Loop Syntax

while (condition)
Matlab Commands
end

Example

while ((a>3) & (b==5))


Some Matlab Commands;
end
USE OF M-FILE

Click to create
a new M-File

• Extension “.m”
• A text file containing script or function or program to
run.
Use of M-File Save file as Untitled.m

If you include “;” at the


end of each statement,
result will not be shown
immediately
WRITING USER DEFINED FUNCTIONS
 Functions are m-files which can be executed by
specifying some inputs and supply some desired
outputs.
 The code specifies the Matlab that an m-file is actually
a function like
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)

 You should write this command at the beginning of the


m-file and you should save the m-file with a file name
same as the function name.
WRITING USER DEFINED FUNCTIONS
 Examples
 Write a function : out=squarer (A, ind)
 Which takes the square of the input matrix if the input
indicator is equal to 1
 And takes the element by element square of the input

matrix if the input indicator is equal to 2

Same Name
WRITING USER DEFINED FUNCTIONS
Another function which takes an
input array and returns the sum The function sumprod(.) can be called
and product of its elements as from command window or an m-file as
Outputs.
PLOT THE FUNCTION SIN(X) BETWEEN
0≤X≤4Π
 Create an x-array of 100 samples between 0 and 4π.

 Calculate sin(.) of the x-array


>>x=linspace(0,4*pi,100); 1

0.8

 Plot the y-array 0.6

0.4

0.2

>>y=sin(x); 0

-0.2

-0.4

-0.6

>>plot(y) -0.8

-1
0 10 20 30 40 50 60 70 80 90 100
GRAPH PLOTTING:
Plot command is used to produce graphs.
Syntax : plot(xvalues, yvalues, ’style option’)

PLOT(X,Y) plots vector Y versus vector X. X is a row vector and


Y is a column vector. Also x-axis data and y-axis data must be of
same length.

B blue . Point - solid


G green o circle : dotted
R red x x-mark -. Dash dot
C cyan + plus -- dashed
M magenta * star
Y yellow s square
K black d diamond
W white v triangle(down)
^ triangle(up)
< triangle(left)
> triangle(right)
p pentagram
h hexagram
Example: plot(X,Y,’C+:’) plots a cyan dotted line with a plus at
each data point.
PLOT THE FUNCTION E-X/3SIN(X) BETWEEN
0≤X≤4Π
 Create an x-array of 100 samples between 0 and 4π.
>>x=linspace(0,4*pi,100);

 Calculate sin(.) of the x-array


>>y=sin(x);

 Calculate e-x/3 of the x-array


>>y1=exp(-x/3);

 Multiply the arrays y and y1

>>y2=y*y1;
PLOT THE FUNCTION E-X/3SIN(X) BETWEEN
0≤X≤4Π
 Multiply the arrays y and y1 correctly

>>y2=y.*y1; 0.7

0.6

0.5

 Plot the y2-array 0.4

0.3
>>plot(y2)
0.2

0.1

-0.1

-0.2

-0.3
0 10 20 30 40 50 60 70 80 90 100
DISPLAY FACILITIES
 plot(.)

Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(x,y)

 stem(.)

Example:
>>stem(y)
DISPLAY FACILITIES
 title(.)
This is the sinus function
>>title(‘This is the sinus function’) 1

0.8

 xlabel(.) 0.6

0.4

0.2
>>xlabel(‘x (secs)’)

sin(x)
0

-0.2
 ylabel(.) -0.4

-0.6
>>ylabel(‘sin(x)’)
-0.8

-1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
PLOT THE RESPONSE OF SERIES R-L
CIRCUIT WITH R=10Ω , L=2H, V=10V,
I(0)=0
AN INTERESTING FUNCTION:

MATLAB can laugh like us:

load laughter Enter

sound(y,Fs) Enter
THE END

Das könnte Ihnen auch gefallen