Fitting complex function...
wolfwolf
10-31-2008, 04:52 AM
Hi there,
I am trying to fit a complex function to a set of data (xi, yi). My function looks something like:
f(x) = a0 + a1*x + a2*x*sin(a3*x + a4).
Could someone please provide advice on what is the most efficient (fast and accurate!) method to fit such function to my data?
Many thanks.
Wolf
kutta
11-11-2008, 05:24 AM
Hi there,
I am trying to fit a complex function to a set of data (xi, yi). My function looks something like:
f(x) = a0 + a1*x + a2*x*sin(a3*x + a4).
Could someone please provide advice on what is the most efficient (fast and accurate!) method to fit such function to my data?
Many thanks.
Wolf
Hello Comrade,
At the best I can offer a coded program in C++ that was originally designed to fit in a function other than the present one of yours though ur f(x) formula i just tried to put in the same place.Rest of it is upto you to energise( modify)the same coded stuff to derive what u want to expect from this function and their method declaration is also u ought to include.Thanking You
Bye for now
As
An Amateured Programmer .
(:)
#include <iostream>
#include <cmath>
using namespace std;
void maximum(double, double, double, double); // function prototype
double f(double); // function prototype
int main()
{
double delta; // step size
double a,b,a0,a1,a2,a3,a4; // left and right ends of the original interval
double epsilon; // convergence criterion
// obtain the input data
cout << "Enter the limits of the original search interval, a and b: ";
cin >> a >> b;
cout << "Enter a0,a1,a2,a3,a4:";
cin >> a0 >> a1 >> a2 >> a3 >> a4;
cout << "Enter the convergence criteria: ";
cin >> epsilon;
cout << "Enter the step size: ";
cin >> delta;
maximum(a, b, epsilon, delta);
cin.get();cin.ignore(); // this line is optional
return 0;
}
void maximum(double a, double b, double epsilon, double delta)
{
double x1, x2;
double f1, f2;
double max;
// echo back the passed input data
cout << "\nThe original search interval is from " << a << " to " << b << endl;
cout << "The convergence criterion is: interval < " << epsilon << endl;
cout << "The step size is " << delta << endl;
x1 = a;
max = x1;
while(delta >= epsilon && max < b)
{
x2 = x1 + delta;
f1 = f(x1);
f2 = f(x2);
if (f1 < f2)
{
x1 = x2;
max = x2;
}
else
{
delta = delta / 2;
max = x1;
}
}
if(max > b && max != b)
max = max - delta;
cout << "\nThe max was found at x = " << max << endl;
return;
}
// function to evaluate f(x)
double f(double x)
{
double a0,a1,a2,a3,a4=0.0;
//const double PI = 2*asin(1.0); // value of pi
const double DE = a0 + a1*x + a2*x*sin(a3*x + a4);
//return;
//return (exp(-x) - sin(0.5 * PI * x));
}
kutta
06-14-2009, 08:17 PM
Hi there,
I am trying to fit a complex function to a set of data (xi, yi). My function looks something like:
f(x) = a0 + a1*x + a2*x*sin(a3*x + a4).
Could someone please provide advice on what is the most efficient (fast and accurate!) method to fit such function to my data?
Many thanks.
Wolf
Hello NR-Comrade,
Withstanding your non-response to my first suggestion,I am
trying to invigorate the same stuff in an absolutey simpler way that I am sure you that you would reply with a feedback on it at the earliest.Also please note the you have to define/initialise your varibles viz a0,a1,a2,a3,a4 before executing the following C++ prg.
Thanking You
As
Kutta(C.R.Muthukumar)
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{ cout << " x f(x)\n\n";
cout << fixed;
for (int i=20; i<=40; i+=2)
{ double x = i*10.0;
cout << setw(3) << setprecision(1)
<< x << " "
<< setw(15) << setprecision(10)
<< a0 + a1*x + a2*x*sin(a3*x + a4) << endl;
}
return 0;
}
:)
Kutta, your code doesn't do anything even slightly like what the original poster asked for. It looks like it's trying to find the maximum of the given function with a fixed set of parameters, whereas Wolf is trying to find a set of parameters that makes the function fit the data well.
Wolf, you might try the Levenberg-Marquardt nonlinear least-squares fitter in NR. There are other implementations of the basic LM idea that allegedly work much better by using a more sophisticated approach to adjusting the lambda parameter, such as LMDER and NL2SOL (the ACM's "Algorithm 573"), both of which I think are freely and legally available online. (There might be some restrictions on what you're allowed to do with the code.)