Hello, I would like to find the intersection of two functions. One of them is a user defined function which I am using for a fit, the other one is a constant (for now).
When I subtract one from the other and take the absolute value in a new function I receive an error message upon running the code :
Error in TFormula::Eval: Formula is invalid and not ready to execute
g2 is unknown
I guess this is because the function g2 is defined with parameters, that are not fixed, even though it set them to definite values.
Here a minimal example which runs and gives the Error message(s).
from ROOT import TCanvas, TF1, TGraph
# Python libraries :
import numpy as np
class gen_expo :
def __call__(self, x, parameters) :
a = parameters[0]
b = parameters[1]
c = parameters[2]
x = x[0]
y = np.exp(a + b*x) + c
return y
t0 = 219.0
t1 = 219.8
# Functions :
g2 = TF1("g2", gen_expo(), t0, t1, 3)
g_const = TF1("g_const", "[0]", t0, t1)
# Set function parameters :
g_const.SetParameter(0, 1.3)
const = -10000.013040075753
slope = 45.51314504839986
c = 1.0519174076078592
g2.SetParameters(const, slope, c)
# Diff-function (that does not work yet).
gd = TF1("gd", "abs(g2-g_const)", t0, t1 )
width = 600
height = 500
can_i = TCanvas("can_i", " Intersection of two functions ", width, height )
can_i.cd()
g2.Draw("ac*")
g_const.Draw("al* same")
gd.Draw("al* same" )
can_i.Update()
input("Press enter to continue. ")
How can I make the gd function work in order to determine the intersection of the two functions?
Thanks @couet for this example in cpp. Written in a very simple form it works in pyroot with the functions I used in the post above. The working code below however, does not work if I set a parameter in the g_const function
Also, I wonder how to use functions defined as part of a python class to get the intersection. I tried to translate the example you posted, but it did not work out so far.