LRC network simulation
This script simulates a classical LRC electrical circuit consisting of an inductor, a resistor and a capacitor. An step voltage is applied to the input, and the voltage across the capacitor exhibits oscillatory behavior.from pylab import figure, plot, show, grid, ion, legend,xlabel,ylabel import scipy.integrate import time import numpy def f_LC(t, state, arg1): charge, field = state L, C, R = arg1 I_L = field / L U_C = charge / C # Step function: if (t > 1.0): Vin = 1.0 else: Vin = 0.0 U_R = R * I_L # Ohms law dfield = Vin - U_C - U_R # Calculate voltage across coil rates = [I_L, dfield] return rates def main(): initial_state = [0.0, 0.0] # Initial charge and field L = 0.1 # Henry C = 0.1 # Farad R = 0.1 # Ohm t0 = 0.0 t1 = 10.0 # Seconds dt = 0.01 # Seconds r = scipy.integrate.ode(f_LC) r.set_integrator('zvode', method='bdf', with_jacobian=False) r.set_f_params([L, C, R]) r.set_initial_value(initial_state, t0) t = list() charge = list() while r.successful() and r.t < t1: r.integrate(r.t+dt) t.append(r.t) charge.append(r.y[0]) figure() U_C = numpy.array(charge) / C plot(t, U_C, 'o-',label='voltage across capacitor [V]') legend() grid(True) xlabel('time [s]') ylabel('Electrical potential [V]') show() if __name__ == '__main__': main()