![]() |
|
#1
|
|||
|
|||
|
Dear all,
Is it possible to send me an example for fitting of a non-linear equation to a matrix of data? Thanks |
|
#2
|
|||
|
|||
|
Example function fgauss for sandwitching to get the model
Quote:
Also this fg class below requires a little more debugging before use especially so for the accessing of vector/objects .Thanks Quote:
Last edited by kutta; 02-04-2009 at 05:08 AM. Reason: Example of algorithm included |
|
#3
|
|||
|
|||
|
Thanks Kutta,
I think that your code can be implemented for non-linear optimization of a matrix of data and I am trying to use it in my program. However, the code wrote is basically refers to Ed2 b/c of fgaus, gaussj, covsrt, mrqcof,mrqmin routines. Is it a way to use the new routines in 3rd Ed so that everything would be more consistent with nr3.h? Thanks again for your email. |
|
#4
|
|||
|
|||
|
Quote:
Code:
//
// Demonstration of use of Numerical Recipes Fitmrq.
//
// The original function is the sum of two Gaussian functions,
// and the data points are corrupted by noise.
//
// The Numerical Recipes distribution conveniently supplies a
// function that can be fed to Fitmrq to try to model data that
// consists of samples from sum of any number of Gaussian-type functions.
//
// I corrupt the data values with additive Gaussian noise.
//
// davekw7x
//
// Always include nr3.h first
#include "../code/nr3.h"
// For Normaldev
#include "../code/ran.h"
#include "../code/gamma.h"
#include "../code/deviates.h"
// For Fitmrq
#include "../code/gaussj.h"
#include "../code/fitmrq.h"
// The fgauss() function is in the file fit_examples.h
#include "../code/fit_examples.h"
//
// Simplify appearance of expressions with x*x by using an in-line function
//
inline Doub sqr(const Doub & x)
{
return x * x;
}
int main()
{
int i;
Doub noise = 0.1; // Standard deviation of generated noise
Normaldev ran(0, noise, 8654321); // Gaussian noise generator
//Normaldev ran(0, noise, time(0)); // Gaussian noise generator
Int num_points = 101;
// In order to use fgauss on K Gaussians, we need
// some vectors with size 3*K. So, for two Gaussians
// we have size = 6
//
// The two groups of three parameters contain amplitude, mean,
// and standard deviation of a Gaussian function that is
// used to generate the data points for testing.
//
// This is the actual array of parameters for the two Gaussian
// functions:
// [first group], [second group]
const Doub actual_d[] = { 5.0, 2.0, 3.0, 2.0, 5.0, 3.0 };
//
// This is the "guess" that we supply to fitmrq:
//
// Try it with a "pretty good" guess:
const Doub guess_d[] = { 4.8, 2.2, 2.8, 2.2, 4.8, 3.2 };
//
// Just for kicks, you might want to try it with a "not so pretty
// good" guess. Maybe you will get lucky; maybe not.
// The point is that Levenberg-Marquardt requires an initial
// guess of some kind.
//
//const Doub guess_d[] = { 2.0, 2.0, 2.0, 1.0, 1.0, 1.0 };
//
// The number of elements in the array is needed to initialize
// a VecDoub object from that array.
//
const Int ma = sizeof(actual_d) / sizeof(actual_d[0]);
VecDoub actual(ma, actual_d);
VecDoub guess(ma, guess_d);
VecDoub x(num_points);
VecDoub y(num_points);
VecDoub sd(num_points);
//
// Fill array y with values for sum of two gaussians, corrupted
// by additive gaussian noise.
//
// The sd array contains estimates of the standard deviations
// of the elements of y. We use a "measurement error" factor
// equal to the standard deviation of the noise distribution.
//
Doub xstart = 0.0;
Doub xmax = 10.0;
Doub deltax = xmax / (num_points - 1);
Doub xx;
for (i = 0, xx = xstart; i < num_points; i++, xx += deltax) {
x[i] = xx;
y[i] = ran.dev() + (actual[0]*exp(-sqr((xx - actual[1]) / actual[2])) +
actual[3]*exp(-sqr((xx - actual[4]) / actual[5])));
sd[i] = noise * y[i];
}
//
// Use the constructor to bind the data
//
Fitmrq mymrq(x, y, sd, guess, fgauss); // Use default tolerance 1.0e-3
//
// Now do the fit
//
mymrq.fit();
cout << scientific << setprecision(5);
cout << setw(10) << "Actual"
<< setw(13) << "Fitted"
<< " "
<< setw(13) << "Err. Est."
<< endl;
for (i = 0; i < actual.size(); i++) {
cout << setw(13) << actual[i]
<< setw(13) << mymrq.a[i]
<< " +- " << sqrt(mymrq.covar[i][i])
<< endl;
}
cout << endl;
cout << fixed;
cout << "Number of points = " << num_points
<< ", Chi-squared = " << mymrq.chisq << endl;
return 0;
}
Output on my Linux workstation (g++ version 4.1.2) Code:
Actual Fitted Err. Est.
5.00000e+00 5.52170e+00 +- 6.84019e-01
2.00000e+00 2.14463e+00 +- 4.97134e-01
3.00000e+00 2.93038e+00 +- 4.86329e-01
2.00000e+00 1.78320e+00 +- 1.43392e+00
5.00000e+00 5.46802e+00 +- 9.33546e-01
3.00000e+00 2.56607e+00 +- 2.90910e-01
Number of points = 101, Chi-squared = 309.83746
Dave Footnotes: 1. I think that chances of getting helpful responses with fewest iterations might be improved if requestors would give some kind of problem statement in the original post. (But there are no guarantees.) 2. I make no claims as to the suitability or usability of the procedure or of the appropriateness of any assumptions about noise or anything else; my intent is to show how to invoke the tools. Interpretation of results is up to the user. (In other words: Your Mileage May Vary.) Last edited by davekw7x; 02-04-2009 at 10:50 PM. |
|
#5
|
|||
|
|||
|
Thanks Dave,
Sure, I will add my program and the problem statement in my future questions. |
|
#6
|
|||
|
|||
|
Alternative on Matrix & its relatedequation
Quote:
Thanking you As Kutta(C.R.Muthukumar)
|
|
#7
|
|||
|
|||
|
Modelling of HomogeniusEquations via Matrices
Quote:
shown for ur perusal:- Thanking You As Kutta(C.R.Muthukumar)
|
|
#8
|
|||
|
|||
|
Hi Dave and Kutta,
The example that you have provided had the fgauss function as an example which is basically a single variable function. Now, my situation is different. My function has two variables, i.e., y=f(x,eta)=> x and eta matrices should be used as an input in the function that I should write in fit_examples.h. I am confused about implementing of fitmrq.h into my program. I have attached my codes along with the req'd txt files. Note that the experimental (simulated) values are A0Sim and the calculated values are A0Calc. Cordially, Amir |
|
#9
|
|||
|
|||
|
Multiple variables
I need help on multi-variable function as discussed above. Thanks
|
|
#10
|
|||
|
|||
|
NonLinearIntegral equations with Linear Equations
Quote:
Invoke the equation Z(x)=f(x) + {K(x,t)F(t,Z(t))dt} from a to b .Here the unknown function F(t,Z(t)) may be any function of Z(t) such as[Z(t)]^3 and tan[Z(t)]. In such a case nonlinear equations are produced first instead of linear equaitions.These equations can then be solved by a suitable iterative technique such as the Newton Method with good initiall approximation.This is the required method for multivariables However from the contextual point Pl find an good method (SOR)included that needs a bit of fine tuning to make it workable/executable Thanking You As Kutta(C.R.Muthukumar) ![]() Quote:
Last edited by kutta; 02-17-2009 at 03:57 AM. Reason: for proper spacing of text |
|
#11
|
|||
|
|||
|
variables to optimize in the program
In the program that I have provided, the desired variable to be optimized are ksij0 and ksij1. The objective function (to be minimized) as I mentioned before are (A0Sim-A0Calc).
|
|
#12
|
|||
|
|||
|
Solution by use of NormaEquations
Quote:
ksij0,ksij1,the ojectivefunction which are as similar to this Please do not get confused and a little bit of initiation will enable you to win for a concrete solution since this is from NR However if this does not lead you so ,please try the ones under NonLinear Models whose routine name is mrqmin(with many variables).method Thanking you As Kutta(C.R.Muthukumar) ![]() Quote:
|
|
#13
|
|||
|
|||
|
Thanks Kutta for your message. I will work on your solution but it is worth mentioning that I found MINPACK package somewhat easier relative to Numerical Recipes when it comes to optimization of multi-variable functions like the one I have enclosed. However, I always prefer to use NR for my programs and that's why I am asking Dave if he has easier solution. Thanks and warm wishes.
|
|
#14
|
|||
|
|||
|
Solutions of NonLinearEquations by Matrix Method
Quote:
Pl try the following for nonlinear ones of more variables. Thanking You As Kutta(C.R.Muthukumar) |
|
#15
|
|||
|
|||
|
NonLinearIntegral equations with Linear Equations
Quote:
be coordinating what has been eariler said for solving nonlinear ones via linear ones. Indeed this is programmed using Simpsons method as mentioned(PDE) .Hence please ref my old reply and start making the linear ones in sequencial, and then use the formula that has been given for NonLinear Equations which is ur present requirements of the suject at the first instant. For Example only Thanking You As Kutta(C.R.Muthukumar) ![]() Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|