0

I am trying to solve linear equations with a numpy array based on user inputs. I've programmed a simple calculator that can deal with every day problems, but I would like to add the feature where you can choose to solve a linear equation.
Exp:

2x + 10 = 20 ==> x = 5 

In above example, how would I use user inputs with a numpy array?

print('Select Mathematical Operation.')
print('1. Addition') 
print('2. Subtraction')
print('3. Multiplication')
print('4. Division')

userchoice = input ('Enter choice (1/2/3/4): ')

import numpy as np
x = int(input("Enter x:"))
y = int(input("Enter y:"))
w = int(input("Enter w:"))
a = np.array([[x],[y]])
b = np.array([w])
z = np.linalg.solve(a,b)
return z

1 Answers1

0

You can create a matrix with n dimensions (n being the number of unknown variables) and add all the values to that matrix, then invert it and multiple it by right side of the equation to find variable matrix. All those operators available at numpy. You can detect letters with string processing to determine number of variables and values. Or ask to user about it. For mathematical background: https://m.youtube.com/watch?v=7euvxjnvzYc