Problem: Write a code that asks the user to input a functon with multiple variables and then integrates it using as limits numbers given by the user
This is the code that I have written so far
from sympy import Symbol, integrate, parse_expr
import time
import math
class Control():
def __init__(self):
self.run()
def one_variable(self):
func = input("Enter a mathematical function with one variable(x): ")
x_lower,x_upper = input("Enter the lower and upper limit for the x variable: ").split(',')
x = Symbol('x')
parsed_func = parse_expr(func)
start = time.time()
result = integrate(parsed_func, (x, x_lower, x_upper))
end = time.time()
t = end - start
print(result)
print('It took', t, 'seconds to calculate the integral')
def two_variables(self):
func = input("Enter a mathematical function with two variables(x,y): ")
x_lower,x_upper = input("Enter the lower and upper limit for the x variable: ").split(',')
y_lower,y_upper = input("Enter the lower and upper limit for the y variable: ").split(',')
x = Symbol('x')
y = Symbol('y')
parsed_func = parse_expr(func)
start = time.time()
result = integrate(parsed_func, (x, x_lower, x_upper), (y, y_lower, y_upper))
end = time.time()
t = end - start
print(result)
print('It took', t, 'seconds to calculate the integral')
def three_variables(self):
func = input("Enter a mathematical function with three variables(x,y,z): ")
x_lower,x_upper = input("Enter the lower and upper limit for the x variable: ").split(',')
y_lower,y_upper = input("Enter the lower and upper limit for the y variable: ").split(',')
z_lower,z_upper = input("Enter the lower and upper limit for the z variable: ").split(',')
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
parsed_func = parse_expr(func)
start = time.time()
result = integrate(parsed_func, (x, x_lower, x_upper), (y, y_lower, y_upper), (z, z_lower, z_upper))
end = time.time()
t = end - start
print(result)
print( 'It took', t, 'seconds to calculate the integral')
def four_variables(self):
func = input("Enter a mathematical function with four variables(x,y,z,w): ")
x_lower,x_upper = input("Enter the lower and upper limit for the x variable: ").split(',')
y_lower,y_upper = input("Enter the lower and upper limit for the y variable: ").split(',')
z_lower,z_upper = input("Enter the lower and upper limit for the z variable: ").split(',')
w_lower,w_upper = input("Enter the lower and upper limit for the w variable: ").split(',')
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
w = Symbol('w')
parsed_func = parse_expr(func)
start = time.time()
result = integrate(parsed_func, (x, x_lower, x_upper), (y, y_lower, y_upper), (z, z_lower, z_upper), (w, w_lower, w_upper))
end = time.time()
t = end - start
print(result)
print('It took', t, 'seconds to calculate the integral')
def run(self):
while True:
print('Choose the number of variables (1-4)')
reply = input("... ")
if not reply: break
if reply == '1':
self.one_variable()
if reply == '2':
self.two_variables()
if reply == '3':
self.three_variables()
if reply == '4':
self.four_variables()
if __name__ == "__main__":
Control()
I would like to know if there is a more efficient way to do it and also how to add a graphical user interface to the code using tkinter