initializes the circuit variables.
def plot_rc_circuit(R, C, t_max, dt): t = [] vc = [] ic = []
t.append(0) vc.append(1) ic.append(-1)
for i in range(int(t_max / dt)): t.append(t[i] + dt) vc.append(vc[i] - dt / (R * C) * vc[i]) ic.append(-vc[i] / R)
plt.plot(t, vc, 'r', t, ic, 'b') plt.show()
def plot_rl_circuit(R, L, t_max, dt): t = [] vl = [] il = []
t.append(0) vl.append(1) il.append(-1)
for i in range(int(t_max / dt)):
import matplotlib.pyplot as plt
import tkinter as tk
def plot_rc_circuit(R, C, t_max, dt):
t = []
vc = []
ic = []
t.append(0)
vc.append(1)
ic.append(-1)
for i in range(int(t_max / dt)):
t.append(t[i] + dt)
vc.append(vc[i] - dt / (R * C) * vc[i])
ic.append(-vc[i] / R)
plt.plot(t, vc, 'r', t, ic, 'b')
plt.show()
def plot_rl_circuit(R, L, t_max, dt):
t = []
vl = []
il = []
t.append(0)
vl.append(1)
il.append(-1)
for i in range(int(t_max / dt)):
t.append(t[i] + dt)
vl.append(vl[i] - R / L * dt * il[i])
il.append((vl[i] - vl[i - 1]) / L * dt)
plt.plot(t, vl, 'r', t, il, 'b')
plt.show()
def set_variables():
root = tk.Tk()
root.title("Circuit Variables")
tk.Label(root, text="R (ohm)").grid(row=0, column=0)
tk.Label(root, text="C (farad)").grid(row=1, column=0)
tk.Label(root, text="L (henry)").grid(row=2, column=0)
tk.Label(root, text="t_max (second)").grid(row=3, column=0)
tk.Label(root, text="dt (second)").grid(row=4, column=0)
e1 = tk.Entry(root)
e2 = tk.Entry(root)
e3 = tk.Entry(root)
e4 = tk.Entry(root)
e5 = tk.Entry(root)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
e5.grid(row=4, column=1)
tk.Button(root, text='Plot RC Circuit',
command=lambda: plot_rc_circuit(float(e1.get()), float(e2.get()), float(e4.get()), float(e5.get()))).grid(
row=5, column=0, pady=4)
tk.Button (root, text='Plot RL Circuit', command=lambda: plot_rl_circuit(float(e1.get()), float(e3.get()), float(e4.get()), float(e5.get()))).grid(row=5, column=1, pady=4)
tk.mainloop()
set_variables()