Based on How approximation search works I would try this in C++:
// (global) input data
#define _n 100
double px[_n]; // x input points
double py[_n]; // y input points
// approximation
int ix;
double e;
approx aa,ab;
// min max step recursions ErrorOfSolutionVariable
for (aa.init(-100,+100.0,10.00,3,&e);!aa.done;aa.step())
for (ab.init(-0.1,+ 0.1, 0.01,3,&e);!ab.done;ab.step())
{
for (e=0.0,ix=0;ix<_n;ix++) // test all measured points (e is cumulative error)
{
e+=fabs(fabs(aa.a*cos(ab.a*px[ix]))-py[ix]);
}
}
// here aa.a,ab.a holds the result A,B coefficients
It uses my approx
class from the question linked above
- you need to set the
min,max
and step
ranges to match your datasets
- can increase accuracy by increasing the recursions number
- can improve performance if needed by
- using not all points for less accurate recursion layers
- increasing starting step (but if too big then it can invalidate result)
You should also add a plot of your input points and the output curve to see if you are close to solution. Without more info about the input points it is hard to be more specific. You can change the difference computation e
to match any needed approach this is just sum of abs differences (can use least squares or what ever ...)