Advertisement
DeaD_EyE

voltage_divider 2

Jan 14th, 2021
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. def solve(Vin=None, Vout=None, R1=None, R2=None, RL=None):
  2.     """
  3.    Calculation of a voltage divider with load.
  4.  
  5.    >>> solve(24, 10, 500, RL=390)
  6.    4239.130434782609
  7.  
  8.    Reference: http://www.sengpielaudio.com/Rechner-spannungsteiler.htm
  9.    """
  10.     missing = [key for key, value in locals().items() if value is None]
  11.     if len(missing) != 1:
  12.         raise TypeError("One Value must be None")
  13.     missing = missing[0]
  14.     if missing == "Vin":
  15.         return Vout * (R1 * R2 + R1 * RL + R2 * RL) / (R2 * RL)
  16.     elif missing == "Vout":
  17.         return Vin * R2 * RL / (R1 * R2 + R1 * RL + R2 * RL)
  18.     elif missing == "R1":
  19.         return ((Vin - Vout) / Vout) * R2 * RL / (R2 + RL)
  20.     elif missing == "R2":
  21.         return Vout * R1 * RL / (RL * Vin - Vout * R1 - Vout * RL)
  22.     elif missing == "RL":
  23.         return Vout * R1 * R2 / (R2 * Vin - Vout * R1 - Vout * R2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement