RC circuit simulation with text box

    0

    0

    This code defines a function to calculate voltages across a capacitor and resistor. This code also includes a Slider to allow the user to change the values of the capacitor and resistor. When the user clicks on the reset button, the values of the capacitor and resistor are reset to their default values.

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button, Slider
    
    def calculate_voltages(t, otpor, napon, kapacitivnost):
      vc = napon * (1 - np.exp(-t / (otpor * kapacitivnost)))
      vr = napon * np.exp(-t / (otpor * kapacitivnost))
      return vc, vr
    
    def update_plot(otpor, kapacitivnost, napon):
      t = np.linspace(0, 10, 100)
      vc, vr = calculate_voltages(t, otpor, napon, kapacitivnost)
      vc_plot.set_ydata(vc)
      vr_plot.set_ydata(vr)
      plt.draw()
    
    fig, ax = plt.subplots()
    t = np.linspace(0, 10, 100)
    napon = 1
    otpor = 10
    kapacitivnost = 1
    vc, vr = calculate_voltages(t, otpor, napon, kapacitivnost)
    vc_plot, = ax.plot(t, vc, color='blue', label='Voltage across capacitor')
    vr_plot, = ax.plot(t, vr, color='green', label='Voltage across resistor')
    ax.legend()
    
    axcolor = 'lightgoldenrodyellow'
    otporax = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
    otpor_slider = Slider(otporax, 'Отпорност (оми)', 1, 1000, valinit=otpor)
    
    kapacitivnostax = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
    kapacitivnost_slider = Slider(kapacitivnostax, 'Капацитивност (уФ)', 0.1, 100, valinit=kapacitivnost)
    
    naponax = plt.axes([0.25, 0.2, 0.65, 0.03], facecolor=axcolor)
    napon_slider = Slider(naponax, 'Напон (В)', 0.1, 10, valinit=napon)
    
    def sliders_on_change(val):
      update_plot(otpor_slider.val, kapacitivnost_slider.val, napon_slider.val)
    otpor_slider.on_changed(sliders_on_change)
    kapacitivnost_slider.on_changed(sliders_on_change)
    napon_slider.on_changed(sliders_on_change)
    
    resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
    reset_button = Button(resetax, 'Ресет', color=axcolor, hovercolor='0.975')
    
    def reset_button_on_click(mouse_event):
      otpor_slider.reset()
      kapacitivnost_slider.reset()
      napon_slider.reset()
    reset_button.on_clicked(reset_button_on_click)
    
    plt.show()
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.