|
| 1 | +""" |
| 2 | +---------------------------------------------------------------------------------------- |
| 3 | +An electric field (E) is a physical field that surrounds electrically charged particles. |
| 4 | +In classical electromagnetism, the electric field of a single charge describes their |
| 5 | +capacity to exert attractive or repulsive forces on another charged object. |
| 6 | +
|
| 7 | +The electric field is a vector field, which means it has both a magnitude and |
| 8 | +a direction. |
| 9 | +
|
| 10 | +The direction of the electric field is the direction of the force that a positive test |
| 11 | +charge would experience if placed in the field. |
| 12 | +
|
| 13 | +The magnitude of the electric field of an electrically charged point object is given by |
| 14 | +the formula: |
| 15 | +
|
| 16 | +------------------- |
| 17 | +| E = k * Q / r^2 | |
| 18 | +------------------- |
| 19 | +
|
| 20 | +k --> Coulomb's constant and is equal to 1/(4π*ε0) |
| 21 | +Q --> charge of the charged object (C) |
| 22 | +r --> distance between the center of the charged object and the electric field |
| 23 | +measurement point (m) |
| 24 | +
|
| 25 | +Reference: https://en.wikipedia.org/wiki/Electric_field |
| 26 | +""" |
| 27 | + |
| 28 | +def __check_args(charge: float, distance: float) -> None: |
| 29 | + """ |
| 30 | + Check that the arguments are valid |
| 31 | + >>> __check_args(50, -10) |
| 32 | + Traceback (most recent call last): |
| 33 | + ... |
| 34 | + ValueError: The distance is always a positive number. |
| 35 | + >>> __check_args("50", 10) |
| 36 | + Traceback (most recent call last): |
| 37 | + ... |
| 38 | + TypeError: Invalid charge. Should be an integer or float. |
| 39 | + >>> __check_args(50, "10") |
| 40 | + Traceback (most recent call last): |
| 41 | + ... |
| 42 | + TypeError: Invalid distance. Should be an integer or float. |
| 43 | + """ |
| 44 | + |
| 45 | + # Ensure valid instance |
| 46 | + if not isinstance(charge, (int, float)): |
| 47 | + raise TypeError("Invalid charge. Should be an integer or float.") |
| 48 | + |
| 49 | + if not isinstance(distance, (int, float)): |
| 50 | + raise TypeError("Invalid distance. Should be an integer or float.") |
| 51 | + |
| 52 | + # Ensure valid distance |
| 53 | + if distance <= 0: |
| 54 | + raise ValueError("The distance is always a positive number.") |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +def __eletric_field_direction(magnitude_of_the_electric_field: float) -> float: |
| 59 | + """ |
| 60 | + Determine the direction of the electric field based on the magnitude. |
| 61 | + >>> __eletric_field_direction(-15) |
| 62 | + 'Since the charge is negative, the electric field is pointing towards the charge.' |
| 63 | + >>> __eletric_field_direction(20) |
| 64 | + 'Since the charge is positive, the electric field is pointing away from the charge.' |
| 65 | + >>> __eletric_field_direction(0) |
| 66 | + 'The electric field is zero.' |
| 67 | + """ |
| 68 | + if magnitude_of_the_electric_field < 0: |
| 69 | + return "Since the charge is negative, the electric field is pointing towards the charge." |
| 70 | + elif magnitude_of_the_electric_field > 0: |
| 71 | + return "Since the charge is positive, the electric field is pointing away from the charge." |
| 72 | + else: |
| 73 | + return "The electric field is zero." |
| 74 | + |
| 75 | + |
| 76 | +def electric_field(charge: float, distance: float) -> float: |
| 77 | + """ |
| 78 | + Calculate the magnitude of the electric field of an electrically charged |
| 79 | + point object. |
| 80 | +
|
| 81 | + >>> electric_field(20, 15) |
| 82 | + Since the charge is positive, the electric field is pointing away from the charge. |
| 83 | + 798893492.65 |
| 84 | + >>> electric_field(15, 5) |
| 85 | + Since the charge is positive, the electric field is pointing away from the charge. |
| 86 | + 5392531075.38 |
| 87 | + >>> electric_field(-20, 15) |
| 88 | + Since the charge is negative, the electric field is pointing towards the charge. |
| 89 | + -798893492.65 |
| 90 | + >>> electric_field(0, 1) |
| 91 | + The electric field is zero. |
| 92 | + 0.0 |
| 93 | + """ |
| 94 | + __check_args(charge, distance) |
| 95 | + |
| 96 | + coulombs_constant = 8.9875517923e9 # in N·m²/C² |
| 97 | + mag_eletric_field = (coulombs_constant * charge) / (distance**2) |
| 98 | + |
| 99 | + print(__eletric_field_direction(mag_eletric_field)) |
| 100 | + |
| 101 | + return round(mag_eletric_field, 2) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + import doctest |
| 106 | + |
| 107 | + doctest.testmod() |
0 commit comments