電磁気学



```
import numpy as np

# Constants
epsilon_0 = 8.854187817e-12  # Permittivity of free space (F/m)

# Function definitions
def point_potential(q, b, d):
    return (q / (2 * np.pi * epsilon_0)) * np.log(b / d)

def cylinder_capacitance(b, d):
    return 2 * np.pi * epsilon_0 / np.log(b / d)

def combined_capacitance(a, b, c):
    return 2 * np.pi * epsilon_0 / np.log(b / a) + 2 * np.pi * epsilon_0 / np.log(c / b)

# Input parameters
q = 1e-9  # Point charge (C)
a = 0.01  # Inner radius of inner cylinder (m)
b = 0.02  # Outer radius of inner cylinder (m)
c = 0.03  # Inner radius of outer cylinder (m)
d = 0.04  # Position of point P (m)

# Calculations
V_p = point_potential(q, b, d)
C_inner = cylinder_capacitance(b, d)
C_combined = combined_capacitance(a, b, c)

# Results display
print(f"Potential at point P V: {V_p:.3e} V")
print(f"Capacitance of inner cylinder C (b, d): {C_inner:.3e} F")
print(f"Combined capacitance C (a, b, c): {C_combined:.3e} F")

```


```

import numpy as np

# 定数の設定
mu_0 = 4 * np.pi * 1e-7  # 真空の透磁率
I = 1.0  # 電流 [A]
a = 1.0  # 半径 [m]
z1 = 1.0  # z軸上の距離 [m]

# 3番の式に対応: 微小要素 ds による磁場の寄与
dB_ds = (mu_0 * I) / (4 * np.pi * (a**2 + z1**2))

# 4番の式に対応: 計算するための補助式
H = a / np.sqrt(a**2 + z1**2)

# 5番の式に対応: ds による磁場の寄与（冪乗を追加）
dB_ds_mod = (mu_0 * a * I) / (4 * np.pi * (a**2 + z1**2)**(3/2))

# 6番の式に対応: 積分後の磁場
B_z1 = (mu_0 * a**2 * I) / (2 * (a**2 + z1**2)**(3/2))

# 結果の表示
print(f"微小要素 ds による磁場の寄与 dB/ds: {dB_ds}")
print(f"補助式 H の値: {H}")
print(f"ds による磁場の寄与 (修正版) dB/ds: {dB_ds_mod}")
print(f"積分後の磁場 B(z1): {B_z1}")

```






```
import numpy as np
import matplotlib.pyplot as plt

# Constants
rho = 1  # Set this to any value you want
epsilon_0 = 1  # Permittivity of free space
a = 1  # Radius

# Define the range of r values
r1 = np.linspace(0.01, a, 400)  # 0 < r < a (avoid r = 0 to prevent division by zero)
r2 = np.linspace(a, 3*a, 400)   # r > a

# Electric field E(r)
E1 = (rho * a**3) / (3 * epsilon_0 * r1**2)
E2 = (rho * a**2) / (3 * epsilon_0 * r2)

# Potential V(r)
V1 = (rho * a**2) / (2 * epsilon_0 * a) - (rho * r1**2) / (6 * epsilon_0)
V2 = (rho * a**2) / (2 * epsilon_0 * r2)

# Plot Electric Field
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.plot(r1, E1, label=r'$E(r) \, \text{for} \, 0 < r < a$')
plt.plot(r2, E2, label=r'$E(r) \, \text{for} \, r > a$')
plt.xlabel('r')
plt.ylabel('E(r)')
plt.title('Electric Field E(r)')
plt.axvline(x=a, color='black', linestyle='--', label=r'$r = a$')
plt.axhline(y=rho*a/(3*epsilon_0), color='black', linestyle='--')
plt.legend()

# Plot Potential
plt.subplot(1, 2, 2)
plt.plot(r1, V1, label=r'$V(r) \, \text{for} \, 0 < r < a$')
plt.plot(r2, V2, label=r'$V(r) \, \text{for} \, r > a$')
plt.xlabel('r')
plt.ylabel('V(r)')
plt.title('Potential V(r)')
plt.axvline(x=a, color='black', linestyle='--', label=r'$r = a$')
plt.axhline(y=rho*a**2/(2*epsilon_0*a), color='black', linestyle='--')
plt.legend()

plt.tight_layout()
plt.show()
```

```

import numpy as np
import matplotlib.pyplot as plt

# Constants
rho = 1  # Set to any value you want
epsilon_0 = 1  # Permittivity of free space
a = 1  # Radius
d = 1  # Displacement

# Create a grid for plotting
x = np.linspace(-2, 2, 400)
y = np.linspace(-2, 2, 400)
X, Y = np.meshgrid(x, y)

# Part 1 (c): Electric field E = (-grad V)
Ex1 = (rho / (3 * epsilon_0)) * X
Ey1 = (rho / (3 * epsilon_0)) * Y

# Part 2 (2)(a): Electric field E1 + E2
rho1 = 1  # You can set this to any value
Ex2 = (rho + rho1) / (3 * epsilon_0) * X - rho1 * d / (3 * epsilon_0)
Ey2 = (rho + rho1) / (3 * epsilon_0) * Y

# Part 2 (2)(b): Resulting field when rho1 = -rho
Ex3 = (rho * d) / (3 * epsilon_0)
Ey3 = 0

# Plotting
plt.figure(figsize=(15, 5))

# Subplot for Part 1 (c)
plt.subplot(1, 3, 1)
plt.quiver(X, Y, Ex1, Ey1)
plt.title("Electric Field E (Part 1 (c))")
plt.xlabel('x')
plt.ylabel('y')

# Subplot for Part 2 (2)(a)
plt.subplot(1, 3, 2)
plt.quiver(X, Y, Ex2, Ey2)
plt.title("Electric Field E (Part 2 (2)(a))")
plt.xlabel('x')
plt.ylabel('y')

# Subplot for Part 2 (2)(b)
plt.subplot(1, 3, 3)
plt.quiver(X, Y, Ex3, Ey3)
plt.title("Electric Field E (Part 2 (2)(b))")
plt.xlabel('x')
plt.ylabel('y')

plt.tight_layout()
plt.show()

```

```

import numpy as np
import matplotlib.pyplot as plt

# Constants
mu_0 = 1  # Permeability of free space
b = 1     # Magnetic field strength
l1 = 1    # Length of the conductor
R = 1     # Distance
a = 0.5   # Another distance
omega = 2 * np.pi  # Angular frequency (let's assume 1 Hz for simplicity)

# Time array
t = np.linspace(0, 2, 1000)

# Magnetic flux
phi = (mu_0 * b * l1 / (4 * np.pi)) * np.log((R**2 + a**2 + 2 * R * a * np.cos(omega * t)) / 
                                            (R**2 + a**2 - 2 * R * a * np.cos(omega * t)))

# Induced EMF
V = (mu_0 * b * l1 * omega * (R**2 + a**2) * np.sin(omega * t)) / \
    (np.pi * ((R**2 + a**2 + 2 * R * a * np.cos(omega * t)) * 
              (R**2 + a**2 - 2 * R * a * np.cos(omega * t))))

# Plotting
plt.figure(figsize=(12, 6))

# Magnetic flux plot
plt.subplot(2, 1, 1)
plt.plot(t, phi)
plt.title(r'Magnetic Flux $\phi(t)$')
plt.xlabel('Time (s)')
plt.ylabel(r'$\phi(t)$')
plt.grid(True)

# Induced EMF plot
plt.subplot(2, 1, 2)
plt.plot(t, V)
plt.title('Induced Electromotive Force V(t)')
plt.xlabel('Time (s)')
plt.ylabel('V(t)')
plt.grid(True)

plt.tight_layout()
plt.show()

```

```



import numpy as np
import matplotlib.pyplot as plt

# Constants
rho0 = 1  # Example value for charge density
epsilon0 = 8.854187817e-12  # Permittivity of free space
d = 1  # Example value for d

# Define the piecewise function E(x)
def E(x):
    E_values = np.piecewise(x, 
                            [(-d/3 <= x) & (x < 0), (0 <= x) & (x <= 2*d/3)],
                            [lambda x: (2 * rho0 / epsilon0) * x - (2 * rho0 / (3 * epsilon0)) * d,
                             lambda x: (rho0 / epsilon0) * x - (2 * rho0 / (3 * epsilon0)) * d])
    return E_values

# Define the range of x values
x_values = np.linspace(-d/3, 2*d/3, 400)

# Calculate the corresponding E(x) values
E_values = E(x_values)

# Plot the function
plt.figure(figsize=(8, 6))
plt.plot(x_values, E_values, label=r'$E(x)$')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.grid(color = 'gray', linestyle = '--', linewidth = 0.5)
plt.title(r'Piecewise Function $E(x)$')
plt.xlabel(r'$x$')
plt.ylabel(r'$E(x)$')
plt.legend()
plt.show()

```


```
import numpy as np
import matplotlib.pyplot as plt

# Define the parameters
a = 1  # Inner radius of cylinder A (example value)
b = 2  # Outer radius of cylinder A and inner radius of cylinder B (example value)
c = 3  # Outer radius of cylinder B (example value)
q = 1  # Total charge (example value)

# Calculate the natural logarithms
ln_b_a = np.log(b / a)
ln_c_b = np.log(c / b)

# Calculate the induced charges q_A and q_C
q_A = (ln_b_a / (ln_b_a + ln_c_b)) * q
q_C = (ln_c_b / (ln_b_a + ln_c_b)) * q

# Print the results
print(f"Induced charge q_A: {q_A:.3e} C")
print(f"Induced charge q_C: {q_C:.3e} C")

# Plot the results
fig, ax = plt.subplots()
bars = ax.bar(['q_A', 'q_C'], [q_A, q_C], color=['blue', 'red'])

# Add labels and title
ax.set_ylabel('Charge (C)')
ax.set_title('Induced Charges in the Cylindrical Capacitor')
ax.bar_label(bars)

# Display the plot
plt.show()

```

```
import numpy as np
import matplotlib.pyplot as plt

# Constants
sigma_1 = 1  # Example value for surface charge density on inner cylinder (C/m^2)
sigma_2 = 1  # Example value for surface charge density on outer cylinder (C/m^2)
epsilon_1 = 8.854187817e-12  # Permittivity of free space (F/m)
a = 1  # Inner radius of inner cylinder (m)
b = 3  # Outer radius of outer cylinder (m)
c = 2  # Outer radius of inner cylinder / Inner radius of outer cylinder (m)
V = 1  # Voltage (V)

# Calculate constants for E field
log_b_c = np.log(b / c)
log_c_a = np.log(c / a)
denominator = sigma_1 * log_b_c + sigma_2 * log_c_a

# Define the electric field E(r)
def E(r):
    E_field = np.zeros_like(r)
    # For a < r < c
    mask_1 = (a < r) & (r < c)
    E_field[mask_1] = -sigma_2 / (r[mask_1] * denominator) * V
    # For c < r < b
    mask_2 = (c < r) & (r < b)
    E_field[mask_2] = -sigma_1 / (r[mask_2] * denominator) * V
    return E_field

# Define the electric displacement D
D = epsilon_1 * (sigma_2 / (c * denominator)) * V

# Generate r values for plotting
r_values = np.linspace(a, b, 800)
E_values = E(r_values)

# Plot the electric field E(r)
plt.figure(figsize=(10, 6))
plt.plot(r_values, E_values, label=r'$E(r) \, (a < r < b)$')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(a, color='black', linestyle='--', linewidth=0.5)
plt.axvline(b, color='black', linestyle='--', linewidth=0.5)
plt.axvline(c, color='black', linestyle='--', linewidth=0.5)
plt.title('Electric Field $E(r)$')
plt.xlabel('$r$ (m)')
plt.ylabel('$E(r)$ (V/m)')
plt.legend()
plt.grid(True)
plt.show()

# Print the electric displacement D
print(f"Electric displacement D: {D:.3e} C/m^2")

```

```

import numpy as np
import matplotlib.pyplot as plt

# 定数の設定
mu_0 = 1.0  # 定数
a = 1.0     # 定数
b = 1.0     # 定数
t0 = 1.0    # 時間スケールの定数

# 時間 t の範囲を設定
t = np.linspace(0, 5, 500)

# 電圧 V(t) の式を計算
V_t = (mu_0 * a**2 * b**2 / (2 * (a**2 + b**2)**(3/2))) * (t / t0)

# グラフのプロット
plt.plot(t, V_t)
plt.xlabel('t')
plt.ylabel('V(t)')
plt.title('Voltage V(t) vs Time t')
plt.grid(True)
plt.show()

```