Калькулятор Закона Ома

Рассчитывайте напряжение, ток, сопротивление и мощность по закону Ома.

Введите любые два значения для расчёта двух других

Формулы

V = I × R

P = V × I

P = I² × R

P = V² / R

I = V / R = √(P / R)

R = V / I = V² / P

Часто задаваемые вопросы

Реализация кода

import math

# Ohm's Law: V = I * R,  P = V * I = I^2 * R = V^2 / R

def solve_ohm(V=None, I=None, R=None, P=None):
    """Solve for any two unknowns given two known values.
    Pass exactly two keyword arguments."""
    known = {k: v for k, v in {"V": V, "I": I, "R": R, "P": P}.items() if v is not None}
    if len(known) != 2:
        raise ValueError("Provide exactly 2 known values")

    # Derive missing values
    if V is None:
        if I and R: V = I * R
        elif P and I: V = P / I
        elif P and R: V = math.sqrt(P * R)
    if I is None:
        if V and R: I = V / R
        elif P and V: I = P / V
        elif P and R: I = math.sqrt(P / R)
    if R is None:
        if V and I: R = V / I
        elif P and I: R = P / I**2
        elif P and V: R = V**2 / P
    if P is None:
        if V and I: P = V * I
        elif I and R: P = I**2 * R
        elif V and R: P = V**2 / R

    return {"V": V, "I": I, "R": R, "P": P}

# Examples
print(solve_ohm(V=12, R=4))      # I=3A, P=36W
print(solve_ohm(P=100, R=25))    # V=50V, I=2A
print(solve_ohm(I=0.5, P=10))    # V=20V, R=40Ω

# Series resistors
r_series = sum([100, 220, 470])  # 790 Ω
print(f"Series: {r_series} Ω")

# Parallel resistors (two)
def parallel(r1, r2): return (r1 * r2) / (r1 + r2)
print(f"Parallel 100Ω||100Ω: {parallel(100,100)} Ω")  # 50 Ω

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.