0% found this document useful (0 votes)
96 views58 pages

Matlab Basic Operation

MATLAB provides tools for numerical computation, visualization, and programming. It allows users to perform matrix operations, plot functions and data, implement algorithms, and test ideas. Some key MATLAB operations include entering and manipulating matrices, performing element-wise and linear algebraic operations on matrices, using colon notation to generate arrays, and accessing specific elements or submatrices of a larger matrix. MATLAB is a useful tool for scientific and technical computing applications.

Uploaded by

Zar Lay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views58 pages

Matlab Basic Operation

MATLAB provides tools for numerical computation, visualization, and programming. It allows users to perform matrix operations, plot functions and data, implement algorithms, and test ideas. Some key MATLAB operations include entering and manipulating matrices, performing element-wise and linear algebraic operations on matrices, using colon notation to generate arrays, and accessing specific elements or submatrices of a larger matrix. MATLAB is a useful tool for scientific and technical computing applications.

Uploaded by

Zar Lay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

MATLAB BASIC

OPERATION

Lecture by : Daw Wint Yu Yu Zaw


20.12.2018

1
What is Matlab?
 The name of Matlab stands for Matrix
Laboratory.

MATrix LABoratory = MATLAB

2
Introduction
MATLAB

Numerical computation Visualization

Matrix algebra Block diagram


Complex arithmetic
Linear system Simulation
Differential equation
Non linear system
Other types of scientific computations

3
MATLAB Basic Operations
When MATLAB is invoked, The command window
will display the prompt >> MATLAB is ready for
entering data or executing commands.

To quit MATLAB, type the command exit or quit.

You can exit the program by choosing EXIT


MATLAB from the File menu or by selecting the
close icon ( x ) at the upper right-hand corner of the
screen.
4
Default MATLAB Screen

5
Default MATLAB Screen

6
MATLAB statements
MATLAB statements are normally of the form:

variable = expression
Example
A matrix

may be entered as follows:

A = [1 2 3; 2 3 4; 3 4 5]
7
Command Line Editing
If you make a mistake when entering a matlab
command, you do not have to type the whole line
again. The arrow keys can be used to save much
typing:
↑ ctrl-p Recall previous line
↓ ctrl-n Recall next line
← ctrl-b Move back one character
→ ctrl-f Move forward one character

8
Operators

9
Examples

10
Examples
>> 4 + 3
ans =
7

>> 3 - 3
ans =
0

>> 4 * 22 + 6 * 48 + 2 * 82
ans=
540

11
Examples

12
Practice Exercise

13
Some Basic MATLAB Commands
Command Description

% Comments. Everything appear in


after % command is not executed.
clear Clears the variables or functions
from workspace.
clc Clears the command window
during a work session.

14
Matrix Operations
Typing Matrices
to type a matrix into matlab you must begin
with a square bracket [
separate elements in a row with commas or
spaces
use a semicolon ; to separate rows and
execute but not shown
end the matrix with another square bracket ].

15
Example:

A = [1 2 3; 2 3 4; 3 4 5]

OR

A=[1 2 3
234
3 4 5]

16
The basic matrix operations are addition(+),
subtraction(-), multiplication (*), and conjugate
transpose(‘) of matrices.
MATLAB has two forms of matrix division: the left
inverse operator \ or the right inverse operator /.
Note that if a variable name and the “=” sign are
omitted, a variable name ans is automatically created.
Matrix division can either be the left division operator
\ or the right division operator /. The right division a/b,
for instance, is algebraically equivalent to while the
left division a\b is algebraically equivalent to
17
MATLAB as
>> E = [7 2 3; 4 3 6; 8 1 5];
>> F = [1 4 2; 6 7 5; 1 9 1];
and
>> G = E - F
>> H = E + F
then, matrices G and H will appear on the screen as
G=
6 -2 1
-2 -4 1
7 -8 4
H=
8 6 5
10 10 11
9 10 6
18
>> Q = E*F
results as
Q=
22 69 27
28 91 29
19 84 26

>> 2*Q
gives
ans =
44 138 54
56 182 58
38 168 52

19
If Z * I = V and Z is non-singular, the left division,
Z\V is equivalent to MATLAB expression

I = inv (Z) * V

where inv is the MATLAB function for obtaining


the inverse of a matrix. The right division denoted by
V/Z is equivalent to the MATLAB expression

I = V * inv (Z)

20
Transpose
B = [6 9 12 15 18];
C = B’

The above results in


C=
6
9
12
15
18

21
Array Operations
An array of elements (numbers or characters) in rows
and columns is called matrix.
Array operations refer to element-by-element
arithmetic operations.
The linear algebraic matrix operations, * / \ ‘ , by a
period (.) indicates an array or element-by-element
operation.
Thus, the operators .* , .\ , ./, .^ , represent element-by-
element multiplication, left division, right division, and
raising to the power, respectively.

22
Example:
A1 = [2 7 6
8 9 10];
B1 = [6 4 3
2 3 4];
then
C1 = A1.*B1
results in
C1 =
12 28 18
16 27 40
23
The statement
D1 = A1./B1
gives the result
D1 =
0.3333 1.7500 2.0000
4.0000 3.0000 2.5000
and the statement
E1 = A1.\B1
gives
E1 =
3.0000 0.5714 0.5000
0.2500 0.3333 0.4000

24
If r1 and s1 are matrices of the same dimensions, then
the result q is also a matrix of the same dimensions.
For example, if
r1 = [ 7 3 5];
s1 = [ 2 4 3];
then
q1 = r1.^ s1
gives the result
q1 =
49 81 125

25
Element-by-element operations

26
Complex Numbers
MATLAB allows operations involving complex
numbers. Complex numbers are entered using
function i or j.
MATLAB as
z = 2+2*I , z = 2 + I * 2
or
z = 2+2*j , z = 2 + j * 2
 real(z)
 imag (z)
 abs(z)
 angle(z) * 180/pi % degree
27
Also, a complex number za
za = 2 √2 exp[(π / 4) j]

can be entered in MATLAB as


>> za = 2*sqrt(2)*exp((pi/4)*j)

Practice…….

28
If w is a complex matrix given as

then we can represent it in MATLAB as


w = [1+j*1 2-2*j; 3+2*j 4+3*j]
which will produce the result
w=
1.0000 + 1.0000i 2.0000 - 2.0000i
3.0000 + 2.0000i 4.0000 + 3.0000i

29
The Colon Symbol (:)
Step
Used in matrix
All
Any or with step
The statement
>> t1 = 1:6
will generate a row vector containing the numbers
from 1 to 6 with unit increment.
MATLAB produces the result
t1 =
1 2 3 4 5 6
30
Non-unity, positive or negative increments, may be specified.
For example, the statement
>> t2 = 3:-0.5:1
will result in
t2 =
3.0000 2.5000 2.0000 1.5000 1.0000
The statement
>> t3 = [(0:2:10);(5:-0.2:4)]
will result in a 2-by-4 matrix
t3 =
0 2.0000 4.0000 6.0000 8.0000 10.0000
5.0000 4.8000 4.6000 4.4000 4.2000 4.0000

31
>> a = [1 2 3;4 5 6;7 8 9] >> a(2,:)
ans =
4 5 6
a= >> a(:,3)
1 2 3 ans =
3
4 5 6
6
7 8 9 9
>> a(3,2)
ans =
8
>> a(2:3,3)
ans =
32
Practice Exercise
M=[1 3 5 7 11; 13 17 19 23 29; 31 37 41 47 53]
Gives
M=
1 3 5 7 11
13 17 19 23 29
31 37 41 47 53

To find the size of the matrix (i.e., the number of rows and columns),
enter:
>> size(M)
gives
ans =
35
33
To view a particular element, for example, the (2, 4)
element, enter:
>> M(2,4)
gives
ans =
23
To view a particular row such as the 3rd row, enter:
>> M(3,:)
gives
ans =
31 37 41 47 53
34
To view a particular column such as the 4th column,
enter:
>> M(:,4)
gives
ans =
7
23
47

35
If we wanted to construct a submatrix of the original
matrix, for example, one that includes the block from
the 2ndto 3rd row (included) and from the 2nd column
to the 4th column (included), enter:
>> M(2:3,2:4)
gives
ans =
17 19 23
37 41 47

36
Output Format

37
38
Output Commands
disp displays a string or a matrix in the
command window
fprintf creates formatted output which can
be sent to the command window or to a file
Character String
A sequence of characters in single quotes is called a
character string or text variable.

Examples
 disp (‘The output is’) OR disp([g, dx, x])
 fprintf (‘Area = %7.3f square meters\n’,pi*4.5^2)
39
EXERCISE

The voltage, v, across a resistance is given as


(Ohm’s Law), v = Ri , where i is the current and R the
resistance. The power dissipated in resistor R is given
by the expression
If R = 10 Ohms and the current is increased from 0
to 10 A with increments of 2A, write a MATLAB
program to generate a table of current, voltage and
power dissipation.

40
Solution:
 % Voltage and power calculation
>> R=10; % Resistance value
>> i=(0:2:10); % Generate current values
>> v=i.*R; % array multiplication to obtain
voltage
>> p=(i.^2)*R; % power calculation
>> sol=[i v p] % current, voltage and power
values are printed

41
MATLAB produces the following result:
sol =
Columns 1 through 6
0 2 4 6 8 10

Columns 7 through 12
0 20 40 60 80 100

Columns 13 through 18
0 40 160 360 640 1000

Columns 1 through 6 constitute the current values, columns 7


through 12 are the voltages, and columns 13 through 18 are the power
dissipation values.

42
M-Files
Script Files
Function Files

Script files - Script files are especially useful for


analysis and design problems that require long
sequences of MATLAB commands. The file can be
invoked by entering the name of the m-file.

Function Files - Function files are m-files that are used


to create new MATLAB functions.
43
Example (1)
Simplify the complex number z and express it both in rectangular
and polar form.

+
MATLAB Script
1. Z1 = 3+4*j; Z2 = 5+2*j; Z3 = 2*exp(j*theta); Z4 = 3+6*j;Z5 =
1+2*j;
2. theta = (60/180)*pi; % angle in radians
3. disp('Z in rectangular form is'); % displays text inside brackets
4. Z_rect = Z1*Z2*Z3/(Z4+Z5);
5. Z_rect
6. Z_mag = abs (Z_rect); % magnitude of Z
7. Z_angle = angle(Z_rect)*(180/pi); % Angle in degrees
8. disp('complex number Z in polar form, mag, phase is'); % displays
text
9. Z_polar = [Z_mag, Z_angle] % inside brackets
44
The program is named ex1.m. Execute it by typing
ex1 in the MATLAB command window.
Observe the result, which should be
>> Z in rectangular form is
>> Z_rect =
1.9108 + 5.7095i
>> Complex number Z in polar form mag, phase is
>> Z_polar =
6.0208 71.4966

45
Example (2)
Write a MATLAB script or function to obtain the roots
of the quadratic equation
ax^2 + bx + c = 0
MATLAB Script
1. a = 2; b = 2; c = 3;
2. d = b^2 – 4*a*c;
3. x1 = (-b + sqrt(d))/(2*a);
4. x2 = (-b – sqrt(d))/(2*a);
5. X = [x1;x2] or [x1 x2]

46
Save file name.m
Command Window
>> file name
>> -0.5+1.1180i
-0.5-1.1180i

47
Function
1. function X = quard (a,b,c);
2. d = b^2 – 4*a*c;
3. x1 = (-b + sqrt(d))/(2*a);
4. x2 = (-b – sqrt(d))/(2*a);
5. end

Save quard.m
Note: function name = file name
Command Window
X = quard ( 2,2,3)

48
Plotting Commands
GRAPH FUNCTIONS
MATLAB has built-in functions that allow one to generate ba x-
y plots, 3-D plots, and bar charts. MATLAB also allows one to give
titles to graphs, label the x- and y-axes, and add a grid to graphs.

X-Y PLOTS AND ANNOTATIONS


The plot command generates a linear x-y plot. There are three
variations of the plot command.
(a) plot(x)
(b) plot(x, y)
(c) plot(x1, y1, x2, y2, x3, y3, ..., xn, yn)

49
Plotting Functions
FUNCTION DESRIPTION
axis freezes the axis limits
bar plots bar chart
grid adds grid to a plot
hold holds plot (for overlaying other plots)
mesh performs 3-D mesh plot
plot performs linear x-y plot
text positions text at a specified location on graph
title used to put title on graph
xlabel labels x-axis
ylabel labels y-axis
legend adds a legend to a graph
50
If x is a vector, the command
plot(x)
For example, if
>>x = [ 0 3.7 6.1 6.4 5.8 3.9 ];
>>plot(x)
then, plot(x) results in the graph shown in Figure.

Figure: Graph of a Roll Vector x


51
If x and y are vectors of the same length, then the
command
plot(x, y)

For example, the MATLAB commands


>>t = 0:0.5:4;
>>y = 6*exp (-2*t);
>>plot( t, y)
>>title('Response of an RC circuit')
>>xlabel('time in seconds')
>>ylabel('voltage in volts')
>>grid
52
The plot is shown in Figure.

Figure: Graph of Voltage versus Time of a Response of an RLC Circuit

53
 For systems that support color, the color of the graph may be specified
using the statement:
plot(x, y, ’colour’)

 Line and mark style may be added to color type using the command

plot(x, y, ’colour,style,marker’)

 Symbols for Color Used in Plotting


COLOR SYMBOL
red r
green g
blue b
white w
invisible i
black k
cyan c
54
Print Types
LINE-TYPES INDICATORS POINTTYPES INDICATORS
solid - point .
dash -- plus +
dotted : star *
dash dot -. circle o
x-mark x

55
Example
For an R-L circuit, the voltage v(t) and current i(t) are
given as

Sketch v(t) and i(t) for t = 0 to 20 milliseconds.

56
MATLAB Script Solution
% RL circuit
% current i(t) and voltage v(t) are generated; t is time
t = 0:1e-3:20e-3; v = 10*cos(377*t);
a_rad = (60*pi/180); % angle in radians
i = 5*cos(377*t + a_rad);
plot(t, v, ‘*’, t, i, 'o')
title('Voltage and Current of an RL circuit')
xlabel('Sec')
ylabel('Voltage(V) and Current(mA)')
text(0.003, 1.5, 'v(t)');
text(0.009,2, 'i(t)')
57
Figure :Plot of Voltage and Current of an RL Circuit under Sinusoidal Steady State
Conditions

58

You might also like