Reputation: 161
I am trying to locate a region under the curve of two functions such that the area of this region equals a given number m
. For example, by integrating the two functions:
f(x) = 3x - 3x^2
g(x) = x
symbolically from a to b, and equating each to m (e.g. m=1/3). The system can be solved to find the values of a and b:
1.5 b^2 - b^3 - 1.5 a^2 + a^3 = 1/3
0.5 b^2 - 0.5 a^2 = 1/3
The positive solution I got to this is approximately: a = 0.364823, b = 0.894294
Now, my question is: how can I find a and b if f and g were given to me as vectors (or arrays in python) instead of symbolic functions? I know how to use trapz()
to find areas for vectors but I don't know if there is a way to use it to find a
and b
in the above problem numerically.
Thanks!
Upvotes: 4
Views: 1515
Reputation: 63757
You can try sympy
>>> from sympy import *
>>> x,a,b = symbols('x a b')
>>> fx = "3*x - 3*x**3"
>>> gx = "x"
>>> m = 1/3
>>> int_fx = integrate(fx, (x,a,b))
3*a**4/4 - 3*a**2/2 - 3*b**4/4 + 3*b**2/2
>>> int_gx = integrate(gx, (x,a,b))
-a**2/2 + b**2/2
>>> solve([Eq(int_fx, m), Eq(int_gx, m)],(a, b))
[(-0.577350269189626, -1.00000000000000), (-0.577350269189626, 1.00000000000000), (0.577350269189626, -1.00000000000000), (0.577350269189626, 1.00000000000000)]
By the way, I am having hard time to understand how you managed to get a definite integral solution of
1.5 b^2 - b^3 - 1.5 a^2 + a^3 = 1/3
for
f(x) = 3x - 3x^3
within
[a,b]
Upvotes: 2
Reputation: 308938
I think you need to look at integral equations: Numerical Recipes and SO, for example.
Upvotes: 0