Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Programming question about how build a fitting function

  • To: mathgroup at
  • Subject: [mg28863] Programming question about how build a fitting function
  • From: "SANCHEZ DE LEON, Guillermo" <gsl at>
  • Date: Wed, 16 May 2001 03:28:10 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

(* 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 *) 

  Module[{k, A},Set@@@({k@@Take[#,2],Last[#]}&/@lst);
      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 *)

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

NewFunction[AcuteInput3[3,{{1,2,0.15},{1,3, a13},{2,1,a21},{2,0,0.15}


Guillermo Sanchez

  • Prev by Date: AW: Creating graph with only a few data points
  • Next by Date: Re: Problem overriding simple built-in functions[2]
  • Previous by thread: AW: Creating graph with only a few data points
  • Next by thread: Re: Problem overriding simple built-in functions[2]