Ohm's Law with Python

Introduction

As I studied Python, I realized that it would be easy to calculate complex numbers, so I tried to see if Ohm's law used in electrical engineering could be implemented really easily in Python.

Ohm's law (direct current)

It is one of the most important laws in electrical engineering and is the first formula to learn.

V = IR

Ohm'sLawDC.py


# V = IR for DC circuit
print('Empty a parameter to calculate')

V = float(input('Input V : ') or 0)
I = float(input('Input I : ') or 0)
R = float(input('Input R : ') or 0)

if not V:
    V = R * I
elif not I:
    I = V / R
elif not R:
    R = V / I
else:
    print('Empty a parameter to calculate')

print('V =', V, '[V]')
print('I =', I, '[A]')
print('R =', R, '[Ω]')

This is the first input part, but it is assumed that the parameters entered empty will be calculated. Therefore, if you convert the input as it is as shown below, an error will occur at the time of conversion with the parameter of empty input.

V = float(input('Input V : '))

Therefore, I wrote that after inputting with str type once, it is converted only when the input is not empty. I really wish I could have a simpler notation ... I checked the initial value of input and whether it can be set, but I could not find such an argument orz

2019/11/10 postscript I was told in the comments that the following notation can be used. By doing this, I was able to convert it to a number without an if statement. it's amazing!

V = float(input('Input V : ') or 0)

The or operator actually judges the value on the left side as true or false and returns the result. Deep, I see ... https://docs.python.org/ja/3/library/stdtypes.html#boolean-operations-and-or-not

The result is output as follows.

Empty a parameter to calculate
Input V : 100
Input I : 
Input R : 50
V = 100.0 [V]
I = 2.0 [A]
R = 50.0 [Ω]

Ohm's law (exchange)

This is the main subject of this time. I didn't use complex numbers at all for direct current, but for alternating current, I need to calculate complex numbers with impedance (Z) added.

v = iz
z = \sqrt{(r^2+x^2)}

The difference from direct current is that ** $ x $ (reactance) ** is added to the parameters. This reactance represents the imaginary component in the AC circuit.

Now, let's write Ohm's law in an AC circuit in Python. Make sure to calculate for the empty parameter of $ v $, $ i $, $ r $, $ x $.

Ohm'sLawAC.py


# v = iz for AC circuit

print('Empty a parameter to calculate')

v = complex(input('Input v[V] : ') or 0)
i = complex(input('Input i[A] : ') or 0)
r = complex(input('Input r[Ω] : ') or 0)
x = complex(input('Input x[jΩ] : ') or 0)*1j

if not v:
    z = (r ** 2 + (x / 1j) ** 2) ** (1/2)
    v = z * i
elif not i:
    z = (r ** 2 + (x / 1j) ** 2) ** (1/2)
    i = v / z
elif not r:
    z = v / i
    r = (z ** 2 - (x / 1j) ** 2) ** (1/2)
elif not x:
    z = v / i
    x = (z ** 2 - r ** 2) ** (1/2) *1j
else:
    print('Empty a parameter to calculate')

print('v =', v, '[V]')
print('i =', i, '[A]')
print('r =', r, '[Ω]')
print('x =', x, '[jΩ]')
print('z =', z, '[Ω]')

Surprisingly, it was difficult to input real numbers and internally convert real numbers to complex numbers. It's easy as long as you know that you can convert real numbers to imaginary numbers with "* 1j".

The result is output as follows.

Empty a parameter to calculate
Input v[V] : 10
Input i[A] : 2
Input r[Ω] : 3
Input x[jΩ] : 
v = (10+0j) [V]
i = (2+0j) [A]
r = (3+0j) [Ω]
x = 4j [jΩ]
z = (5+0j) [Ω]

Ohm's law could definitely be calculated using complex numbers. There were occasional trips such as the input / output part and the conversion method from real numbers to complex numbers, but if you know how to write imaginary numbers in Python, the rest will work as long as you write the calculation formula obediently, so there is almost no problem. was. I think it was really easy.

After all you are amazing! Python-kun!

Recommended Posts

Ohm's Law with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python
Python programming with Atom
Learning Python with ChemTHEATER 02
Use Python 3.8 with Anaconda
Competitive programming with python
Handle rabbimq with python