Reactance Calculator

Use the sliders below to adjust the Capacitance and Inductance and watch how the resonant frequency shifts.

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 600

from shiny import App, render, ui
import numpy as np
import matplotlib.pyplot as plt

# 1. Define the User Interface
app_ui = ui.page_fluid(
    ui.input_slider("C_uF", "Capacitance (uF)", min=10, max=500, value=100, step=10),
    ui.input_slider("L_mH", "Inductance (mH)", min=1, max=50, value=10, step=1),
    ui.output_plot("reactance_plot")
)

# 2. Define the Server Logic
def server(input, output, session):
    @output
    @render.plot(alt="Reactance vs Frequency")
    def reactance_plot():
        C = input.C_uF() * 1e-6
        L = input.L_mH() * 1e-3
        
        f = np.linspace(10, 1000, 500)
        X_C = 1 / (2 * np.pi * f * C)
        X_L = 2 * np.pi * f * L
        
        fig, ax = plt.subplots(figsize=(8, 5))
        ax.plot(f, X_C, label='Capacitive Reactance ($X_C$)', color='#0072B2', linewidth=2.5)
        ax.plot(f, X_L, label='Inductive Reactance ($X_L$)', color='#D55E00', linewidth=2.5)
        
        f_res = 1 / (2 * np.pi * np.sqrt(L * C))
        if f_res <= 1000:
            X_res = 2 * np.pi * f_res * L
            ax.plot(f_res, X_res, 'ko', markersize=8, label=f'Resonance ({f_res:.1f} Hz)')
            
        ax.set_title('Reactance vs Frequency', fontsize=14, fontweight='bold')
        ax.set_xlabel('Frequency (Hz)', fontsize=12)
        ax.set_ylabel('Reactance (Ohms)', fontsize=12)
        ax.set_ylim(0, 150)
        ax.grid(True, linestyle='--', alpha=0.7)
        ax.legend(fontsize=11)
        
        return fig

app = App(app_ui, server)