|
| 1 | +""" |
| 2 | +Magnetic flux (Φ) is a scalar quantity that measures the number of magnetic field lines (B) |
| 3 | +that pass through a closed area (A). Furthermore, the magnetic flux depends on the angle |
| 4 | +formed between the magnetic field and the normal line (N) in area A. Check out the formula |
| 5 | +used to calculate this flux: |
| 6 | + ------------ |
| 7 | + | Φ = B.A.cos(θ) | |
| 8 | + ------------ |
| 9 | +
|
| 10 | +Φ = magnetic flux (weber (Wb) or tesla square meter (T.m²)) |
| 11 | +B = magnetic field (tesla (T)) |
| 12 | +A = area (square meter (m²)) |
| 13 | +θ = angle between magnetic field and normal line (degrees (°)) |
| 14 | +
|
| 15 | +(Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law ) |
| 16 | +""" |
| 17 | + |
| 18 | +from math import cos, radians as deg_to_rad |
| 19 | + |
| 20 | + |
| 21 | +def check_args( |
| 22 | + magnetic_field: float, |
| 23 | + area: float, |
| 24 | + angle: float |
| 25 | +) -> None: |
| 26 | + """ |
| 27 | + Check that the arguments are valid |
| 28 | + """ |
| 29 | + |
| 30 | + # Ensure valid instance |
| 31 | + if not isinstance(magnetic_field, (int, float)): |
| 32 | + raise TypeError("Invalid magnetic field. Should be an integer or float.") |
| 33 | + |
| 34 | + if not isinstance(area, (int, float)): |
| 35 | + raise TypeError("Invalid area. Should be an integer or float.") |
| 36 | + |
| 37 | + if not isinstance(angle, (int, float)): |
| 38 | + raise TypeError("Invalid angle. Should be an integer or float.") |
| 39 | + |
| 40 | + # Ensure valid angle |
| 41 | + if angle < 0 or angle > 180: |
| 42 | + raise ValueError("Invalid angle. Range is 0-180 degrees.") |
| 43 | + |
| 44 | + # Ensure valid magnetic field |
| 45 | + if magnetic_field < 0: |
| 46 | + raise ValueError("Invalid magnetic field. Should be a positive number.") |
| 47 | + |
| 48 | + # Ensure valid area |
| 49 | + if area < 0: |
| 50 | + raise ValueError("Invalid area. Should be a positive number.") |
| 51 | + |
| 52 | + |
| 53 | +def magnetic_flux( |
| 54 | + magnetic_field: float, |
| 55 | + area: float, |
| 56 | + angle: float |
| 57 | +) -> float: |
| 58 | + """ |
| 59 | + >>> magnetic_flux(50.0, 2, 0.0) |
| 60 | + 250.0 |
| 61 | + >>> magnetic_flux(1.5, 3.0, 60.0) |
| 62 | + 2.25 |
| 63 | + >>> magnetic_flux(1.5, 3.0, -60.0) |
| 64 | + 2.25 |
| 65 | + >>> magnetic_flux(0.5, 4.0, 90.0) |
| 66 | + 0.0 |
| 67 | + >>> magnetic_flux(1, 2.0, 180.0) |
| 68 | + -2.0 |
| 69 | + >>> magnetic_flux(-1.0, 2.0, 30.0) |
| 70 | + Traceback (most recent call last): |
| 71 | + ... ValueError: Invalid magnetic field. Should be a positive number. |
| 72 | + >>> magnetic_flux(1.0, 'a', 30.0) |
| 73 | + Traceback (most recent call last): |
| 74 | + ... TypeError: Invalid area. Should be an integer or float. |
| 75 | + >>> magnetic_flux(1.0, -2.0, 30.0) |
| 76 | + Traceback (most recent call last): |
| 77 | + ... ValueError: Invalid area. Should be a positive number. |
| 78 | + """ |
| 79 | + check_args(magnetic_field, area, angle) |
| 80 | + radians = deg_to_rad(angle) |
| 81 | + return magnetic_field * area * cos(radians) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + from doctest import testmod |
| 86 | + |
| 87 | + testmod() |
0 commit comments