Home > Enterprise >  Integrating formula with x values from array
Integrating formula with x values from array

Time:03-09

I want to integrate a formula for all x values that are in an array. How do I use the array for the values of x?

And do I need to integrate the formula by hand before that? The original formula that needs to be integrated would be (a*(4-x))-b

It is also important to note, that the array does contain multiple identical values

CodePudding user response:

Assuming you are trying to integerate in the range [0, 4], here's something you can do that does not require finding the integral by hand:

import scipy.integrate as integrate
import scipy.special as special

# Defining the function you're integrating by
def this_funct(x, a, b):
    return (a*(4-x)) - b

# Range you are integrating along
bottom_of_range = 0
top_of_range = 4

# Change a and b to be whatever you want
a = 3
b = 4

# Result contains the integral of this_funct in the range (bottom_of_range, top_of_range).
result = integrate.quad(lambda x: this_funct(x, a, b), bottom_of_range, top_of_range)[0]

You can change bottom_of_range, top_of_range, a and b as needed.

  • Related