Search code examples
pythonmathintegral

Integral Calculator wont work when given formula given more than 1 x in the formula


So I'm working on an integral calculator right now, and it works fairly well, although as soon as defining y as x**2+x*2+2 it stops working. What prompts my program to work with y = x**2+2 but not with y = x**2+x*2+2?

import math as math

x0 = 0                                                                                      
x1 = 0                                                                                      

def func(y, x):                                                                             
    return eval(y)

def func2(z, x):                                                                            
    return eval(z)

def func3(c, d):        
    a = 0.0                                                                                 
    for i in range(1, n+1):                                                                 
        x0 = a + (i-1) * dx                                                                     
        Ai = dx * (c + d)/ 2.                                                           
        a = a + Ai                                                                      
    return a

y = str(raw_input("Function 1: "))                                                      
z = str(raw_input("Function 2: "))                                          

a = float(input("Left boundary: "))                                                     
b = float(input("Right boundary: "))                                                    
dx = float(input("Trapezoid width: "))                                                      

n = int((b - a) / dx)                                                               

Area2 = func3(func(y, x0), func(y, x1))                                                                             
Area3 = func3(func2(z, x0), func2(z, x1))

Area4 = Area2 - Area3
if Area4 < 0:
    Area4 = Area3 - Area2

print "Area = ", Area4                                                                  

Solution

  • import math as math
    
    x0 = 0                                                                                      
    x1 = 0                                                                
    
    def f(function, x):
       function = eval(function)
       return function
    
    def func3(b, a, func):        
       area = 0.0
       dx = (b - a) / n                                                
       for i in range(1, n+1):                                                                 
           x0 = a + (i-1) * dx  
           x1 = a + i*dx
           Ai = dx * (f(func, x0) + f(func, x1))/ 2.                                                          
           area = area + Ai                                                                      
       return area
    
    y = str(input("Function 1: "))                                                      
    z = str(input("Function 2: "))                                          
    
    a = float(input("Left boundary: "))                                                     
    b = float(input("Right boundary: "))                                                    
    n = int(input("Trapezoid width: "))                                                      
    
    
    Area2 = func3(b, a, y)                                                                             
    Area3 = func3(b, a, z)
    
    Area4 = Area2 - Area3
    if Area4 < 0:
       Area4 = Area3 - Area2
    
    print "Area = ", Area4
    

    Is this more or less what you wanted to do?