You are on page 1of 38

Circuit Analysis In Matlab

Introduction to MATLAB
MATLAB (short for MATrix LABoratory) is a special-purpose computer program optimized to perform engineering and scientific calculations. The language was originally developed in the 1970s for applications involving matrices, linear algebra, and numerical analysis. MATLAB has a number of add-on software modules, called toolboxes, that perform more specialized computations.

Circuit Analysis In MATLAB


Matlab can be used to analyze the electric circuits by avoiding the lengthy and boring calculations. We can plot the V,I etc quantities . Generally there are three types of circuits . Resistive circuits RL/RC circuits RLC circuits In addition we will also discuss the bode plot.

Resistive Circuits
In resistive circuits there are generally two techniques to solve the circuits. Nodal Analysis Mesh/Loop Analysis

Nodal Analysis
Kirchhoffs current law states that for any electrical circuit the algebraic sum of all the currents at any node in the circuit equals zero. In nodal analysis, if there are n nodes in a circuit, and we select a reference node, the other nodes can be numbered from V1through Vn-1. With one node selected as the reference node, there will be n-1 independent equations If we assume that the admittance between nodes i and j is given as Yij, we can write the nodal equations in the form of following general equations

Ym1V1+ Ym2V2 + + Ymm Vm= Im.


where m = n - 1

Im is the algebraic sum of current sources at node m. Vm is Voltage


That equation can be expressed in matrix form as [Y][ V] =[I] The solution of the above equation is [V] = [Y]-1 [I] [Y] -1 is an inverse of [Y]. In MATLAB, we can compute [V] by using the command V=inv(Y) * (I) Now if we avoid MATLAB then we will have to calculate the above equation using a long procedure for answers but using MATLAB it can be just done in a few lines.

Example:

For the circuit shown ,find the nodal voltages V1 ,V2, and V3

Using KCL and assuming that the currents leaving a node are positive, we have At node 1,

The MATLAB program for solving the nodal voltages is

Mesh analysis
Loop analysis is a method for obtaining loop currents. The technique uses Kirchhoff voltage law (KVL) to write a set of independent simultaneous equations. The Kirchhoff voltage law states that the algebraic sum of all the voltages around any closed path in a circuit equals zero.

Loop Analysis
Equation can be expressed in matrix form as [Z][I] = [V] The solution to above equation is

Example:

Loop Analysis

Matlab Answers are the current through RB is 0.037 Amps the power supplied by 10V source is 4.7531 watts

RC Circuits
Consider RC network as shown in figure.

Example
Assume that for Figure C= 10 F, use MATLAB to plot the voltage across the capacitor if R is equal to 1.0 k. MATLAB Script % Charging of an RC circuit % c = 10e-6; r = 1e3; tau = c*r; t = 0:0.002:0.05; v = 10*(1-exp(-t/tau)); plot(t,v1,'*') axis([0 0.06 0 12]) title('Charging of a capacitor ') xlabel('Time, s') ylabel('Voltage across capacitor') text(0.03, 5.0, '+ for R = 1 Kilohms')

RL circuit
Consider the fig

Example
For the sequential circuit shown in Figure , the current flowing through the inductor is zero. At t= 0, the switch moved from position a to b, where it remained for 1 s. After the 1 s delay, the switch moved from position b to position c, where it remained indefinitely. Sketch the current flowing through the inductor versus time. MATLAB Script % Solution to Example % tau1 is time constant when switch is at b % tau2 is the time constant when the switch is in position c % tau1 = 200/100; for k=1:20 t(k) = k/20; i(k) = 0.4*(1-exp(-t(k)/tau1)); end Imax = i(20); tau2 = 200/200; for k = 21:120

t(k) = k/20; i(k) = Imax*exp(-t(k-20)/tau2); end % plot the current plot(t,i,'o') axis([0 6 0 0.18]) title('Current of an RL circuit') xlabel('Time, s') ylabel('Current, A') Figure 5.8 shows the current it().

RLC Circuits
The purpose of this MATLAB example is to explore the effects of varying the resistance value in the underdamped parallel RLC circuit. Consider the natural response of the parallel RLC circuit shown in Figure. The homogeneous second order differential equation for the

voltage across all three elements is given by Depending on the element values, the circuit will be either overdamped, critically damped, or underdamped.

Suppose the inductance and capacitance values are L = 0.1 H and C= 1 mF with initial values v n(0) = 10 V and i L(0) = -0.6 A. In order for the circuit to be underdamped, the resistance value must satisfy

or R > 5 Ohms. When R = 5 Ohms, the circuit is critically damped. We will therefore examine the behavior of this circuit for resistance values greater than 5 Ohms. We will now explore the solution for v n(t) for various values of R. Given a value for R, the solution to the underdamped differential equation is obtained by solving for the exponential coefficient

the resonant angular frequency

the damped resonant angular frequency With R = 25/3 Ohms and the L and C values given above, the solution for the voltage v n(t) is

How does the solution for v n(t) change as the value of R is varied MATLAB SCRIPT: >> L=0.1; >> C=0.001; >> R=25/3; >> a=1/(2*R*C) a= 60.0000 >> w0=1/sqrt(L*C) w0 = 100

>> wd=sqrt(w0^2 - a^2) wd = 80.0000 >> B1=10; >> B2=(a/wd)*B1 - 10/(wd*R*C) + 0.6/(wd*C) B2 = -1.7764e-15 >> t=0:0.001:0.12; >> v=B1*exp(-a*t).*cos(wd*t) + B2*exp(-a*t).*sin(wd*t); >> hold off >> plot(1000*t,v,'b+-') >> hold on

>> R=20; >> a=1/(2*R*C); >> wd=sqrt(w0*w0 - a*a); >> B2=(a/wd)*B1 - 10/(wd*R*C) + 0.6/(wd*C); >> v=B1*exp(-a*t).*cos(wd*t) + B2*exp(-a*t).*sin(wd*t); >> plot(1000*t,v,'mo-'); >> R=50; >> a=1/(2*R*C); >> wd=sqrt(w0*w0 - a*a); >> B2=(a/wd)*B1 - 10/(wd*R*C) + 0.6/(wd*C); >> v=B1*exp(-a*t).*cos(wd*t) + B2*exp(-a*t).*sin(wd*t); >> plot(1000*t,v,'kx-'); >> R=100; >> a=1/(2*R*C); >> wd=sqrt(w0*w0 - a*a); >> B2=(a/wd)*B1 - 10/(wd*R*C) + 0.6/(wd*C); >> v=B1*exp(-a*t).*cos(wd*t) + B2*exp(-a*t).*sin(wd*t); >> plot(1000*t,v,'rd-');

>>legend('R=25/3','R=20','R=50','R=100') >> ylabel('v_n(t), V'); >> xlabel('t, ms'); >> title('Natural Response of an Underdamped Parallel RLC Circuit');

Bode Plot
MATLAB can be used to display the Bode plot or frequency response plot corresponding to a network function. As an example, consider the network function.

MATLAB input file that can be used to obtain the Bode plot corresponding to this network function. This MATLAB file consists of four parts. In the first part, the MATLAB command logspace is used to specify the frequency range for the Bode plot. The command logspace also provides a list of frequencies that are evenly spaced (on a log scale) over this frequency range.

The given network has four parameters -- the gain K, the zero z, and two pole p1 and p2. The second part of the MATLAB input file specifies values for these four parameters. The third part of the MATLAB input file is for a loop that evaluates H(w ), | H(w )|, and H(w ) at each frequency in the list of frequencies produced by the command logspace. The fourth part of the MATLAB input file does the plotting. The command semilogx ( w/2*pi, 20*log10(mag) ) does several things. The command semilogx indicates that the plot is to be made using a logarithmic scale for the first variable and a linear scale for the second variable. The first variable, frequency, is divided by 2p to convert to Hz. The second variable, | H(w )|, is converted to dB. The second and third parts of the MATLAB input file can be modified to plot the Bode plots for a different network function.

MATLAB Script: % Set up values for the angular frequency using the logspace command. Note that logspace will create an array with 50 entries if you do not specify a number. >> wmin=1; >> wmax=100000; >> w = logspace(log10(wmin), log10(wmax)); % Set up network parameters, then calculate the values for the network function, the magnitude of the network function, and the phase of the network function. The code here uses MATLAB's built in vectorizing ability. The .* and ./ operators are element multiplication and division. This means that each element in the solution vector is made by multiplying or dividing corresponding elements in the first and second vector. For example, if a=[1 2 3] and b=[4 5 6], a.*b will give [4 10 18] while a*b w ill give an error. a*b represents regular matrix multiplication, and for that the number of columns in the first array must equal the number of rows in the second

>> K = 10; >> z = 100; >> p1 = 10; >> p2 = 1000; >> H = K*(1+j*w/z)./((1+j*w/p1).*(1+j*w/p2)); >> mag = abs(H); >> phase = angle(H); % Finally, make the plot >> subplot(2,1,1) >> semilogx(w/(2*pi), 20*log10(mag)) >> set(gca, 'TickLength',[.03 .02]) >> xlabel('Frequency, Hz') >> ylabel('Gain, dB') >> title('Bode Plot') >> axis([10^0, 10^5, -20, 20]) >> subplot(2,1,2) >> set(gca, 'TickLength',[.03 .02]) >> semilogx(w/(2*pi), phase) >> xlabel('Frequency, Hz') >> ylabel('Phase, rad') ____________________

Bode Plot Using built-In commands


%% Circuit constants R = 10000; C = 22e-9; %% Set up transfer function % Create "s" as a transfer function for use later s = tf('s'); % Use s to generate transfer function for circuit H = (s * R * C) / (s * R * C + 1); % Use bode command to analyze transfer function bode(H); title('Bode plots for System H');

Circuit Analysis in Simulink


Simpowersystem and simscape are two libraries that can be used to model simple electronics circuit including RC ,RL ,series RLC and Parallel RLC as well as three phase delta-wye connections.

References
Electronics and Circuit analysis using MATLAB by

JOHN O.

ATTIA
http://electronics.stackexchange.com/questions/28516/matla b-and-rlc-analysis?rq=1 www.mathworks.com Matlab help Lecture slides

You might also like