Skip to content

Instantly share code, notes, and snippets.

@bahostetterlewis
Created June 5, 2015 00:52
Show Gist options
  • Save bahostetterlewis/c49af5a5b0847796f813 to your computer and use it in GitHub Desktop.
Save bahostetterlewis/c49af5a5b0847796f813 to your computer and use it in GitHub Desktop.
functional_solution.py
from __future__ import print_function, generators, division
from functools import partial
from itertools import imap
from operator import add
from visual import *
# Constants
K = 8.988e9
def e_at_point_from_point(charge_point, point):
r = point - charge_point["pos"]
return (K * charge_point["charge"] * norm(r)) / (mag(r) ** 2)
def point_gen(length, pieces):
return imap(lambda x: vector(x, 0, 0), linspace(-(length / 2), length / 2, pieces))
def charge_gen(length, charge, pieces):
return imap(lambda x: dict(charge=charge/pieces, pos=x), point_gen(length, pieces))
def e_at_point(point, rod, charge, n):
return vector(reduce(add, imap(partial(e_at_point_from_point, point=point), charge_gen(rod, charge, n))))
print("E at A:", e_at_point(point=vector(0, .05), rod=1, charge=1e-8, n=1000))
print("E at B:", e_at_point(point=vector(.25, .25), rod=1, charge=1e-8, n=1000))
print("E at B:", e_at_point(point=vector(.5, .5), rod=1, charge=1e-8, n=1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment