%____________________________________________________________________________ %%%%%%% Lesson 5: Illustrative Example -- Parallel RLC Circuit %____________________________________________________________________________ % 1) Problem Statement (mimic types of statements you will get in the 6.003 HW) % 2) Basic MATLAB Solution: M-files are rlcstep.m (function) and % rlc1.m (script) % % (Consult the Lesson 5 supplementary handout which describes the analytical % solutions that this solution relies upon) % % 3) Advanced MATLAB solution: M-file is rlc2.m (script) % % (Uses advanced and useful MATLAB commands so solution does not rely on % analytical solution!) %____________________________________________________________________________ %%%%%%% Parallel RLC Circuit: Problem Statement (mimic HW problems) %____________________________________________________________________________ Consider a parallel RLC circuit driven by a current source. Determine the step response for the following sets of component values, and use MATLAB to plot each response from t = [0,T]: Case (i) : R = 1 Ohm, C = 1 Farad, L = 6 Henrys, T = 20 sec Case (ii) : R = 1 Ohm, C = 1 Farad, L = 1 Henry , T = 20 sec Case (iii): R = 1 Ohm, C = 1 Farad, L = 4 Henrys, T = 20 sec Advanced Note: You may find the following commands useful: residue, step % Outline of Solution: We will first demonstrate how to create a % specialized function and script (using M-files) % involving the already familiar commands given in the % Basic Note. This solution method requires that we % first obtain the analytical solution of the step % response in our circuit. % % Then, we will make use of MATLAB's Help features to % learn about the currently unfamiliar, yet very powerful % commands given in the Advanced Note. We will % create another script (M-file) that uses these advanced % commands, and see that we will have saved ourselves % a significant amount of work because we will not % require grinding through the analytical solution! %____________________________________________________________________________ %%%%% Basic Solution %____________________________________________________________________________ % The approach we take here is to define a MATLAB function which takes as % inputs values for R, L, C, and T and gives the corresponding step response % as the output. This function we will call rlcstep.m The equations in % this file come from the analytical solution (consult the Lesson 5 % supplementary handout). % Then, we write a MATLAB script rlc1.m which calls on function % rlcstep.m for each of the three cases, and includes the necessary plot % commands. % % NOTE: While studying this solution, we will also be introduced to MATLAB's % C language-based "control flow" operations (e.g., if-elseif-else % conditional statements, while loops, for loops, etc.). % _______________________________________________________ type rlcstep.m %View the MATLAB function M-file %_______________________________________________________ function V = rlcstep(R,L,C,T) % function V = rlcstep(R,L,C,T) % % % RLCSTEP Function RLCSTEP returns the step response of a parallel RLC % circuit for the given input parameters. Input variables R, L, % and C denote the values of the circuit components: resistor (in % Ohms), inductor (in Henrys), and capacitor (in Farads), % respectively. Output variable V is a row vector representing % the step response v(t) from t = [0,T]. % % This function first checks whether the system poles (natural % frequencies) are real & distinct, complex conjugates, or real % & equal. Depending on which of these cases is true for the % given component values, the correct "analytical" solution is % applied (consult the Lesson 5 supplementary handout). At the % end of this example, we will solve the same problem much more % quickly, without relying on our analytical solution, by using % some of MATLAB's more sophisticated functions. t = linspace(0,T,201); %define the time vector dis = (R*C)^(-2) - 4/(L*C); %calculate the "discriminant" for the poles p1 = -1/(2*R*C) + sqrt(dis)/2; %compute the values for the two poles p2 = -1/(2*R*C) - sqrt(dis)/2; if dis > 0 %Case (i): poles are real, distinct K1 = 1/((p1-p2)*C); V = K1*(exp(p1*t) - exp(p2*t)); elseif dis < 0 %Case (ii): poles are complex conjugates K1 = 1/((p1-p2)*C); V = -2*imag(K1)*exp(real(p1)*t) .* sin(imag(p1)*t); else %Case (iii): poles are real, equal V = (1/C)*t .* exp(p1*t); end %_________________________END OF FUNCTION____________________ %NOTICE: 1) Function header line (distinguishes function from script) % 2) Help section (the many rows of commented lines at top of file) % 3) Use of an if-elseif-else statement % 4) Included in-line comments for future reference %__________________________________________________________________________ rlc1 %Run MATLAB script file % RLC1 MATLAB script file which generates the basic solution to % Lesson 5 of the 6.003 MATLAB "Pico"-Course. This script calls % on function rlcstep.m for each of the three cases, and % includes the necessary plot commands. % Case (i) : R = 1 Ohm, C = 1 Farad, L = 6 Henrys, T = 20 sec % Case (ii) : R = 1 Ohm, C = 1 Farad, L = 1 Henry , T = 20 sec % Case (iii): R = 1 Ohm, C = 1 Farad, L = 4 Henrys, T = 20 sec % Define component values T = 20; R = 1; C = 1; L = [6; 1; 4]; words = ['poles real,distinct'; 'poles complex conj.';'poles real,equal']; % Cycle through the cases, generating all three plots on the same figure for case = 1:3 v(case,:) = rlcstep(R,L(case,1),C,T); %function call subplot(3,1,case),plot( linspace(0,T,size(v,2)), v(case,:) ); xlabel('t (sec)'); ylabel('v(t)'); text(10,0.4,words(case,:)); end subplot(3,1,1) title('Parallel RLC Circuit: Step Response for Three Cases'); clear %____________________________________________________________________________ %%%%% Advanced Solution %____________________________________________________________________________ % The approach we take here is to rely on the powerful functions given in % the advanced note of the problem statement. The first step is to use % MATLAB's Help features to learn exactly how these functions work. Then, % we write a MATLAB script rlc2.m which calls on these functions for % each of the three cases. These functions will allow us to determine % the form of the step response, in addition to plotting them, without % relying on the analytical solutions! %______________________________________________ !clear help residue %Learn about function residue RESIDUE Partial-fraction expansion (residues). [R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term of a partial fraction expansion of the ratio of two polynomials B(s)/A(s). If there are no multiple roots, B(s) R(1) R(2) R(n) ---- = -------- + -------- + ... + -------- + K(s) A(s) s - P(1) s - P(2) s - P(n) Vectors B and A specify the coefficients of the numerator and denominator polynomials in descending powers of s. The residues are returned in the column vector R, the pole locations in column vector P, and the direct terms in row vector K. The number of poles is n = length(A)-1 = length(R) = length(P). The direct term coefficient vector is empty if length(B) < length(A), otherwise length(K) = length(B)-length(A)+1. (...more explanation) See also POLY, ROOTS, DECONV. %______________________________________________ !clear help step %Learn about function step STEP Step response of LTI systems. STEP(SYS) plots the step response of each input channel of the LTI system SYS (created with either TF, ZPK, or SS). The time range and number of points are chosen automatically. STEP(SYS,TFINAL) simulates the step response from t = 0 to the final time t = TFINAL. For discrete-time systems with unspecified sampling time, TFINAL is interpreted as the number of samples. STEP(SYS,T) uses the user-supplied time vector T for simulation. For discrete-time systems, T should be of the form Ti:Ts:Tf where Ts is the sample time of the system. For continuous systems, T should be of the form Ti:dt:Tf where dt will become the sample time of a discrete approximation to the continuous system. The step input is always assumed to start at t = 0 (regardless of Ti). (... more explanation) %______________________________________________ !clear rlc2 %Run MATLAB script file % RLC2 MATLAB script file which generates the advanced solution to % Lesson 5 of the 6.003 MATLAB "Pico"-Course. This script calls % on functions residue.m and step.m for each of the % three cases, and includes the necessary plot commands. % Case (i) : R = 1 Ohm, C = 1 Farad, L = 6 Henrys, T = 20 sec % Case (ii) : R = 1 Ohm, C = 1 Farad, L = 1 Henry , T = 20 sec % Case (iii): R = 1 Ohm, C = 1 Farad, L = 4 Henrys, T = 20 sec % Define component values T = 20; R = 1; C = 1; L = [6; 1; 4]; words = ['poles real,distinct'; 'poles complex conj.';'poles real,equal']; t = linspace(0,T,201); % Cycle through the cases, generating all three plots on the same figure for case = 1:3 Hnum = [1/C 0]; %Define system function Hden = [1 1/(R*C) 1/(L(case,1)*C)]; %To continue lesson, type: return [K,P] = residue(Hnum,Hden) %Determine Partial Fraction Expansion v(case,:) = step(Hnum,Hden,t)'; %function call subplot(3,1,case), hold on plot( t, v(case,:), 'rx'); xlabel('t (sec)'); ylabel('v(t)'); text(10,0.4,words(case,:)); end subplot(3,1,1) title('Parallel RLC Circuit: Step Response for Three Cases'); %______________________________________________ K = %output of for case=1 1.3660 -0.3660 P = -0.7887 -0.2113 K = %output for case=2 0.5000 + 0.2887i 0.5000 - 0.2887i P = -0.5000 + 0.8660i -0.5000 - 0.8660i K = P= %output for case=3 1.0000 -0.5000 -0.5000 -0.5000 %__________________________________________________________________