I am creating a program, which through differentiation, can return the coordinates and nature of any stationary points, given an equation.
My whole program so far is:
equation = input("y=")
expression_list = []
for i in equation:
expression_list.append(i)
def derive(expression):
global derivation
derivation = []
for i in expression:
if i == "x" and int(expression[expression.index(i)+2]) != 0:
derivation.append(int(expression[expression.index(i)-1]) * int(expression[expression.index(i)+2]))
derivation.append("x")
derivation.append("^")
derivation.append(int(expression[expression.index(i)+2]) - 1)
derive(expression_list)
expression_list_1st_derivative = derivation
print(expression_list_1st_derivative)
The area of the program that I am having trouble with is:
def derive(expression):
global derivation
derivation = []
for i in expression:
if i == "x" and int(expression[expression.index(i)+2]) != 0:
derivation.append(int(expression[expression.index(i)-1]) * int(expression[expression.index(i)+2]))
derivation.append("x")
derivation.append("^")
derivation.append(int(expression[expression.index(i)+2]) - 1)
derive(expression_list)
expression_list_1st_derivative = derivation
print(expression_list_1st_derivative)
Using y=4x^5+3x^4
as an example, I have created this for loop in the hope of giving ["20","x","^","4","12","x","^","3"]
which, if you have studied calculus, you will know as x/dx. I have not included operators yet; that being something I will work on after clearing this hurdle. Currently these lines of code instead give ["20","x","^","4","20","x","^","4"]
. What changes must I make to get the correct result?