1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <iostream> #include <cmath> #include <iomanip> const double PI = 3.141592653589793; // Function to calculate impedance of RLC circuit double calculateImpedance(double R, double L, double C, double f) { double omega = 2 * PI * f; // Angular frequency double XL = omega * L; // Inductive reactance double XC = 1 / (omega * C); // Capacitive reactance return std::sqrt(R * R + (XL - XC) * (XL - XC)); // Impedance } // Function to calculate voltage across R, L, and C void calculateVoltages(double R, double L, double C, double f, double Vsource, double& VR, double& VL, double& VC) { double omega = 2 * PI * f; // Angular frequency double XL = omega * L; // Inductive reactance double XC = 1 / (omega * C); // Capacitive reactance double Z = calculateImpedance(R, L, C, f); // Impedance // Voltage across R VR = (R / Z) * Vsource; // Voltage across L VL = ((XL) / Z) * Vsource; // Voltage across C VC = ((XC) / Z) * Vsource; } int main() { // Circuit parameters double R, L, C, f, Vsource; // User input std::cout << "Enter the resistance (R) in ohms: "; std::cin >> R; std::cout << "Enter the inductance (L) in henries: "; std::cin >> L; std::cout << "Enter the capacitance (C) in farads: "; std::cin >> C; std::cout << "Enter the frequency (f) in hertz: "; std::cin >> f; std::cout << "Enter the source voltage (V) in volts: "; std::cin >> Vsource; double VR, VL, VC; calculateVoltages(R, L, C, f, Vsource, VR, VL, VC); std::cout << std::fixed << std::setprecision(2); std::cout << "Impedance of the RLC circuit: " << calculateImpedance(R, L, C, f) << " ohms\n"; std::cout << "Voltage across the resistor (VR): " << VR << " volts\n"; std::cout << "Voltage across the inductor (VL): " << VL << " volts\n"; std::cout << "Voltage across the capacitor (VC): " << VC << " volts\n"; return 0; } |
Explanation
- Constants:
- PI: Mathematical constant π.
- Function
calculateImpedance(double R, double L, double C, double f)
:- Purpose: Computes the total impedance of the series RLC circuit.
- Parameters:
R
: Resistance in ohms.L
: Inductance in henries.C
: Capacitance in farads.f
: Frequency in hertz.
- Implementation:
- Angular Frequency:
omega = 2 * PI * f
. - Inductive Reactance:
XL = omega * L
Advertisement - Capacitive Reactance:
XC = 1 / (omega * C)
. - Impedance Calculation:
Z = sqrt(R^2 + (XL - XC)^2)
.
- Angular Frequency:
Advertisement - Function
calculateVoltages(double R, double L, double C, double f, double Vsource, double& VR, double& VL, double& VC)
:- Purpose: Calculates the voltage across each component (resistor, inductor, capacitor) based on the source voltage.
- Parameters:
R
,L
,C
,f
,Vsource
: Same as above, plusVsource
, the source voltage.VR
,VL
,VC
: Output parameters for voltages across the resistor, inductor, and capacitor.
- Implementation:
- Uses
calculateImpedance
to get the total impedance. - Calculates voltage drops across each component using voltage division.
Advertisement - Uses
- Main Function:
- User Input: Prompts for resistance, inductance, capacitance, frequency, and source voltage.
- Voltage Calculation: Calls
calculateVoltages
to compute voltages across each component. - Result Display: Outputs the impedance and voltages with two decimal places of precision.
Usage
- RLC Circuit Simulation: Models the impedance and voltage distribution in a series RLC circuit.
- Electrical Analysis: Demonstrates how to calculate and interpret impedance and voltages in AC circuits.