Created
March 29, 2019 02:32
-
-
Save henry232323/fdbbc771aa43c80b961732939e7ba563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
from matplotlib.pyplot import quiver | |
import matplotlib.pyplot as plt | |
nodes = [] | |
kc = 8.99 * 10 ** 9 | |
ec = 1.60217662 * 10 ** (-19) | |
# x, y, charge | |
nodes.append((-1, 0, 4*ec)) | |
#nodes.append((0, 0, -4*ec)) | |
nodes.append((1, 0, 4*ec)) | |
""" | |
nodes.append((-1, 0, 4*ec)) | |
nodes.append((2, 3, -ec)) | |
nodes.append((-2, 2, 1.5*ec)) | |
nodes.append((2, -2.5, -3*ec)) | |
nodes.append((-4, -4, -2*ec)) | |
""" | |
def get_vector(x0, y0): | |
Fy, Fx = 0, 0 | |
for node in nodes: | |
x1, y1, c = node | |
dx, dy = x1 - x0, y1 - y0 | |
theta = math.atan2(dy, dx) | |
if dx + dy == 0: | |
Fe = 0 | |
else: | |
Fe = kc * c / (dx ** 2 + dy ** 2) | |
Fy += Fe * math.sin(theta) | |
Fx += Fe * math.cos(theta) | |
if math.isclose(Fx, 0, abs_tol=1e-15): | |
Fx = 0 | |
if math.isclose(Fy, 0, abs_tol=1e-15): | |
Fy = 0 | |
F = math.sqrt(Fy ** 2 + Fx ** 2) | |
""" | |
phi = math.atan2(Fy, Fx) | |
print(f"X-Force is {Fx}, Y-Force is {Fy}") | |
print(f"The Force is {F} in direction {phi}") | |
print(math.isclose(0, F)) | |
print(math.isclose(0, Fy), math.isclose(0, Fx)) | |
""" | |
return Fx, Fy, F, theta | |
def draw_graph(vector): | |
xs, ys = [], [] | |
us, vs = [], [] | |
for x in range(-50, 50, 5): | |
x = x/5 | |
xp, yp, up, vp = [], [], [], [] | |
for y in range(-50, 50, 5): | |
y = y/5 | |
xr, yr, fg, theta = vector(x, y) | |
xp.append(x) | |
yp.append(y) | |
if fg == 0: | |
xr, yr = 0, 0 | |
fg = 1 | |
up.append(xr/fg) | |
vp.append(yr/fg) | |
xs.append(xp) | |
ys.append(yp) | |
us.append(up) | |
vs.append(vp) | |
plt.figure() | |
plt.title("shit") | |
r = quiver(np.array(xs), np.array(ys), np.array(us), np.array(vs), units='dots') | |
plt.show() | |
draw_graph(get_vector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
from matplotlib.pyplot import quiver | |
import matplotlib.pyplot as plt | |
nodes = [] | |
kc = 8.99 * 10 ** 9 | |
ec = 1.60217662 * 10 ** (-19) | |
# x, y, charge | |
nodes.append((-1, 0, 4*ec)) | |
#nodes.append((0, 0, -4*ec)) | |
nodes.append((1, 0, 4*ec)) | |
""" | |
nodes.append((-1, 0, 4*ec)) | |
nodes.append((2, 3, -ec)) | |
nodes.append((-2, 2, 1.5*ec)) | |
nodes.append((2, -2.5, -3*ec)) | |
nodes.append((-4, -4, -2*ec)) | |
""" | |
def get_vector(x0, y0): | |
Fy, Fx = 0, 0 | |
for node in nodes: | |
x1, y1, c = node | |
dx, dy = x1 - x0, y1 - y0 | |
theta = math.atan2(dy, dx) | |
if dx + dy == 0: | |
Fe = 0 | |
else: | |
Fe = kc * c / (dx ** 2 + dy ** 2) | |
Fy += Fe * math.sin(theta) | |
Fx += Fe * math.cos(theta) | |
if math.isclose(Fx, 0, abs_tol=1e-15): | |
Fx = 0 | |
if math.isclose(Fy, 0, abs_tol=1e-15): | |
Fy = 0 | |
F = math.sqrt(Fy ** 2 + Fx ** 2) | |
""" | |
phi = math.atan2(Fy, Fx) | |
print(f"X-Force is {Fx}, Y-Force is {Fy}") | |
print(f"The Force is {F} in direction {phi}") | |
print(math.isclose(0, F)) | |
print(math.isclose(0, Fy), math.isclose(0, Fx)) | |
""" | |
return Fx, Fy, F, theta | |
def draw_graph(vector): | |
xs, ys = [], [] | |
us, vs = [], [] | |
for x in range(-50, 50, 5): | |
x = x/5 | |
xp, yp, up, vp = [], [], [], [] | |
for y in range(-50, 50, 5): | |
y = y/5 | |
xr, yr, fg, theta = vector(x, y) | |
xp.append(x) | |
yp.append(y) | |
if fg == 0: | |
xr, yr = 0, 0 | |
fg = 1 | |
up.append(xr/fg) | |
vp.append(yr/fg) | |
xs.append(xp) | |
ys.append(yp) | |
us.append(up) | |
vs.append(vp) | |
plt.figure() | |
plt.title("shit") | |
r = quiver(np.array(xs), np.array(ys), np.array(us), np.array(vs), units='dots') | |
plt.show() | |
draw_graph(get_vector) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment