Programming question about how build a fitting function
- To: mathgroup at smc.vnet.net
- Subject: [mg28863] Programming question about how build a fitting function
- From: "SANCHEZ DE LEON, Guillermo" <gsl at fab.enusa.es>
- Date: Wed, 16 May 2001 03:28:10 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
(* Hi, gurus. I have an beautiful problem. This Math function gives the solution of eq: X(t) = Xo Exp[-A t], where Xo is the matriz with initial conditions and A is the matrix of the coefficients, and X(t): {x1(t),....,xn(t)} represent the retention in differents compartments *) AcuteInput3[n_,lst_,{opts1__}]:= Module[{k, A},Set@@@({k@@Take[#,2],Last[#]}&/@lst); k[i_,j_]:=0; A =DiagonalMatrix[Table[-Sum[k[i,j],{j,0,n}],{i,1,n}]]+ Table[k[j,i],{i,1,n},{j,1,n}];MatrixExp[A t].{opts1}]//Expand; (*Here is an example of solution*) {x1[t_], x2[t_]}=AcuteInput3[2,{{1,2,0.1}, {2,1,0.3},{2,0,0.05}},{1,0.5}] (* out[]: \!\({\(-0.14372630957871807`\)\ *\^\(\(-0.4386000936329382`\)\ \ t\) + 1.1437263095787182`\ *\^\(\(-0.011399906367061716`\)\ \ t\), 0.162219139602902`\ *\^\(\(-0.4386000936329382`\)\ t\) + 0.33778086039709804`\ *\^\(\(-0.011399906367061716`\)\ \ t\)}\) *) (*But often we have the inverse problem. We know the experimental values of some compartment [xi(t)] and we want fit some coefficientts (one or several). Example: I know the the experimental values of {{t ,x2}} *) list2 ={{0,0.5},{10,0.30},{20,0.26},{30,0.24},{40,0.21},{50,0.19},{60, 0.17},{70,0.15},{80,0.135},{90,0.12},{100,0.11 }}; (*a12 and a21 are the coeff. to be fitting with list2. Here is a way *) sseX[{a12_?NumberQ, a21_?NumberQ},list_ ]:= Block[{q2}, q2[tiemp_]= AcuteInput3[2,{{1,2,a12}, {2,1,a21},{2,0,0.05}},{1,0.5}][[2]]/.t* tiemp;Plus@@Apply[(q2[#1]-#2)^2&,list,{1}] ]; FindMinimum[sseX[{a12,a21},list2],{a12,0,0.5},{a21,1,5}, MaxIterations*50] (* Here is the solution *) {0.0000420101,{a12*0.0810962,a21*0.243153}} My question: I wish build a package and I need join the funtions sseX and FindMinimum[sseX]. In tother words: I want build a pattern that make automatic and flexible the previous conputation. Some thing like this: NewFunction[AcuteInput3[..][[j]],{{a12, min,max}, {a21, min,max},..., {aij, min, max}}, list] (*j is the compartment where we have the experimental values, in the previous example j=2*) The function should be work with this example list3 ={{0,0.50},{2,0.38},{4,0.25},{6,0.16},{8,0.10},{10,0.06},{12,0.04 },{14,0.02},{16,0.01},{18,0.008},{20,0.005}}; NewFunction[AcuteInput3[3,{{1,2,0.15},{1,3, a13},{2,1,a21},{2,0,0.15} },{1,0.5,0}][[2]],{a13,0,0.5},{a21,1,5},list3] Thanks Guillermo Sanchez