# Test7 And Poly

Content type
User Generated
Subject
Python
School
Hunter College
Type
Homework
Rating
Showing Page:
1/7
In this task, we are going to write a program test7.py that finds the
roots of cubic polynomials listed in the file poly.txt using goalSeek
function.
#Importing the needed modules to run this code
import re
import numpy as np
__FILENAME__ = "poly.txt"
imported = [] #The coeficients and values imported from poly.txt will be stored
in this list
try:
polyFile = open(__FILENAME__, "r")
except FileNotFoundError:
print("The file " + __FILENAME__ + " was not found in this directory")
exit()
#Go through each line in the file
for line in polyFile:
if(line[0] == "#"):
#This is a header line. Should be ignored.
continue
else:
#Store the firts 6 coeficients and values found this line
#We use a Regex as a parameter to the findall function to reference the v
alues we want from the line
imported.append(re.findall(r"[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-
+]?\d+)?",line)[0:6])
for index, line in enumerate(imported):
#Remove lines that are not polynomials
if(len(line) != 6):
imported.remove(line)
continue
#Convert the coeficients and values from str to float
for index2, val in enumerate(line):
imported[index][index2] = float(val)
#Print all the imported data to check if it was correctly imported
print("A B C D Lo Hi")
for line in imported:
print(line)

Showing Page:
2/7
#This funtion will return another function (P) which is a function representation
of a cubic polynomial with the values provided by makePoly
def makePoly(A,B,C,D):
def P(x):
return A*x*x*x + B*x*x + C*x + D
return P
#Attribution for this function to 2019 Hakan İbrahim Tol
def GoalSeek(fun,goal,x0,fTol=0.0001,MaxIter=10000):
# Goal Seek function of Excel
# via use of Line Search and Bisection Methods
# Inputs
# fun : Function to be evaluated
# goal : Expected result/output
# x0 : Initial estimate/Starting point
# Initial check
if fun(x0)==goal:
print('Exact solution found')
return x0
# Line Search Method
step_sizes=np.logspace(-1,4,6)
scopes=np.logspace(1,5,5)
vFun=np.vectorize(fun)
for scope in scopes:
break_nested=False
for step_size in step_sizes:
cApos=np.linspace(x0,x0+step_size*scope,int(scope))
cAneg=np.linspace(x0,x0-step_size*scope,int(scope))
cA=np.concatenate((cAneg[::-1],cApos[1:]),axis=0)
fA=vFun(cA)-goal
if np.any(np.diff(np.sign(fA))):
index_lb=np.nonzero(np.diff(np.sign(fA)))
if len(index_lb[0])==1:

Showing Page:
3/7

End of Preview - Want to read all 7 pages?
Access Now
Unformatted Attachment Preview
In this task, we are going to write a program test7.py that finds the roots of cubic polynomials listed in the file poly.txt using goalSeek function. #Importing the needed modules to run this code import re import numpy as np __FILENAME__ = "poly.txt" imported = [] #The coeficients and values imported from poly.txt will be stored in this list try: polyFile = open(__FILENAME__, "r") except FileNotFoundError: print("The file " + __FILENAME__ + " was not found in this directory") exit() #Go through each line in the file for line in polyFile: if(line[0] == "#"): #This is a header line. Should be ignored. continue else: #Store the firts 6 coeficients and values found this line #We use a Regex as a parameter to the findall function to reference the v alues we want from the line imported.append(re.findall(r"[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][+]?\d+)?",line)[0:6]) for index, line in enumerate(imported): #Remove lines that are not polynomials if(len(line) != 6): imported.remove(line) continue #Convert the coeficients and values from str to float for index2, val in enumerate(line): imported[index][index2] = float(val) #Print all the imported data to check if it was correctly imported print("A B C D Lo Hi") for line in imported: print(line) #This funtion will return another function (P) which is a function representation of a cubic polynomial with the values provided by makePoly def makePoly(A,B,C,D): def P(x): return A*x*x*x + B*x*x + C*x + D return P #Attribution for this fun ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

### Review

Anonymous
Goes above and beyond expectations!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4