Curve fitting to an arbitrary function #1081
-
Hi, I would like to get more explanations on how to use this function public static (double P0, double P1) Curve(double[] x, double[] y, Func<double, double, double, double> f, double initialGuess0, double initialGuess1, double tolerance = 1e-8, int maxIterations = 1000) Lets suppose my function is y = P0 / (1+P1*x), This is what I’m trying to do: var x = new double[] { 0, 100, 200 , 500}; var result = Fit.Curve( Thanks a lot in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello, According to the signature of that function, it is expecting a func delegate that defines your function and which takes as input three double parameters and returns a double output value. Checking the class reference (found here and screenshotted below), we can see that the inputs are meant to be p0, p1, and x in that order. Therefore, you can declare a Func type with the matching signature that performs the calculation defined by your function and then pass it to Curve.Fit(). In case you are unfamiliar with that feature of the C# language, you may do this using a lambda as I do in the code below. There are also a number of other ways such as defining a static function with the same signature and setting it to your func object, or simply defining the lambda right in the arguments of the Fit() method. The best method depends on the context of your code. Hope this helps.
-Mikhail |
Beta Was this translation helpful? Give feedback.
-
hello, thanks a lot for your detailed answer. It works very well now. |
Beta Was this translation helpful? Give feedback.
Hello,
According to the signature of that function, it is expecting a func delegate that defines your function and which takes as input three double parameters and returns a double output value. Checking the class reference (found here and screenshotted below), we can see that the inputs are meant to be p0, p1, and x in that order.
Therefore, you can declare a Func type with the matching signature that performs the calculation defined by your function and then pass it to Curve.Fit().
In case you are unfamiliar with that feature of the C# language, you may do this using a lambda as I do in the code below. There are also a number of other ways such as defining a static function with the sam…