I am using a software library (Math.Net) to try to fit two Lorentzians to a curve. I have found some example software which shows the fitting out a few various types of curves (Line, Parabola, Power Function, and a Sum of Trigonometric functions). I would like to modify a model of these curves to represent my two Lorentzians.
The model that represents the Sum of Trig Funcs uses the model equation
y = a * cos(b * x) + b * sin(a * x)
It also has a gradient function for the parameters a and b as (in C# code)
public override void GetGradient(double x, Vector<double> parameters, ref Vector<double> gradient)
{
gradient[0] = (Math.Cos(parameters[1] * x) + parameters[1] * Math.Cos(parameters[0] * x));
gradient[1] = (-parameters[0] * Math.Sin(parameters[1] * x) * x + Math.Sin(parameters[0] * x));
}
Where gradient[0] is the gradient value for parameter a at x and gradient[1] is the gradient value for parameter b at x. Also where parameters[0] is the current value of a and parameters[1] is the current value of b.
What I would like to do is to produce these gradient functions for my equation model function
y = b + a1 * g1^2 / ((x - x1)^2 + g1^2) + a2 * g2^2 / ((x - x2)^2 + g2^2)
Where my parameters are b, a1, g1, x1, a2, g2, x2. I am not the moth mathematically inclined person there is but from what I can tell all the gradient functions are the derivative of the model function with respect to the parameter in question, is this correct?
Link to page containing the code I am trying to modify (source download at bottom): Linear and Non-Linear Least Squares with Math.Net
EDIT: Working on the derivations now. -- Done! See answer below!
