RC Circuit Starter Code
The following code contains everything necessary to create graphs of the source voltage, the voltage across the capacitor, and the voltage across the resistor for a series \(RC\) circuit as a function of time, except for code to evaluate equations for the important physics. Specifically, you will need to first be sure that you have set the values of the following variables:R
– Resistance in \(\Omega\).C
– Capacitance in Farads.f
– Frequency of source square wave in Hz. Presently set to1/p
wherep
is the period and has been set to \(10 RC\) time constants.a
– Amplitude of the source voltage square wave in Volts.
Then, within the while loop, which has been set to run for three periods of the source square wave, you will need to enter formula for the following quantities:
dq
– The change in the charge on the capacitor during one time stepdt
.i
– The current, which depends on the charge incrementdq
and time stepdt
.vc
– The voltage across the capacitor which depends on the total chargeq
and the capacitanceC
.vr
– The voltage across the resistor which you find by requiring the total voltage drops across the circuit to add to zero.q
– The new charge across the capacitor for the next time step, which depends on the old chargeq
and the increment of chargedq
.
Note that there are a couple of Boolean flags called PrintData
and ShowCurrentGraph
that control whether some of the data values are printed and whether a graph of current versus time is shown respectively. These are initially set to False
. Change either to True
to display the data or graph as desired.
In addition, the time step is presently set to \(dt = \tau/100\). This means that the integration will perform 100 steps for every \(RC\) time constant of the solution. This is likely a reasonable value, but could be reduced for higher integration accuracy if desired.
GlowScript 3.0 VPython # # Experiment 4 - RL and RC Circuits # Activity 4B - Simulating RL and RC Circuits # # This program solves the differential equations that describe the time # dependence of the charge on a capacitor. Once the charge is known then # the potential on the capacitor is computed and plotted. # # The following function is used to generate a square wave with a specified # 'amplitude' and 'frequency'. The function will return one point in the # square wave at a specified time 't'. The square wave is always positive # and ranges from 0 to 'amplitude' and starts at t=0 at the high state. def square(amplitude, frequency, t): # Find the period as the inverse of the frequency period = 1/frequency # The % operator is a modulus operator. A%B returns the remainder that # remains when one performs the division A/B. By evaluating t%period # we how far along we are in the cycle ignoring the fact that we may be # more than 1 cycle into the square wave. if (t%period < period/2): # in the first half of the cycle, so return high state return(amplitude) else: # in last half of the cycle, so return low state return(0) print_options(width=1000, height=300) # Set up a voltage versus time graph to hold graphs of the # 1. Source Voltage (the square wave) in graph 'vsg' # 2. Voltage Across Capacitor in graph 'vcg' # 3. Voltage Across Resistor in graph 'vrg' VoltsVsTime = graph(title = 'Voltage vs Time', width = 1000, height = 400, xtitle = 'Time, t [s]', ytitle='Voltage, V [V]', fast = False) vsg = gcurve(color = color.blue, width = 2, label = 'Source Voltage') vcg = gcurve(color = color.red, width = 2, label = 'Capacitor Voltage') vrg = gcurve(color = color.green, width = 2, label = 'Resistor Voltage') # Set up a graph of current vs time ShowCurrentGraph = False # Set to True to show the Curent Graph if (ShowCurrentGraph): CurrentVsTime = graph(title = 'Current vs Time', width = 1000, height = 400, xtitle = 'Time, t [s]', ytitle='Current, I [A]', fast = False) ig = gcurve(color = color.blue, width = 2, label = 'Current') # Define the resistance and capacitance values for the circuit. R = 33 # Resistance in ohms C = 100e-6 # Capacitance in Farads # Compute the time constant tau as the product of R and C. Units of tau are seconds tau = R*C # SET UP THE SOURCE VOLTAGE SIGNAL # # Define the period, frequency, and amplitude of the input signal. # # Define 'p' as the period of the square wave input signal to the circuit. # Initially this is set at 10*tau which means that the signal will be on for # five time constants and off for five time constants. In this scenerio # there is plenty of time for the capacitor to full charge during the 'on' cycle # and fully discharge during the 'off' cycle. # # You may want to change the period so that you will get a frequency that matches # exactly the value used in your experiment, or simply define the frequency you want # first snd then compute p=1/f in the opposite order of what is done here. p = 10*tau # Period of the square wave (seconds) f = 1/p # Frequency of the square wave (Hz) a = 4.0 # Amplitude of the square wave (volts) # SET UP THE TIME STEP SIZE # # For good integration of the differential equations of the circuit you want # a small step size 'dt' in comparison to the time constant 'tau' of the circuit. dt = tau/100 # dt = time step (seconds) # INITIALIZE PARAMETERS q = 0 # Initial charge (in Coulombs) on the capacitor t = 0 # Initial time value (seconds) # PRINTING OPTIONS # # The PrintData flag can be set to True or False to control whether the data # values are printed in the output window. Printing causes the program to run # slowly so it is initially turned off. Using a small step size (dt) results in # a very large number of data points, way more than we would want to print. # We'll count which data point we are on with the variable n. The variable N # will determine how often we print values. For example, if N = 10 then would # mean we will print every 10th data point PrintData = True # False = Printing off, True = Printing on. n = 0 # Counter for printing purposes N = 10 # Print every Nth data value if (PrintData): print("Simulated Time, t [s]\tSimulated Source Voltage, Vs [V]\tSimulated Capacitor Voltage, Vc [V]\tSimulated Resistor Voltage, Vr [V]\t") while(t < 3*p): # Use the square function to compute a new value for the source voltage, vs vs = square(a,f,t) # Compute how much the charge on the capacitor changes (dq) in one time step dt # Enter a formula here in terms of the source voltage (vs), the resistance (R), # capacitance (C), existing charge (q), and time step (dt). dq = # Compute the current (i) in temrs of the change in charge (dq) and time step (dt) i = # Compute the voltage across the capacitor in terms of the chare (q) and capacitance (c) vc = # Compute the voltage across the resistor by making sure the voltage around the # entire circuit adds to zero vr = # Plot the results vsg.plot(t, vs) # Source Voltage vcg.plot(t, vc) # Capacitor Voltage vrg.plot(t, vr) # Resistor Voltage if (ShowCurrentGraph): ig.plot(t, i) # Print the results if (PrintData): if n%N==0: print("{:f}\t{:f}\t{:f}\t{:f}".format(t,vs,vc,vr)) # Compute the new charge (q) in terms of the old charge (also q) and the amount # the charge changed (dq) in one time step q = # Increment the time by one time step and the line counter t = t + dt n = n + 1
RL Circuit Starter Code
For the RL circuit the integration variable will be current instead of charge. Here is a starter code that you could use to plot the voltage across the inductor and the voltage across the resistor for the RL circuit.
GlowScript 3.0 VPython # # Experiment 4 - RL and RC Circuits # Activity 4B - Simulating RL and RC Circuits # # This program solves the differential equations that describe the time # dependence of the current thru an inductor. # # The following function is used to generate a square wave with a specified # 'amplitude' and 'frequency'. The function will return one point in the # square wave at a specified time 't'. The square wave is always positive # and ranges from 0 to 'amplitude' and starts at t=0 at the high state. def square(amplitude, frequency, t): # Find the period as the inverse of the frequency period = 1/frequency # The % operator is a modulus operator. A%B returns the remainder that # remains when one performs the division A/B. By evaluating t%period # we how far along we are in the cycle ignoring the fact that we may be # more than 1 cycle into the square wave. if (t%period < period/2): # in the first half of the cycle, so return high state return(amplitude) else: # in last half of the cycle, so return low state return(0) print_options(width=1000, height=300) # Set up a voltage versus time graph to hold graphs of the # 1. Source Voltage (the square wave) in graph 'vsg' # 2. Voltage Across Inductor in graph 'vlg' # 3. Voltage Across Resistor in graph 'vrg' VoltsVsTime = graph(title = 'Voltage vs Time', width = 1000, height = 400, xtitle = 'Time, t [s]', ytitle='Voltage, V [V]', fast = False) vsg = gcurve(color = color.blue, width = 2, label = 'Source Voltage') vlg = gcurve(color = color.red, width = 2, label = 'Inductor Voltage') vrg = gcurve(color = color.green, width = 2, label = 'Resistor Voltage') # Set up a graph of current vs time ShowCurrentGraph = False # Set to True to show the Curent Graph if (ShowCurrentGraph): CurrentVsTime = graph(title = 'Current vs Time', width = 1000, height = 400, xtitle = 'Time, t [s]', ytitle='Current, I [A]', fast = False) ig = gcurve(color = color.blue, width = 2, label = 'Current') # Define the resistance and capacitance values for the circuit. R = 33 # Resistance in ohms L = 8.2e-3 # Inductance in Henries # Compute the time constant tau as the product of R and L. Units of tau are seconds tau = L/R # SET UP THE SOURCE VOLTAGE SIGNAL # # Define the period, frequency, and amplitude of the input signal. # # Define 'p' as the period of the square wave input signal to the circuit. # Initially this is set at 10*tau which means that the signal will be on for # five time constants and off for five time constants. In this scenerio # there is plenty of time for the capacitor to full charge during the 'on' cycle # and fully discharge during the 'off' cycle. # # You may want to change the period so that you will get a frequency that matches # exactly the value used in your experiment, or simply define the frequency you want # first snd then compute p=1/f in the opposite order of what is done here. p = 10*tau # Period of the square wave (seconds) f = 1/p # Frequency of the square wave (Hz) a = 4.0 # Amplitude of the square wave (volts) # SET UP THE TIME STEP SIZE # # For good integration of the differential equations of the circuit you want # a small step size 'dt' in comparison to the time constant 'tau' of the circuit. dt = tau/100 # dt = time step (seconds) # INITIALIZE PARAMETERS i = 0 # Initial current (Amps) t = 0 # Initial time value (seconds) # PRINTING OPTIONS # # The PrintData flag can be set to True or False to control whether the data # values are printed in the output window. Printing causes the program to run # slowly so it is initially turned off. Using a small step size (dt) results in # a very large number of data points, way more than we would want to print. # We'll count which data point we are on with the variable n. The variable N # will determine how often we print values. For example, if N = 10 then would # mean we will print every 10th data point PrintData = True # False = Printing off, True = Printing on. n = 0 # Counter for printing purposes N = 10 # Print every Nth data value if (PrintData): print("Simulated Time, t [s]\tSimulated Source Voltage, Vs [V]\tSimulated Inductor Voltage, VL [V]\tSimulated Resistor Voltage, Vr [V]\t") while(t < 3*p): # Use the square function to compute a new value for the source voltage, vs vs = square(a,f,t) di = vl = vr = # Plot the results vsg.plot(t, vs) # Source Voltage vlg.plot(t, vl) # Inductor Voltage vrg.plot(t, vr) # Resistor Voltage if (ShowCurrentGraph): ig.plot(t, i) # Print the results if (PrintData): if n%N==0: print("{:f}\t{:f}\t{:f}\t{:f}".format(t,vs,vl,vr)) i = # Increment the time by one time step and the line counter t = t + dt n = n + 1