Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #a - [f(a)/f'(a)] = f(b)
- #polynomial = input("Input polynomials in the form ax^n + bx^n-1 + ...")
- polyCoef = []
- deriv_polyCoef = []
- polyValue = []
- deriv_polyValue = []
- deriv_polyCoef = []
- i = 0
- j = 0
- def main():
- polyLength = int(input("How many terms are in your polynomial? (including zero terms) "))
- for i in range(polyLength):
- coef = int(input(str(i + 1) + " coefficient "))
- polyCoef.append(coef)
- print(polyCoef)
- print("Is this your polynomial?")
- for j in range(polyLength):
- print(str(polyCoef[j]) + "x^" + str(polyLength - j - 1))
- polyContinue = input("If it is not, type 'n' to re-input coefficients. Otherwise, type anything else to continue ")
- if polyContinue != "n":
- for k in range(polyLength):
- deriv_power = int(polyLength - k - 1) #New power after differentiation
- deriv_coef = int(polyCoef[k]) * deriv_power #New coefficient after differentiation
- deriv_polyCoef.append(deriv_coef)
- print(deriv_polyCoef)
- print("This is your new polynomial, ")
- for l in range(polyLength):
- print(str(deriv_polyCoef[l]) + "x^" + str(int(polyLength - l - 2)))
- else:
- polyCoef.clear()
- main()
- def newtonMthd():
- newtonIteration = int(input("Type your starting point.")) #this is the starting point
- repeat = int(input("How many iterations would you like?"))
- for iteration in range(repeat):
- print("This is your starting point " + str(newtonIteration))
- for m in range(len(polyCoef)):
- polyInput = polyCoef[m] * (int(newtonIteration) ** (len(polyCoef) - m - 1)) #plugs in the starting point into the given polynomial
- polyValue.append(polyInput) #this is f(x1)
- for n in range(len(polyCoef) - 1):
- deriv_polyInput = int(deriv_polyCoef[n]) * (int(newtonIteration) ** (len(polyCoef) - n - 2)) #plugs in the starting point into the derivative of f(x)
- deriv_polyValue.append(deriv_polyInput) #this is f'(x1)
- newtonIteration = int(newtonIteration) - ((int(sum(polyValue)))/int((sum(deriv_polyValue)))) #this is the generalized form of newton's method to solve for x_2
- print("Iteration = " + str(newtonIteration))
- main()
- newtonMthd()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement