MathGroup Archive 2010

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

Search the Archive

Re: Struggling with FindFit for a model which takes results from

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108242] Re: Struggling with FindFit for a model which takes results from
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 11 Mar 2010 06:38:20 -0500 (EST)
  • References: <hn5b2i$6q2$1@smc.vnet.net>

Hi Steven,

Your main problem seems to be the numerous syntax and logic errors in
your code:

1. p is undefined
2. you use numbers such as 1*^15 where you mean 1*10^15 (*missing 10
*)
3. missing bracket in insolutemolefxlist[t_] := {XinSalt[t],XinCPA1[t]
4. variables A and NinWater0 run together to form the new ANinWater0
6. equations using = instead of ==
7. FindFit statement without any parametrized model (volume is a
numerical function of t)

Cheers -- Sjoerd


On Mar 9, 1:22 pm, "Steven Mullen" <St... at 21cm.com> wrote:
> I have been struggling for a few days to figure out how to estimate param=
eters in coupled ODEs when the results from the ODEs are used as input for =
the model in FindFit.
>
> It is a relatively straightforward mass transfer problem, with 2 componen=
ts (NinWater and NinCPA1). These equations are designed to calculate the nu=
mber of moles of substances per time, but I have data for total volume, so =
I am evaluating an equation to calculate the volume from (moles*molar volum=
e).
>
> I have been trying to use FindFit (for a few days now) to estimate the pa=
rameters Lp and PCPA1 found in the ODEs to obtain a best fit to the volume =
data (with no luck).
>
> Here is the code I have so far.
>
> Lp := 2.64*^-27; (* mol^2/(min atm um^5)  *)
>
> Rad = 63; (* um *)
>
> A = 4 p Rad^2;  (* um^2*)
>
> R=0.08206; (* L atm /(mol K) *)
>
> T=286;
>
> minutes = 10;
>
> Vb = 0.19;
>
> Vbv = 4 /3 p Rad^3 * Vb  (* um^3 *)
>
> volconv = 1*^15  (* converts um^3 to L ; 1x10^15 um^3 per L *);
>
> molconv = 1*^6 (* converts moles to micromoles ; 1x10^6 umol per mol *)=
;
>
> PMVWater = 1.8*^7;  (* um^3/umol*)
>
> PMVDMSO = 7.13*^7;
>
> PCPA1 := 4.38*^-31;
>
> BSalt = 2.749 (*salt*);
>
> BCPA1 = 2.423 (*DMSO*);
>
> insecondvirialcoefficientlist={BSalt, BCPA1} ;
>
> exsecondvirialcoefficientlist={BSalt, BCPA1}
>
> NinWater0 = 4/3 p Rad^3 (1-Vb)/PMVWater
>
> NinSalt = 1.663*2.062*^-4 (* Salts; assumes 0.3 osmolal =8A 0.176 mol=
al *);
>
> NinCPA10 =0.1*^-100(* DMSO *);
>
> totmolin[t_] := (NinWater[t]+ NinSalt+ NinCPA1[t])
>
> XinSalt[t_] := NinSalt/totmolin[t]
>
> XinCPA1[t_] := NinCPA1[t]/totmolin[t]
>
> insolutemolefxlist[t_] := {XinSalt[t],XinCPA1[t]
>
> waterin[t_]:= Total[insolutemolefxlist[t]] (1+insecondvirialcoefficient=
list.insolutemolefxlist[t])
>
> NexWater = 0.056*^6 (* water *);
>
> NexSalt = 1.663*245;  (* assumes mole fraction of 0.003 *)
>
> NexCPA1 = 1694  (* DMSO *);
>
> totmolex = NexWater+ NexSalt+ NexCPA1;
>
> XexWater =NexWater/totmolex;
>
> XexSalt = NexSalt/totmolex;
>
> XexCPA1 = NexCPA1/totmolex;
>
> exsolutemolefxlist = {XexSalt,XexCPA1}
>
> waterex=Total[exsolutemolefxlist] * (1+exsecondvirialcoefficientlist.ex=
solutemolefxlist);
>
> V0=NinWater0 * PMVWater + Vbv + NinCPA10*PMVDMSO
>
> tref=T+273.16;
>
> volconv = 1*^15
>
> (* **********************************************************************=
********************************************* *)
>
> (* The 2 parameters I wish to find are Lp and PCPA1 here in the 2 ODEs *)
>
> solution = NDSolve[{NinWater'[t]=8A- Lp*volconv*molconv*A*R*T (watere=
x - waterin[t]),
>
>     NinCPA1'[t] =8A PCPA1*volconv*molconv*A*R*T (Log[XexCPA1]+
>
>        (0.5-BCPA1) XexWater (1-XexCPA1)-
>
>        (0.5-BSalt) XexWater XexSalt-
>
>        Log[(NinCPA1[t]/(NinWater[t]+NinSalt+NinCPA1[t]))]-
>
>        (0.5-BCPA1) (NinWater[t]/(NinWater[t]+NinSalt+NinCPA1[t]))=
 (1-(NinCPA1[t]/(NinWater[t]+NinSalt+NinCPA1[t])))+
>
>        (0.5-BSalt) (NinWater[t]/(NinWater[t]+NinSalt+NinCPA1[t]))=
 (NinSalt/(NinWater[t]+NinSalt+NinCPA1[t]))),
>
>    NinWater[0] =8ANinWater0, NinCPA1 [0] =8A NinCPA10},{NinWater,=
 NinCPA1}, {t,0,minutes}]
>
> (*   End of ODEs  *)
>
> (*Start of Curve Fitting *)
>
> Vtotal[t] := (Vbv + NinWater[t]*PMVWater + NinCPA1[t]*PMVDMSO) /.soluti=
on;
>
> (* So I am making this calculation using output from the ODEs to determin=
e the total volume, which includes a fixed volume (Vbv), the result from Ni=
nWater'[t] * molar volume of water, and likewise for the DMSO. The sum of a=
ll 3 components is the total volume, which is what I am trying to fit to th=
e data (see below).
>
> volume:=Evaluate[Vtotal[t]]
>
> Clear[Lp,PCPA1]  (* It seems that I have to clear these values here to =
make FindFit work to the extent it is working now)  *)
>
> data = {1.04739=D7106,1.02856=D7106,1.01005=D7106,991863.,974012.=
,956510.  ,939369.,922600.,906215.,890226.,874644.,859480.,844747.,830455=
.,816614.,803234.,790325.,777895.,765951.,754500.,743546.,733094.,
>
> 723147.,713704.,704765.,696328.,688389.,680941.,673978.,667489.,661465.,6=
55 892.,650759.,646048.,641746.,637835.,634299.,631118.,628276.,625754.,623=
534.,621597.,619927.,618505.,617316.,616342.,
>
> 615569.,614981.,614565.,614307.,614195.,614216.,614360.,614616.,614975.,6=
15427.,615965.,616581.,617268.,618019.,618827.,619689.,620598.,621549.,6225=
40.,623564.,624620.,625704.,626812.,627942.,
>
> 629092.,630259.,631441.,632636.,633842.,635059.,636284.,637516.,638754.,6=
39997.,641244.,642494.,643746.,644999.,646253.,647508.,648762.,650015.,6512=
67.,652517.,653765.,655011.,656254.,657494.,
>
> 658730.,659963.,661193.,662419.,663641.,664858.,666072.,667281.,668485.,6=
69686.,670881.,672072.,673258.,674440.,675617.,676789.,677956.,679118.,6802=
75.,681428.,682575.,683718.,684856.,685989.,
>
> 687117.,688240.,689359.,690472.,691581.,692685.,693784.,694879.,695969.,6=
97054.,698134.,699210.,700281.,701347.,702409.,703467.,704519.,705568.,7066=
12.,707651.,708686.,709717.,710743.,711765.,
>
> 712783.,713797.,714806.,715811.,716812.,717809.,718801.,719790.,720774.,7=
21755.,722731.,723704.,724673.,725637.,726598.,727555.,728508.,729458.,7304=
03.,731345.,732283.,733218.,734148.,735076.,
>
> 735999.,736919.,737835.,738748.,739657.,740563.,741466.,742365.,743260.,7=
44152.,745041.,745926.,746808.,747687.,748562.,749435.,750304.,751169.,7520=
32.,752891.,753748.,754601.,755451.,756298.,
>
> 77142.,757982.,758820.,759655.,760487.,761315.,762141.,762964.,763784.,76=
4601.,765415.,766227.,767035.,767841.,768644.,769444.,770241.,771036.,77182=
8. ,772617.,773403.,774187.,774968.,775746.,
>
> 776522.,777295.,778066.,778834.,779599.,780362.,781122.,781880.,782635.,7=
83388.,784138.,784885.,785631.,786374.,787114.,787852.,788587.,789321.,7900=
51.,790780.,791506.,792230.,792951.,793670.,
>
> 794387.,795102.,795814.,796524.,797232.,797937.,798640.,799341.,800040.,8=
00737.,801432.,802124.,802814.,803502.,804188.,804872.,805554.,806233.,8069=
11.,807586.,808260.,808931.,809600.,810268.,
>
> 810933.,811596.,812257.,812917.,813574.,814229.,814882.,815534.,816183.,8=
16831.,817476.,818120.,818762.,819402.,820040.,820676.,821310.,821943.,8225=
73.,823202.,823829.,824454.,825077.,825699.,
>
> 826319.,826937.,827553.,828167.,828780.,829391.,830000.,830607.,831213.,8=
31817.,832419.,833019.,833618.,834215.,834811.,835405.,835997.,836587.,8371=
76.,837763.,838349.,838933.,839515.,840096.,
>
> 840675.,841253.,841829.,842403.,842976.,843547.,844117.,844685.,845252.,8=
45817.,846380.,846942.,847503.,848061.,848619.,849175.,849729.,850282.,8508=
34.,851384.,851932.,852480.,853025.,853569.,
>
> 854112.,854653.,855193.,855732.,856269.,856804.,857339.,857872.,858403.,8=
58933.,859462.,859989.,860515.,861040.,861563.,862085.,862605.,863124.,8636=
42.,864159.,864674.,865188.,865700.,866211.,
>
> 866721.,867230.,867737.,868243.,868748.,869252.,869754.,870255.,870754.,8=
71253.,871750.,872246.,872741.,873234.,873726.,874217.,874707.,875196.,8756=
83.,876169.,876654.,877138.,877620.,878101.,
>
> 878582.,879061.,879538.,880015.,880490.,880965.,881438.,881910.,882381.,8=
82850.,883319.,883786.,884252.,884718.,885182.,885645.,886106.,886567.,8870=
26.,887485.,887942.,888399.,888854.,889308.,
>
> 889761.,890213.,890664.,891113.,891562.,892010.,892456.,892902.,893346.,8=
93790.,894232.,894673.,895114.,895553.,895991.,896428.,896864.,897300.,8977=
34.,898167.,898599.,899030.,899460.,899889.,
>
> 900318.,900745.,901171.,901596.,902020.,902443.,902866.,903287.,903707.,9=
04127.,904545.,904962.,905379.,905794.,906209.,906623.,907035.,907447.,9078=
58.,908268.,908677.,909085.,909492.,909898.,
>
> 910303.,910708.,911111.,911514.,911916.,912316.,912716.,913115.,913513.,9=
13910.,914307.,914702.,915097.,915490.,915883.,916275.,916666.,917056.,9174=
46.,917834.,918222.,918609.,918994.,919380.,
>
> 919764.,920147.,920530.,920911.,921292.,921672.,922052.,922430.,922807.,9=
23184.,923560.,923935.,924310.,924683.,925056.,925428.,925799.,926169.,9265=
38.,926907.,927275.,927642.,928008.,928374.,
>
> 928739.,929103.,929466.,929828.,930190.,930551.,930911.,931270.,931629.,9=
31986.,932343.,932700.,933055.,933410.,933764.,934117.,934470.,934822.,9351=
73.,935523.,935873.,936221.,936570.,936917.,
>
> 937264.,937610.,937955.,938299.,938643.,938986.,939328.,939670.,940011.,9=
40351.,940691.,941029.,941368.,941705.,942042.,942378.,942713.,943048.,9433=
82.,943715.,944047.,944379.,944710.,945041.,
>
> 945371.,945700.,946028.,946356.,946683.,947010.,947336.,947661.,947985.,9=
48309.,948632.,948955.,949277.,949598.,949919.,950239.,950558.,950876.,9511=
94.,951512.,951828.,952145.,952460.,952775.,
>
> 953089.,953402.,953715.,954028.,954339.,954650.,954961.,955271.,955580.,9=
55888.,956196.,956504.,956810.,957116.,957422.,957727.,958031.,958335.,9586=
38.,958940.,959242.,959544.,959844.,960144.,
>
> 960444.,960743.,961041.}
>
> (* These data represent one datapoint per second, for 10 minutes *)
>
> FindFit[data,volume,{{Lp,2.64*^-27},{PCPA1, 4.38*^-31}},t]
>
> (* When I evaluate FindFit, I get errors suggesting it is calculating bey=
ond 10 minutes. *)
>
> I have been trying to use the online help and past posts, but with no luc=
k.  If anyone can assist me I would be very grateful.
>
> Thank you.
>
> Steven F. Mullen Ph.D.



  • Prev by Date: Re: Why can't Mathematica tell when something is algebraically zero?
  • Next by Date: Re: Struggling with FindFit for a model which takes results from
  • Previous by thread: Re: plot solution derivative
  • Next by thread: Re: Re: Struggling with FindFit for a model which takes