# LED Electrical Calculations

Given an LED conical spiral of a given length determine various electrical attributes about the setup. This could be multiple strips including partials.

LED Details:

- How many LED strips?
- How many LEDs per strip? Per unit length?
- How many amps per LED?
- How many watts per LED?
- LED strip voltage?

Questions?

- Power requirements?
- Power injection?
- Power wire gauge?
- Draw (amps) from mains?

## References

- https://www.waveformlighting.com/home-residential/how-to-choose-a-power-supply-for-your-led-strip-project
- https://www.rapidtables.com/calc/wire/voltage-drop-calculator.html
- https://www.rapidtables.com/calc/wire/wire-gauge-chart.html
- https://www.reddit.com/r/WLED/comments/q7oxz4/so_glad_to_never_have_to_put_up_holiday_lights/
- https://www.reddit.com/r/WLED/comments/pp9g0e/power_supply_injection_for_ws2812b_700_leds/
- https://www.reddit.com/r/WLED/comments/kplt41/double_check_my_numbers_for_me_please/
- https://www.reddit.com/r/WLED/comments/qmp7fy/power_injection_one_run_with_multiple_injects_or/
- https://www.reddit.com/r/WLED/comments/qx9efb/voltage_drop_of_power_injection_wire/
- https://quinled.info/2020/09/26/power-injection-livestream/

# Formulae

$$
\begin{equation} \tag{1}
V = I \cdot R
\end{equation}
$$

Where:

- $V$ - Voltage (Volts)
- $I$ - Amperage (Amps)
- $R$ - Resistance (Ohms)

## Power

$$
\begin{equation} \tag{2}
P = V \cdot I
\end{equation}
$$

$$
\begin{equation} \tag{3}
P = I^2 \cdot R
\end{equation}
$$

$$
\begin{equation} \tag{4}
P = V^2 \cdot R
\end{equation}
$$

Where:

- $P$ - Power (Watts)
- $V$ - Voltage (Volts)
- $I$ - Amperage (Amps)
- $R$ - Resistance (Ohms)

# Example 1

Using the strip from [here](https://www.amazon.ca/APA102-Pixels-Seperately-SK9822-144LED/dp/B09CDHMS1M) as a reference.

Strip details:
- APA102
- 5m
- 60 LED/m
- 5V DC
- 18 Watt/m

```python
import numpy as np
from scipy import optimize
```

```python
length = 5                # m
LED_count = 60            # LED/m
watt_per_unit_length = 18 # watts per m
strip_voltage = 5 # volts

LED_total_count = LED_count*length

total_power = watt_per_unit_length*length

total_current = total_power/strip_voltage

mains_voltage = 120 # V
total_current_mains = total_power/mains_voltage

print('Strip Details:')
print(f'LED Strip Length = {length} m')
print(f'Voltage          = {strip_voltage} V')
print(f'LED Count        = {LED_count} per m')
print(f'LED Count (tot)  = {LED_total_count}')
print()

print('Strip Power Requirements:')
print(f'Watts per m      = {watt_per_unit_length} W/m')
print(f'Total Watts      = {total_power} W')
print(f'Watts per LED    = {total_power/LED_total_count} W')
print()

print(f'Strip Amps @ {strip_voltage} V:')
print(f'Max Amps         = {total_current} A')
print(f'Max Amps per LED = {total_current/LED_total_count} A ({1000*total_current/LED_total_count} mA)')
print()

print(f'Mains Power Requirements {total_power} W @ {mains_voltage} V:')
print(f'Max Amps         = {total_current_mains} A')
```

    Strip Details:
    LED Strip Length = 5 m
    Voltage          = 5 V
    LED Count        = 60 per m
    LED Count (tot)  = 300

    Strip Power Requirements:
    Watts per m      = 18 W/m
    Total Watts      = 90 W
    Watts per LED    = 0.3 W

    Strip Amps @ 5 V:
    Max Amps         = 18.0 A
    Max Amps per LED = 0.06 A (60.0 mA)

    Mains Power Requirements 90 W @ 120 V:
    Max Amps         = 0.75 A

The power requirements for this strip is 90 Watts. This means we have to provide a power supply that can produce 90 Watts at 5 V. You would probably want to double that wattage and look for something in the 150 W to 180 W range. Essentially, the power supply should be able to produce 30 A to 40 A at 5 V. That would be 1.25 A to 1.5 A at 120 V.

# Voltage drop - DC Power line

## Resistivity

- <https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity#Resistivity_and_conductivity_of_various_materials>

>Electrical resistivity (also called specific electrical resistance or volume resistivity) is a fundamental property of a material that measures how strongly it resists electric current. A low resistivity indicates a material that readily allows electric current. Resistivity is commonly represented by the Greek letter ρ (rho). The SI unit of electrical resistivity is the ohm-meter (Ω⋅m).For example, if a 1 m3 solid cube of material has sheet contacts on two opposite faces, and the resistance between these contacts is 1 Ω, then the resistivity of the material is 1 Ω⋅m.

Resistivity, $\rho$, is in units of ohm meters, $\left( \Omega \cdot m \right)$. The SI base units are:

$$\text{kg} \cdot m^3 \cdot s^{-3} \cdot A^{-2}$$

or

$$\frac{\text{kg} \cdot m^3}{s^3 \cdot A^2} $$

From [Wikipedia](https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity#Resistivity_and_conductivity_of_various_materials), this table shows the resistivity (ρ), conductivity and temperature coefficient of various materials at 20 °C (68 °F; 293 K).

| Material                                        | Resistivity, ρ,  at 20 °C (Ω·m)   | Conductivity, σ,  at 20 °C (S/m)  | Temperature coefficient[c] (K−1)  |
|-------------------------------------------------|-----------------------------------|-----------------------------------|-----------------------------------|
| Silver[d]                                       | 1.59×10−8                         | 6.30×107                          | 3.80×10−3                         |
| Copper[e]                                       | 1.68×10−8                         | 5.96×107                          | 4.04×10−3                         |
| Annealed copper[f]                              | 1.72×10−8                         | 5.80×107                          | 3.93×10−3                         |
| Gold[g]                                         | 2.44×10−8                         | 4.11×107                          | 3.40×10−3                         |
| Aluminium[h]                                    | 2.65×10−8                         | 3.77×107                          | 3.90×10−3                         |
| Calcium                                         | 3.36×10−8                         | 2.98×107                          | 4.10×10−3                         |
| Tungsten                                        | 5.60×10−8                         | 1.79×107                          | 4.50×10−3                         |
| Zinc                                            | 5.90×10−8                         | 1.69×107                          | 3.70×10−3                         |
| Cobalt[i]                                       | 6.24×10−8                         | 1.60×107                          | 7.00×10−3[30][unreliable source?] |
| Nickel                                          | 6.99×10−8                         | 1.43×107                          | 6.00×10−3                         |
| Ruthenium[i]                                    | 7.10×10−8                         | 1.41×107                          |                                   |
| Lithium                                         | 9.28×10−8                         | 1.08×107                          | 6.00×10−3                         |
| Iron                                            | 9.70×10−8                         | 1.03×107                          | 5.00×10−3                         |
| Platinum                                        | 10.6×10−8                         | 9.43×106                          | 3.92×10−3                         |
| Tin                                             | 10.9×10−8                         | 9.17×106                          | 4.50×10−3                         |
| Gallium                                         | 14.0×10−8                         | 7.10×106                          | 4.00×10−3                         |
| Niobium                                         | 14.0×10−8                         | 7.00×106                          |                                   |
| Carbon steel (1010)                             | 14.3×10−8                         | 6.99×106                          |                                   |
| Lead                                            | 22.0×10−8                         | 4.55×106                          | 3.90×10−3                         |
| Galinstan                                       | 28.9×10−8                         | 3.46×106                          |                                   |
| Titanium                                        | 42.0×10−8                         | 2.38×106                          | 3.80×10−3                         |
| Grain oriented electrical steel                 | 46.0×10−8                         | 2.17×106                          |                                   |
| Manganin                                        | 48.2×10−8                         | 2.07×106                          | 0.002×10−3                        |
| Constantan                                      | 49.0×10−8                         | 2.04×106                          | 0.008×10−3                        |
| Stainless steel[j]                              | 69.0×10−8                         | 1.45×106                          | 0.94×10−3                         |
| Mercury                                         | 98.0×10−8                         | 1.02×106                          | 0.90×10−3                         |
| Manganese                                       | 144×10−8                          | 6.94×105                          |                                   |
| Nichrome[k]                                     | 110×10−8                          | 6.70×105 [citation needed]        | 0.40×10−3                         |
| Carbon (graphite) parallel to basal plane[l]    | 250×10−8 to 500×10−8              | 2×105 to 3×105 [citation needed]  |                                   |
| Carbon (amorphous)                              | 0.5×10−3 to 0.8×10−3              | 1.25×103 to 2.00×103              | −0.50×10−3                        |
| Carbon (graphite) perpendicular to basal plane  | 3.0×10−3                          | 3.3×102                           |                                   |
| GaAs                                            | 10−3 to 108[clarification needed] | 10−8 to 103[dubious – discuss]    |                                   |
| Germanium[m]                                    | 4.6×10−1                          | 2.17                              | −48.0×10−3                        |
| Sea water[n]                                    | 2.1×10−1                          | 4.8                               |                                   |
| Swimming pool water[o]                          | 3.3×10−1 to 4.0×10−1              | 0.25 to 0.30                      |                                   |
| Drinking water[p]                               | 2×101 to 2×103                    | 5×10−4 to 5×10−2                  |                                   |
| Silicon[m]                                      | 2.3×103                           | 4.35×10−4                         | −75.0×10−3                        |
| Wood (damp)                                     | 103 to 104                        | 10−4 to 10−3                      |                                   |
| Deionized water[q]                              | 1.8×105                           | 4.2×10−5                          |                                   |
| Glass                                           | 1011 to 1015                      | 10−15 to 10−11                    |                                   |
| Carbon (diamond)                                | 1012                              | ~10−13                            |                                   |
| Hard rubber                                     | 1013                              | 10−14                             |                                   |
| Air                                             | 109 to 1015                       | ~10−15 to 10−9                    |                                   |
| Wood (oven dry)                                 | 1014 to 1016                      | 10−16 to 10−14                    |                                   |
| Sulfur                                          | 1015                              | 10−16                             |                                   |
| Fused quartz                                    | 7.5×1017                          | 1.3×10−18                         |                                   |
| PET                                             | 1021                              | 10−21                             |                                   |
| Teflon                                          | 1023 to 1025                      | 10−25 to 10−23                    |                                   |

>The effective temperature coefficient varies with temperature and purity level of the material. The 20 °C value is only an approximation when used at other temperatures. For example, the coefficient becomes lower at higher temperatures for copper, and the value 0.00427 is commonly specified at 0 °C.

[Reference](https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity#Resistivity_and_conductivity_of_various_materials)

```python
# Resistivity of selected elements (Ω·m)
# Source https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity

# All values are in Ω·m:
resistivity = {
    'Copper':              1.72E-8,
    'Aluminum':            2.65E-8,
    'Carbon Steel':       14.3E-8,
    'Electrical Steel':   46.0E-8,
    'Gold':                2.44E-8,
    'Nichrome':          110.0E-8,
    'Nickel':              6.99E-8,
    'Silver':              1.59E-8,
    'Copper (elemental)':  1.68E-8,
}
```

## Wire Resistance

To effectively perform the wire voltage drop calculations, we need to understand the resistance of the wire based on its material property and its cross-sectional area $\left(\text{mm}^2\right)$. We'll use resistivity (above) and we'll use the [American Wire Gauge](https://en.wikipedia.org/wiki/American_wire_gauge) standard for cross-sectional area.

[Wikipedia](https://en.wikipedia.org/wiki/American_wire_gauge):

>American Wire Gauge (AWG), also known as the Brown & Sharpe wire gauge, is a logarithmic stepped standardized wire gauge system used since 1857, predominantly in North America, for the diameters of round, solid, nonferrous, electrically conducting wire. Dimensions of the wires are given in ASTM standard B 258. The cross-sectional area of each gauge is an important factor for determining its current-carrying ampacity.
>
>Increasing gauge numbers denote decreasing wire diameters, which is similar to many other non-metric gauging systems such as British Standard Wire Gauge (SWG), but unlike IEC 60228, the metric wire-size standard used in most parts of the world. This gauge system originated in the number of drawing operations used to produce a given gauge of wire. Very fine wire (for example, 30 gauge) required more passes through the drawing dies than 0 gauge wire did. Manufacturers of wire formerly had proprietary wire gauge systems; the development of standardized wire gauges rationalized selection of wire for a particular purpose.
>
>The AWG tables are for a single, solid and round conductor. The AWG of a stranded wire is determined by the cross-sectional area of the equivalent solid conductor. Because there are also small gaps between the strands, a stranded wire will always have a slightly larger overall diameter than a solid wire with the same AWG.

AWG goes from n = 0 to 40, including `0000`, `000`, `00`. Smaller values of `n` are larger in diameter.

### AWG Diameter

$$
\begin{equation} \tag{5}
d_n = 0.127 \cdot 92^{\frac{36 - n}{39}}
\end{equation}
$$

Where:
- $d_n$ - AWG wire diameter in milimeters $\left( \text{mm} \right)$
- $n$ - AWG index number from -3 to 40.
    - n = -1 for `00`, n=-2 for `000`, n = -3 for `0000`
    - For example, for n = 0, the gauge used `AWG 0`, as-is; for n = − 1, the gauge is either `00` or `2/0`; for n = − 2, either `000` or `3/0`; n = − 3, either `0000` or `4/0`

```python
def awg_diameter(n:int) -> float:
    """
    Given an AWG gauge number, n, return the diameter in millimeters (mm).
    """

    return 0.127*92**((36 - n)/39)
```

```python
n = 5
d = awg_diameter(n)
print(f'AWG {n} = {d:.3f} mm')
```

    AWG 5 = 4.621 mm

### AWG Area (Cross-Section)

$$
\begin{equation} \tag{6}
A_n = \frac{\pi}{4} \cdot d_n^2
\end{equation}
$$

```python
def awg_area(n:int) -> float:
    """
    Given an AWG gauge number, n, return the cross-sectional
    area in mm^2.
    """

    d = awg_diameter(n)

    return (np.pi/4)*d**2
```

```python
n = 5
a = awg_area(n)
print(f'AWG {n} = {a:.3f} mm^2')
```

    AWG 5 = 16.773 mm^2

## Resistance

We use the resistivity of the material and divide it by the cross-sectional area:

$$
\begin{equation} \tag{7}
R_n = \frac{\rho}{A_n}
\end{equation}
$$

Where:

- $R_n$ - Wire resistance per unit length $\left( \frac{\Omega}{m} \right)$

```python
def wire_resistance(n:int, resistivity:float) -> float:
    """
    Given the AWG gauge index and the conductor resistivity,
    determine the resistance per unit length (Ω/m).

    # args

    n - The index number representing the AWG wire gauge.

    resistivity - The resistivity of the conductor in Ω/m.

    """

    # determine the cross-sectional area (square millimeters (mm^2)) of the
    # AWG wire gauge, n

    a = awg_area(n)*1E-6 # convert the area to m^2

    return resistivity/a
```

```python
q = wire_resistance(5, resistivity["Copper"])
print(f'Resistance per unit length = {q:.3e} (Ω/m)')
print(f'Resistance per unit length = {q*1E3:.3f} (mΩ/m)')
```

    Resistance per unit length = 1.025e-03 (Ω/m)
    Resistance per unit length = 1.025 (mΩ/m)

## Voltage Drop Calculations

- <https://www.rapidtables.com/calc/wire/voltage-drop-calculator.html#calculations>

### Voltage Drop - DC

The voltage drop is, $ V = I \cdot R$, where:

- $V$ - Voltage drop
- $I$ - Current in amps
- $R$ - Resistance in Ohms $\left ( \Omega \right)$

The resistance of the power wires is calculated by the length of the wire and its resistivity.

 NOTE For power lines, we normally double the length, one length for the hot wire, the other for the ground wire. The following calculation will make that assumption.

```python
n = 5
l = 10 # one-way length in meters
target_v = 5 # volts
current = 1 # amp

r_wire = wire_resistance(n, resistivity["Copper"])

v_drop = current*(2*l*r_wire)

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/target_v:.7%}')

print(f'Wire Resistance one-way = {r_wire:.7f}Ω')
print(f'Wire Resistance two-way = {2*r_wire:.7f}Ω')

```

    Voltage Drop            = 0.0205089V
    % Voltage Drop          = 0.4101777%
    Wire Resistance one-way = 0.0010254Ω
    Wire Resistance two-way = 0.0020509Ω

For a length of power line that is 10 meters (20 meters of total length hot + ground), At 5 volts and 1 amp, the voltage drop is 0.021 volts. That is a 0.41% drop in voltage across that total length. This result matches the on-line calculator [here](https://www.rapidtables.com/calc/wire/voltage-drop-calculator.html#calculations).

```python
def voltage_drop(n, current, wire_length, resistivity):
    """

    # args

    n - The index number representing the AWG wire gauge.

    current - The current (Amps) we will run through the wire

    wire_length - Total length of wire run involved. For power lines, it will be
                  typically twice the distance because we have to run two lines for
                  ground and hot (m)

    resistivity - The resistivity of the conductor in Ω/m.

    # Return

    The voltage drop (Volts) across the wire

    """

    return current*wire_length*wire_resistance(n, resistivity)
```

```python
n = 5
l = 10 # one-way length in meters
target_v = 5 # volts
current = 1 # amp

v_drop = voltage_drop(n, current, 2*l, resistivity["Copper"])

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/target_v:.7%}')

print(f'Wire Resistance one-way = {r_wire:.7f}Ω')
print(f'Wire Resistance two-way = {2*r_wire:.7f}Ω')
```

    Voltage Drop            = 0.0205089V
    % Voltage Drop          = 0.4101777%
    Wire Resistance one-way = 0.0010254Ω
    Wire Resistance two-way = 0.0020509Ω

# Voltage Drop - Optimize

Given the target voltage, find an AWG diameter that produces a voltage drip that is less than or equal to a pre-defined percent voltage drop. We need to create an objective function.

```python
def objective_function(
    x,
    voltage:float,
    current:float,
    length:float,
    resistivity:float,
    voltage_drop_ratio:float,
):
    """
    This method defines the objective function, the function we want to minimize.
    This particular instance will search for r, h and d that generates a particular arc length, L.

    # args

    x - 1-D array with shape (n,) of variables to solve for
        - x[0] - n - AWG wire gauge index
        - x[1] -
        - x[2] -

    voltage - The voltage (Volts) we want to maintain i.e. 5 volts

    current - The current (Amps) we will run through the wire

    length - Total length of wire run involved. For power lines, it will be
             typically twice the distance because we have to run two lines for
             ground and hot

    resistivity - The resistivity (Ω·m) of the conductor material in the wire

    voltage_drop_ratio - Ratio of voltage_drop/target_voltage that is acceptable
                         for the length, 0.1 (10%) for example

    # NOTE

    From the Scipy.Optimization docs:

    The objective function to be minimized.

    fun(x, *args) -> float

    where x is an 1-D array with shape (n,) and args is a tuple of the
    fixed parameters needed to completely specify the function.

    """

    v_drop = voltage_drop(x, current, length, resistivity)

    return np.abs(voltage_drop_ratio - v_drop/voltage)
```

```python
# One-way length = 10 m, we'll use 20m
# Our target voltage is 5 V
# Current = 1 amp
# We'll use copper wires
# percentage drop is 10% (0.1)

l = 20
target_v = 5
current = 1
drop_ratio = 0.1

# This yields the same answer as the minimize routine
result = optimize.minimize_scalar(
    objective_function,
    (0, ),
    args=(target_v, current, l, resistivity['Copper'], drop_ratio),
    bounds=(-3, 40),
    method='bounded',
)

# print(result)
# print('-------')

print('Optimal Values:')
print(f'n = {result.x:.4f}')
# print(f'h = {result.x[1]:.4f}')
# print(f'd = {result.x[2]:.4f}')
print('---------')

n = int(result.x)

v_drop = voltage_drop(n, current, l, resistivity["Copper"])

print(f'n = {n}')
print(f'Voltage = {target_v}')
print(f'Current = {current}')
print(f'Length  = {l}')

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/target_v:.7%}')

print(f'Wire Resistance one-way = {r_wire:.7f}Ω')
print(f'Wire Resistance two-way = {2*r_wire:.7f}Ω')
```

    Optimal Values:
    n = 18.7729
    ---------
    n = 18
    Voltage = 5
    Current = 1
    Length  = 20
    Voltage Drop            = 0.4179592V
    % Voltage Drop          = 8.3591841%
    Wire Resistance one-way = 0.0010254Ω
    Wire Resistance two-way = 0.0020509Ω

>NOTE: The optimize method returns a float. We can cast that to an integer and get good results. I don't think we need to use scipy for this, a simple brute force should suffice.

```python
# Brute force search

l = 20
target_v = 5
current = 1
drop_ratio = 0.1

best_n = None

for n in range(-3, 40):
    v_drop = voltage_drop(n, current, l, resistivity["Copper"])

    if v_drop/target_v <= drop_ratio:
#         print(f'n={n:>3}; drop={v_drop:>8.4f}V; % drop = {v_drop/target_v:>10.4%}')
        best_n = n
    else:
        break

print('----')
print(f'n = {best_n}')
print(f'Voltage = {target_v}')
print(f'Current = {current}')
print(f'Length  = {l}')

v_drop = voltage_drop(best_n, current, l, resistivity["Copper"])

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/target_v:.7%}')

print(f'Wire Resistance one-way = {r_wire:.7f}Ω')
print(f'Wire Resistance two-way = {2*r_wire:.7f}Ω')
```

    ----
    n = 18
    Voltage = 5
    Current = 1
    Length  = 20
    Voltage Drop            = 0.4179592V
    % Voltage Drop          = 8.3591841%
    Wire Resistance one-way = 0.0010254Ω
    Wire Resistance two-way = 0.0020509Ω

```python
def search_awg_voltage_drop(
    voltage:float,
    current:float,
    length:float,
    resistivity:float,
    voltage_drop_ratio:float,
):
    """
    Search all AWG wire gauges for the gauge that provides the closest match to the
    voltage drop ratio.

    # args

    voltage - The voltage (Volts) we want to maintain i.e. 5 volts

    current - The current (Amps) we will run through the wire

    length - Total length of wire run involved. For power lines, it will be
             typically twice the distance because we have to run two lines for
             ground and hot

    resistivity - The resistivity (Ω·m) of the conductor material in the wire

    voltage_drop_ratio - Ratio of voltage_drop/target_voltage that is acceptable
                         for the length, 0.1 (10%) for example

    # Return

    The AWG wire gauge index, n,that is just below the voltage drop ratio specified.

    """

    best_n = None

    for n in range(-3, 40):
        drop = voltage_drop(n, current, length, resistivity)

        if drop/voltage <= voltage_drop_ratio:
            best_n = n
        else:
            break

    return best_n

```

It is probably more pragmatic, to simple search the space till we find the proper value.

First Data set:

```python
length = 20
voltage = 5
current = 1
drop_ratio = 0.1

best_n = search_awg_voltage_drop(
    voltage,
    current,
    length,
    resistivity["Copper"],
    drop_ratio,
)

print(f'n = {best_n}')
print(f'Voltage = {target_v}')
print(f'Current = {current}')
print(f'Length  = {l}')

v_drop = voltage_drop(best_n, current, length, resistivity["Copper"])

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/voltage:.7%}')
```

    n = 18
    Voltage = 5
    Current = 1
    Length  = 20
    Voltage Drop            = 0.4179592V
    % Voltage Drop          = 8.3591841%

We want to find a suitable wire gauge for 5 volts and 1 amp of current for a 20 meter total run. It looks like AWG 18 is most suitable. It has a voltage drop of about 8% over that length.

Second Data set:

```python
length = 20 # x2 for the ground and power wires
voltage = 12
current = 1
drop_ratio = 0.1

best_n = search_awg_voltage_drop(
    voltage,
    current,
    length,
    resistivity["Copper"],
    drop_ratio,
)

print(f'n = {best_n}')
print(f'Voltage = {target_v}')
print(f'Current = {current}')
print(f'Length  = {length}')

v_drop = voltage_drop(best_n, current, length, resistivity["Copper"])

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/voltage:.7%}')
```

    n = 22
    Voltage = 5
    Current = 1
    Length  = 20
    Voltage Drop            = 1.0567254V
    % Voltage Drop          = 8.8060454%

If we use the same set of data, expect use 12 volts instead of 5 volts the AWG is 22, a smaller diameter than with 5 volts.

If we look at the same AWG as in the 5 volt case, we get:

```python
v_drop = voltage_drop(18, current, length, resistivity["Copper"])

print(f'Voltage Drop            = {v_drop:.7f}V')
print(f'% Voltage Drop          = {v_drop/voltage:.7%}')
```

    Voltage Drop            = 0.4179592V
    % Voltage Drop          = 3.4829934%

The voltage drop for 12 volts using the same AWG as the 5 volt case is less then 4%. The voltage drop is less than half when using 12 volts versus 5 volts.


# For loop

- To iterate over the List.

- for **int** in range():

- range(end no)
- range(start no.,end no.)
- range(start no.,end no.,step)

```python
d=[1,2,3,4,5]
```

```python
d.append(108)
d
```

    [1, 2, 3, 4, 5, 108]

```python
L = []
for i in range (10):
    L.append(i)
L
```

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

```python
L = []
for i in range(20):
    L.append(i)
L
```

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

```python
a = []
for k in range(2,10):
    a.append(k)
a
```

    [2, 3, 4, 5, 6, 7, 8, 9]

```python
b = []
for i in range(2,21,2):
    b.append(i)
b
```

    [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

```python
sum(b)
```

    110

```python
a+b
```

    [2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

```python
sum(a+b)
```

    154

```python
D = []
for i in range(1,5):
    for j in range(1,5):
        D.append(i+2*j)
        print(D)
```

    [3]
    [3, 5]
    [3, 5, 7]
    [3, 5, 7, 9]
    [3, 5, 7, 9, 4]
    [3, 5, 7, 9, 4, 6]
    [3, 5, 7, 9, 4, 6, 8]
    [3, 5, 7, 9, 4, 6, 8, 10]
    [3, 5, 7, 9, 4, 6, 8, 10, 5]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10, 12]

```python
D = []
for i in range(1,5):
    print(D)
    for j in range(1,5):
        D.append(i+2*j)
```

    []
    [3, 5, 7, 9]
    [3, 5, 7, 9, 4, 6, 8, 10]
    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11]

```python
D = []
for i in range(1,5):
    for j in range(1,5):
        D.append(i+2*j)
D
```

    [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10, 12]

```python
p = [] # empty list l
for i in range(1,5):
    for j in range(1,5):
        p.append(i+2*j)
        print('í=',i,'j=',j,'p=',p)
```

    í= 1 j= 1 p= [3]
    í= 1 j= 2 p= [3, 5]
    í= 1 j= 3 p= [3, 5, 7]
    í= 1 j= 4 p= [3, 5, 7, 9]
    í= 2 j= 1 p= [3, 5, 7, 9, 4]
    í= 2 j= 2 p= [3, 5, 7, 9, 4, 6]
    í= 2 j= 3 p= [3, 5, 7, 9, 4, 6, 8]
    í= 2 j= 4 p= [3, 5, 7, 9, 4, 6, 8, 10]
    í= 3 j= 1 p= [3, 5, 7, 9, 4, 6, 8, 10, 5]
    í= 3 j= 2 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7]
    í= 3 j= 3 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9]
    í= 3 j= 4 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11]
    í= 4 j= 1 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6]
    í= 4 j= 2 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8]
    í= 4 j= 3 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10]
    í= 4 j= 4 p= [3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10, 12]

```python
A = [10*k for k in range(10)]
print(A)
```

    [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

```python
AA = [[10*x+y for x in range(4)] for y in range(3)]
print(AA)
```

    [[0, 10, 20, 30], [1, 11, 21, 31], [2, 12, 22, 32]]

```python
summ = 0
for i in range(1,11):
    summ = summ+i
print(summ)
```

    55

```python
product = 1
for i in range(1,11):
    product = product*i
    print(product)
```

    1
    2
    6
    24
    120
    720
    5040
    40320
    362880
    3628800

### Projectile
- Range
\begin{equation}
R=\frac{u^2\sin2\theta}{2g}
\end{equation}
- Max.Height
\begin{equation}
H=\frac{u^2\sin^2\theta}{2g}
\end{equation}

- using list

```python
import numpy as np
u=100
g=9.8
angle=[]
Range=[]
PI=np.pi
for i in range(0,90):
    angle.append(i)
    Range.append(u**2*np.sin(2*PI*i/180)/(2*g))
```

- import matplotlib.pyplot as plt
- %matplotlib inline
- plt.plot(x,y)

```python
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(angle,Range)
plt.xlabel('Angle')
plt.ylabel('Range')
plt.title('Projectile')
```

- using dictionary

```python
angle = np.arange(1,90)
M = {"Range": [u**2*np.sin(2*PI*x/180)/(2*g) for x in angle],\
     "Max.height": [u**2*np.sin(PI*x/180)**2/(2*g) for x in angle]}
```

```python
import pandas as pd
DF = pd.DataFrame(M)
DF
```

      Range
      Max.height

      0
      17.805866
      0.155401

      1
      35.590038
      0.621416

      2
      53.330849
      1.397476

      3
      71.006684
      2.482636

      4
      88.596009
      3.875573

      ...
      ...
      ...

      84
      88.596009
      506.328508

      85
      71.006684
      507.721446

      86
      53.330849
      508.806606

      87
      35.590038
      509.582666

      88
      17.805866
      510.048680

89 rows × 2 columns

```python
DF.plot()
plt.xlabel('Angle(degree)')
plt.ylabel('Distance(m)')
plt.title('Projectile')
```

```python

```