Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create NewtonsMethod.py
Some of my notes are a bit of a mess. Let me know if you need help with what I did.
  • Loading branch information
jjv14001 committed Sep 30, 2017
1 parent 7a65efe commit 4661e1d
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions NewtonsMethod.py
@@ -0,0 +1,69 @@
import math
import numpy as np

def T1(x):
return x
def T1P(x):
return 1
def T2(x):
return 2*(x**2) - 1
def T2P(x):
return (4*x)
def T3(x):
return 4 * (x**3) - 3*x
def T3P(x):
return (12*(x**2)) - 3
def T4(x):
return 8*(x**4) - 8*(x**2) + 1
def T4P(x):
return (32*(x**3))-(16*x)

def Newtons(x, y):
if (((x > 1) and (y > 1)) or ((x < -1) and (y < -1))):
result = math.sqrt(((x - 1)**2) + ((y - 1)**2))
result2 = math.sqrt(((x + 1) ** 2) + ((y + 1)**2))
print("Distance: " + str(min(result,result2)) + " Point: (1,1)")
#this obviously only work for this assignment bc im lazy
return 0
# HERE WE CHOOSE N
n = 10
# Pick n points by dividing the interval by 1/n
# nlist = [-1 + (1/3), -1 + (2/3), -1 + (3/3), -1 + (4/3), -1 + (5/3), -1 + (6/3)]
#k = 10
currbestpt = [0,0]
currbestdist = 1809418903
for i in range(-50,50,5):
i = i/50 #Can't use floats in range so n = 10
pt1 = [i, T2(i)]
pt2 = [x,y]
v1 = [pt2[0] - pt1[0], pt2[1] - pt1[1]]
# Tangent Vector
v2 = [i,T2P(i)]
dist = math.sqrt(((pt2[0]-pt1[0])**2)+((pt2[1]-pt1[1])**2))
if (dist < currbestdist) and (dist != 0):
currbestpt = pt1
currbestdist = dist
print("Distance: " + str(currbestdist) + " Point: " + str(currbestpt))
return 0



#Newtons(2,2)
Newtons(3,0)
# dot product - np.dot([v1],[v2])
#Test the endpoints, print closest one, return)
# If condition: IF it's a point outside of the interval but still on the function
# Return whichever one of the endpoints has a closer distance
# All the intervals are from [-1,1]
# Divide curve into 1/n segments and from there get n points on the curve
# loop:
# Test each distance
# v1 - vector from the point to pk
# v2 - tangent line at that point
# v1 * v2 = 0 - test how close it is to epsilon (+- 10^-6)
# Break if it's within epsilon and save that point
# Keep a list of 'close points'
# Apply eqn to find next points
# pk = p(k-1) - f(pk-1)/f*(pk-1)
# Test endpoints
# Print the closest

0 comments on commit 4661e1d

Please sign in to comment.