-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path001.3D_Magnetic_Field_Lines_Around_a_Wire.py
57 lines (48 loc) · 1.84 KB
/
001.3D_Magnetic_Field_Lines_Around_a_Wire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# Given data
current = 30.0 # Amperes
force = 2.16 # Newtons
wire_length = 0.04 # meters (4.00 cm)
# Calculate the average field strength (B) using the formula: B = F / (I * L)
average_field_strength = force / (current * wire_length)
# Define a grid of points to represent the magnetic field
x = np.linspace(-0.1, 0.1, 20)
y = np.linspace(-0.1, 0.1, 20)
z = np.linspace(-0.02, 0.02, 20)
X, Y, Z = np.meshgrid(x, y, z)
# Calculate the magnetic field strength at each point due to the wire
mu_0 = 4 * np.pi * 1e-7 # Magnetic constant (permeability of free space)
B_x = (mu_0 * current * wire_length / (4 * np.pi)) * (X - 0) / ((X - 0)**2 + Y**2 + Z**2)**(3 / 2)
B_y = (mu_0 * current * wire_length / (4 * np.pi)) * Y / ((X - 0)**2 + Y**2 + Z**2)**(3 / 2)
B_z = (mu_0 * current * wire_length / (4 * np.pi)) * Z / ((X - 0)**2 + Y**2 + Z**2)**(3 / 2)
# Create a DataFrame for the magnetic field data
field_data = pd.DataFrame({'X': X.flatten(), 'Y': Y.flatten(), 'Z': Z.flatten(),
'B_x': B_x.flatten(), 'B_y': B_y.flatten(), 'B_z': B_z.flatten()})
# Create a 3D scatter plot of the magnetic field lines using Plotly
fig = go.Figure(data=go.Scatter3d(
x=field_data['X'],
y=field_data['Y'],
z=field_data['Z'],
mode='markers',
marker=dict(
size=2,
color=field_data['B_z'], # Color based on B_z component
colorscale='Viridis',
colorbar=dict(title='Magnetic Field (B_z)'),
)
))
fig.update_layout(
scene=dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z',
),
title='Magnetic Field Lines Around a Wire',
)
# Display the Plotly interactive plot
fig.show()
# Print the average field strength
print(f"Average Field Strength (B): {average_field_strength} Tesla")