It can definitely be done more efficiently. In the end what you've got is a series of calculations:
- Integrate LCDMf from 0 to 1.
- Integrate LCDMf from 0 to 2.
- Integrate LCDMf from 0 to 3.
So the very first thing you can do is change it to integrate distinct subintervals and then sum them (after all, what else is integration!):
- Integrate LCDMf from 0 to 1.
- Integrate LCDMf from 1 to 2, add result from step 1.
- Integrate LCDMf from 2 to 3, add result from step 2.
Next you can dive into LCDMf, which is defined this way:
1.0/np.sqrt(0.3*(1+x)**3+0.7)
You can use NumPy broadcasting to evaluate this function at many points instantly:
dx = 0.0001
x = np.arange(0, 100, dx)
y = LCDMf(x)
That should be quite fast, and gives you one million points on the curve. Now you can integrate it using scipy.integrate.trapz()
or one of the related functions. Call this with the y and dx already computed, using the workflow above where you integrate each interval and then use cumsum()
to get your final result. The only function you need to call in a loop then is the integrator itself.