Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE3802/NewtonsMethod.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (64 sloc)
2.01 KB
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 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 |