Creating gradient functions based on model parameters?

166 Views Asked by At

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!

1

There are 1 best solutions below

0
On

After some help from some coworkers and WolframAlpha I figured out how to create the gradient functions with respect to each of the parameters. As I suspected to do this I simply needed to derive my model function with respect to each parameter.

This produced very good results. I managed to cram all of this into the example software in the link in the OP.

enter image description here

Here are the gradient functions that I used (In psuedo code (In C# you cannot ^ doubles so there are a lot of Math.Pow making it almost unreadable)

gradient[0] = 1;
gradient[1] = g1 ^ 2 / (g1 ^ 2 + (x - x1) ^ 2);
gradient[2] = (2 * a1 * g1 * (x - x1)^2) / (g1^2 + (x - x1)^2)^2;
gradient[3] = (2 * a1 * g1^2 * (x-x1)) / (g1^2 + (x - x1)^2)^2;
gradient[4] = g2^2 / (g2^2 + (x - x2)^2);
gradient[5] = (2 * a2 * g2 * (x - x2)^2) / (g2^2 + (x - x2)^2)^2;
gradient[6] = (2 * a2 * g2^2 * (x-x2)) / (g2^ 2 + (x - x2)^2)^2;