▲ 0 r/AskProgramming
Is this a good way to build a math program?
degree = int(input("Of what degree is your function?[0-51]: "))
degree_value = degree
function = ""
constant = 97
while degree >= 0:
if degree > 1:
function = function + "" + chr(constant) + "(x^" + str(degree) + ") + "
elif degree == 1:
function = function + "" + chr(constant) + "(x) + "
elif degree == 0:
function = function + "" + chr(constant)
degree = degree - 1
constant = constant + 1
if constant == 123:
constant = 65
print(function)
constant = 97
constants = []
degree = degree_value
while degree >= 0:
new_constant = (float(input(chr(constant) + " = ")))
constants.append(new_constant)
degree = degree - 1
constant = constant + 1
if constant == 123:
constant = 65
number_of_terms = len(constants)
function = ""
location = 0
degree = degree_value
while number_of_terms > 0:
if constants[location] == 0 and degree == 0:
function = function + "0"
elif constants[location] == 0:
function = function
elif constants[location] != 1:
if degree > 1:
function = (function + str(constants[location]) + "x^"
+ str(degree) + " + ")
elif degree == 1:
function = function + str(constants[location]) + "x + "
elif degree == 0:
function = function + str(constants[location])
elif constants[location] == 1:
if degree > 1:
function = function + "x^" + str(degree) + " + "
elif degree == 1:
function = function + "x + "
elif degree == 0:
function = function + str(constants[location])
number_of_terms = number_of_terms - 1
location = location + 1
degree = degree - 1
print("f(x) = " + function)
derivative = ""
location = 0
degree = degree_value
while degree >= 1:
if (constants[location] == 0 and degree == 1):
derivative = derivative + "0"
elif (constants[location] * degree) != 1 and (constants[location] * degree) != -1:
if degree > 2:
derivative = derivative + str(constants[location] * degree) + "x^" + str(degree - 1) + " + "
elif degree == 2:
derivative = derivative + str(constants[location] * degree) + "x + "
elif degree == 1:
derivative = derivative + str(constants[location])
elif (constants[location] * degree) == 1 or (constants[location] * degree) == -1:
if degree >= 3:
derivative = derivative + "x^" + str(degree - 1) + " + "
elif degree == 2:
if constants[location] > 0:
derivative = derivative + "x + "
else:
derivative = derivative + "-x + "
elif degree == 1:
if constants[location] > 0:
derivative = derivative + "1"
else:
derivative = derivative + "-1"
degree = degree - 1
location = location + 1
print("f'(x) = " + derivative + "\n")
if derivative == "0":
print("f'(x) cannot equal zero.")
quit()
print("Newton-Rhapson method:")
finished = False
while not finished:
x = float(input("Initial guess = "))
iterations = int(input("How many iterations?: "))
function = ""
derivative = ""
x_n = 1
number_of_constants = len(constants) - 1
location = 0
while number_of_constants >= 0:
if number_of_constants > 0:
function = function + str(constants[location]) + " * x ** " + str(number_of_constants) + " + "
if number_of_constants > 1:
derivative = derivative + str(constants[location] * number_of_constants) + " * x ** " + str(number_of_constants - 1) + " + "
if number_of_constants == 1:
derivative = derivative + str(constants[location])
if number_of_constants == 0:
function = function + str(constants[location])
number_of_constants = number_of_constants - 1
if number_of_constants >= 0:
location = location + 1
print("x0 = " + str(x))
while x_n <= iterations:
try:
x = x - (eval(function))/eval((derivative))
print("x" + str(x_n) + " = " + str(x))
x_n = x_n + 1
except:
print("f'(x0) cannot equal zero.")
quit()
answer = input("Do you want to do another approximation?[y/n]: ")
if answer == "n":
finished = True
u/ice_or_flames — 2 days ago