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