Re: Fitting using a custom function

*To*: mathgroup at smc.vnet.net*Subject*: [mg97699] Re: Fitting using a custom function*From*: dh <dh at metrohm.com>*Date*: Thu, 19 Mar 2009 02:10:07 -0500 (EST)*References*: <gpqgdf$5u2$1@smc.vnet.net>

Hi, if I understand correctly, you have some 2 dim. array of experimental values (not y[x]), call it exp. Further you have a function fun that takes input values (your parameters) and returns an array similar to exp. You now want to optimize the function argument. This can be done by defining an (here least square) error function: err[p1_,p2_,..]:= Plus@@ Flatten@ (fun[p1,p2,..]-exp)^2 Now we need to find the minimum of err using e.g. NMinimize or FindMinimum. Here is an example: exp = Table[2 Sin[1.1 x] + 3 Sin[0.5 y], {x, 0, 10}, {y, 0, 10}]; fun[p1_, p2_, p3_, p4_] := Table[p2 Sin[p1 x] + p3 Sin[p4 y], {x, 0, 10}, {y, 0, 10}]; err[p1_, p2_, p3_, p4_] := Plus @@ Flatten[(fun[p1, p2, p3, p4] - exp)^2]; res = NMinimize[err[p1, p2, p3, p4], {p1, p2, p3, p4}] ListPlot3D[{fun[p1, p2, p3, p4] /. res[[2]], exp}] hope this helps, Daniel titos99 wrote: > My problem is pretty simple: > > I have a data file with experimental data that I would like two fit (x and y values). > In order to calculate the theoretical function I only can use a Finite difference method and iterate the calculus as a function of time. At the end of the calculation I get a list with theoretical x and y values. Now, since the calculation is made by using some parameters, I would like Mathematica to find the best set of fitting parameter that fit the experimental data. > Anybody knows how to do this kind of thing!? > The loop that I use for the calculation is itself a Module but I don't know if I can use this together with the Fit function... > > Any idea??? > > Many thanks. -R >