circuit analysis in matlab

34
Circuit Analysis In Matlab

Upload: rana-usman

Post on 22-Oct-2015

90 views

Category:

Documents


9 download

DESCRIPTION

This Slide illustrates how we can use MATLAB to solve electric circuits

TRANSCRIPT

Page 1: Circuit Analysis in Matlab

Circuit Analysis In Matlab

Page 2: Circuit Analysis in 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.

Introduction to MATLAB

Page 3: Circuit Analysis in Matlab

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.

Page 4: Circuit Analysis in Matlab

Resistive Circuits• In resistive circuits there are generally two techniques to solve

the circuits.• Nodal Analysis• Mesh/Loop Analysis

Page 5: Circuit Analysis in Matlab

Nodal AnalysisKirchhoff’s 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 equationsIf 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

Page 6: Circuit Analysis in Matlab

• ∑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 don in a few lines.

Page 7: Circuit Analysis in Matlab

• Example:

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

Page 8: Circuit Analysis in Matlab

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

Page 9: Circuit Analysis in Matlab

The MATLAB program for solving the nodal voltages is

Page 10: Circuit Analysis in Matlab
Page 11: Circuit Analysis in Matlab

Mesh analysis• Loop analysis is a method for obtaining loop currents. The

technique uses Kir-choff voltage law (KVL) to write a set of independent simultaneous equations.

• The Kirchoff voltage law states that the algebraic sum of all the voltages around any closed path in a circuit equals zero.

Page 12: Circuit Analysis in Matlab

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

Page 13: Circuit Analysis in Matlab

Example:

Page 14: Circuit Analysis in Matlab

Loop Analysis

Page 15: Circuit Analysis in Matlab
Page 16: Circuit Analysis in Matlab

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

Page 17: Circuit Analysis in Matlab

RC Circuits• Consider RC network as shown in figure.

Page 18: Circuit Analysis in Matlab

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')

Page 19: Circuit Analysis in Matlab
Page 20: Circuit Analysis in Matlab

RL circuit• Consider the fig

Page 21: Circuit Analysis in Matlab

ExampleFor 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

Page 22: Circuit Analysis in Matlab

• 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().

Page 23: Circuit Analysis in Matlab
Page 24: Circuit Analysis in Matlab

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.

Page 25: Circuit Analysis in Matlab

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 orderfor the circuit to be underdamped, the resistance value must satisfy

or R > 5 Ohms. When R = 5 Ohms, the circuit is critically damped. Wewill 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

Page 26: Circuit Analysis in Matlab

• 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

Page 27: Circuit Analysis in Matlab

>> 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

Page 28: Circuit Analysis in Matlab

>> 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-');

Page 29: Circuit Analysis in Matlab

• >>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');

Page 30: Circuit Analysis in Matlab

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.

Page 31: Circuit Analysis in Matlab

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.

Page 32: Circuit Analysis in Matlab

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

Page 33: Circuit Analysis in Matlab

>> 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')____________________

Page 34: Circuit Analysis in Matlab