In this blog we will learn to simulate a circuit using MATLAB Programming which is the basic step to create your own circuit solver. The circuit we are going to solve is shown in Fig 1. Here V is 6V.

You need to first represent this circuit in terms of matrixes so that you can use them in MATLAB. These two matrixs will describe the network- NetList and SourceList

Let us first give a number to all the nodes in the circuit. Two nodes should be separated by atleast an element.

The considered circuit has 4 nodes and let us choose node 4 as the reference/ground node.

Now create a matrix named as NetList with three columns N1, N2 and R. Each row of this matrix will represent a R resistance element between Node N1 and N2.

Now create a matrix named SourceList with two columns N and V. Each row of this matrix will represent a N node with V voltage. Since only two node voltages are know, this will contain two rows.

If Total Nodes are N

A is of N x N

Aij=

-1/Rij where Rij is the resistance between i and j (if i!=j)

Sum of inverse of all resistance connected to i (if i==j)

Aii=1 and

rest Aik=0

If Total Nodes are N

B is of N x 1

Bi=

0

Voltage of that source Node

Using the above nodal equation can be represented as

\[\mathbf{AV=B}\]\[\mathbf{V}=[V_1 V_2 ... V_N]^T\]\[V=A^{-1}B\]

This V represent the Voltage of all the nodes, thus circuit is solved.

Fig 1: Circuit |

**Create NetList and SourceList**You need to first represent this circuit in terms of matrixes so that you can use them in MATLAB. These two matrixs will describe the network- NetList and SourceList

Let us first give a number to all the nodes in the circuit. Two nodes should be separated by atleast an element.

The considered circuit has 4 nodes and let us choose node 4 as the reference/ground node.

Now create a matrix named as NetList with three columns N1, N2 and R. Each row of this matrix will represent a R resistance element between Node N1 and N2.

Now create a matrix named SourceList with two columns N and V. Each row of this matrix will represent a N node with V voltage. Since only two node voltages are know, this will contain two rows.

Fig 2: NetList and Source List |

**Create A and B matrix**If Total Nodes are N

A is of N x N

Aij=

**Case 1: if i is not a Source Nodes**-1/Rij where Rij is the resistance between i and j (if i!=j)

Sum of inverse of all resistance connected to i (if i==j)

**Case 2: if i is a source Node**Aii=1 and

rest Aik=0

If Total Nodes are N

B is of N x 1

Bi=

**Case 1: if i is not a Source Node**0

**Case 2: if i is a source Node**Voltage of that source Node

**Nodal Equations**Using the above nodal equation can be represented as

\[\mathbf{AV=B}\]\[\mathbf{V}=[V_1 V_2 ... V_N]^T\]\[V=A^{-1}B\]

This V represent the Voltage of all the nodes, thus circuit is solved.

**MATLAB Code****Initialization of the matrixs**NetList=[1 2 2; 1 3 4; 2 3 5.2; 3 4 6; 2 4 3];

Vnod=[1 6; 4 2];

l=size(NetList,1);

N=max([NetList(:,1) ;

NetList(:,2)]);

A=zeros(N,N);

B=zeros(N,1);

**A Matrix Creation**for i=1:l

n1=NetList(i,1);

n2=NetList(i,2);

if n1==n2

else

A(n1,n2)=A(n1,n2)-1/NetList(i,3);

A(n2,n1)=A(n2,n1)-1/NetList(i,3);

A(n1,n1)=A(n1,n1)+1/NetList(i,3);

A(n2,n2)=A(n2,n2)+1/NetList(i,3);

end

end

**B Matrix Creation**

for i=1:size(Vnod,1)A(Vnod(i,1),:)=zeros(1,N);A(Vnod(i,1),Vnod(i,1))=1;B(Vnod(i,1),1)=Vnod(i,2);end

**Solution**Vo=A\B;disp(Vo);