MATLAB Tutorial 1, Basic Electronics (CS) Fall 2016
Matrices & Vectors in MATLAB (MATrix LABoratory):
Row Vector:
In linear algebra, a row vector or row matrix is a 1 m matrix, i.e. a matrix consisting of a
single row of m elements.
MATLAB Code:
>> x=[1 2 3 4]
x=
1 2 3 4
OR
>> x =[1,2,3,4]
x=
1
Column Vector:
In linear algebra, a column vector or column matrix is a m 1 matrix, i.e. a matrix consisting of
a single column of m elements.
MATLAB Code:
>> x=[1;2;3;4]
x=
1
2
3
4
OR
>> x=[1
2
3
4]
x=
1
2
3
4
OR
Page 1|7
>> x=[1 2 3 4]
x=
1
2
3
4
Matrix:
A mxn matrix is a collection of numbers arranged into a fixed number of m rows and n
columns.
MATLAB Code:
>> A=[1 2 3; 3 4 5]
A=
1 2 3
3 4 5
OR
>> A=[1, 2, 3
3,4,5]
A=
1 2 3
3 4 5
Note:
Adding a ; after a command suppresses the output, but the variable has been created in the
workspace. Multiple assignments to one variable name overwrite previous variable.
Example 1
For the circuit in (a), values of current and power are given in the table below.
i.
Find voltage and plot graph between voltage and current.
ii.
If the device is 2.2M then calculate power dissipated by the resistor for each value of
V2
voltage using formula p= R
P a g e 2|7
MATLAB Code:
>> i=[50,100,150,200,250,300]
i=
50 100 150 200 250 300
>> i=i*(10^-6)
i=
1.0e-003 *
0.0500 0.1000 0.1500 0.2000
0.2500
0.3000
>> p=[5.5,22,49.5,88,137.5,198]
p=
5.5000 22.0000 49.5000 88.0000 137.5000 198.0000
>> p=p*(10^-3)
p=
0.0055 0.0220 0.0495 0.0880 0.1375 0.1980
>> v=p./i
v=
110.0000 220.0000 330.0000 440.0000 550.0000 660.0000
>> plot(i/(10^-6),v)
P a g e 3|7
p_dissipated=(v.^2)/(2.2*10^6)
p_dissipated =
0.0055 0.0220 0.0495 0.0880
0.1375
0.1980
0.1375
0.1980
OR
>> p_dissipated=(v.*v)/(2.2*10^6)
p_dissipated =
0.0055 0.0220 0.0495 0.0880
Example 2
a) Using MATLAB find voltage V and current I in the circuit given below:
P a g e 4|7
Mesh Current Equations:
130 I 150 I 2=50
50 I 1+ 290 I 2=150
130 50
50 290
] [] [ ]
I1
I2
50
150
MATLAB Code:
>> A=[130,-50;-50,290]
A=
130 -50
-50 290
>> B=[50;-150]
B=
50
-150
>> I_res = inv(A)*B
I_res =
0.1989
-0.4830
>> V = 80*I_res(1)
V=
15.9091
>> I= I_res(1) - I_res(2)
I=
0.6818
P a g e 5|7
b) Simulate the circuit in SIMULINK to fine voltage V and current I in the circuit
Simulation of Circuit in SIMULINK:
P a g e 6|7
Exercise 1
a) Write node voltage equations for the following circuit and solve the equations using
MATLAB to find Io
b) Simulate the circuit given below in SIMULINK and find Io
Exercise 2
Write mesh current equations for the following circuit and solve the equations using
a)
MATLAB to find all mesh currents and Vo
b) Simulate the circuit given below in SIMULINK and find Vo
P a g e 7|7