Re: NDSolve and NonLinearFit/Regress
- To: mathgroup at smc.vnet.net
- Subject: [mg59470] Re: [mg59417] NDSolve and NonLinearFit/Regress
- From: Selwyn Hollis <sh2.7183 at earthlink.net>
- Date: Wed, 10 Aug 2005 02:57:29 -0400 (EDT)
- References: <200508090730.DAA19065@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Aug 9, 2005, at 3:30 AM, Max wrote: > Hi, > > I have 3 differential equations: > c'[t]=a[t]*b[t]*k1-c[t]*k2 > a'[t]=-a[t]*b[t]*k1+c[t]*k2 > b'[t]=-a[t]*b[t]*k1+c[t]*k2 > c[0]=0 > a[0]=10 > b[0]10 > > I have a set of data points that I need to make fit. I need > Mathematica to find values for k1 and k2 in such a way that it fits > my data points the best. Since I am pretty much a beginner, can > you please be specific on how to do it? > > This should help you get started. (* First define the solution of you diff eqns as a function of k1 and k2. *) soln[k1_, k2_] := ({c[#1], a[#1], b[#1]} & ) /. First[NDSolve[{c'[t] == a[t]*b[t]*k1 - c[t]*k2, a'[t] == (-a[t])*b[t]*k1 + c[t]*k2, b'[t] == (-a[t])*b[t]*k1 + c[t]*k2, c[0] == c0, a[0] == a0, b[0] == b0}, {c, a, b}, {t, 0, tmax}]] (* Let's assume these initial values.*) c0=0; a0=10; b0=5; (* Now, you can plot the solution for given k1, k2, and tmax. *) tmax = 1; Plot[Evaluate[soln[1, 1.5][t]], {t, 0, tmax}]; (* I'll assume a list of times and corresponding data points are given like so: *) times = {0, 0.1, 0.2, 0.7}; data = {{c0, a0, b0}, {3.5, 6, 2.1}, {4, 5.5, 1.1}, {4.4, 5, 1}} (* Now define the least-squares error. *) LSE[(k1_)?NumericQ, (k2_)?NumericQ] := Norm[Flatten[data - soln[k1, k2] /@ times]] (* Here's a plot of the least-squares error. *) Plot3D[LSE[x, y], {x, 0, 4}, {y, 0, 4}]; (* You can use FindMinimum or NMinimize to find the minimizer. *) FindMinimum[LSE[k1, k2], {{k1, 1}, {k2, 1}}] NMinimize[{LSE[k1, k2], k1 > 0, k2 > 0}, {k1, k2}] ----------------------- Selwyn Hollis http://www.appliedsymbols.com
- References:
- NDSolve and NonLinearFit/Regress
- From: Max <maksim@ucla.edu>
- NDSolve and NonLinearFit/Regress