4
$\begingroup$

I have diffusion equation with the initial/boundary conditions:

eq = D[c[x, t], t] - d * D[c[x, t], {x, 2}] == 0;
ibc = {c[x, 0] == If[x != 0, 0, 1], c[0, t] == 1, Derivative[1, 0][c][0.3, t] == 0};

where 'd' is the diffusion coefficient.

Also I have about 500 data points of the diffusion process (in 3D form, C(x,t)). I'd like to estimate the diffusion coefficient.

I've tried to use FindFit to estimate value of the 'd' in two ways: 1)

sol1 = ParametricNDSolve[{eq, ibc}, {c[x,t]}, {x, 0, 0.3}, {t, 0, 3.5}, {d}]
fit1 = FindFit[pts, sol1[d][c[x,t], d, {x,t}]]

2) (based on the Mathematica's Documentary Center -> FindFit)

model[d_?NumberQ] := (model[d] = First[c[x,t] /. NDSolve[{eq, ibc}, {c[x,t]},{x, 0, 0.3}, {t, 0, 3.5}]]

fit2 = FindFit[pts, model[d][c[x, t]], d, {x, t}]

In both cases I get an error:

FindFit::nrlnum: The function value {-0.437097+c[c[0.05812,0.116667]],-0.5016+c[c[0.05812,0.233333]],<<47>>,-0.862967+c[c[0.06838,2.4525]],<<548>>} is not a list of real numbers with dimensions {598} at {d} = {1.}. >>

I would appreciate any help in rebuilding the code.

Thanks!

$\endgroup$
  • $\begingroup$ Take the function out of the brackets, i.e. c[x,t] without the curly braces. Your syntax as it stands is incorrect. $\endgroup$ – dr.blochwave Aug 29 '14 at 12:09
5
$\begingroup$
(* Your equations *)
eq = D[c[x, t], t] - d*D[c[x, t], {x, 2}] == 0;
ibc = {c[x, 0] == KroneckerDelta[x, 0.], c[0, t] == 1,  Derivative[1, 0][c][0.3, t] == 0};

(* a sample data set with d->1/100 *)
sol = NDSolve[{eq /. d -> 1/2, ibc}, c, {x, 0, 0.3}, {t, 0, 3.5}]; 
pts = Flatten[Table[{x, t, c[x, t] /. sol[[1]]}, {x, 0, 0.3, .03}, {t, 0, 3.5, .35}], 1];

The plot:

Plot3D[c[x, t] /. sol, {x, 0, 0.3}, {t, 0, 3.5}, PlotRange -> All, 
        ColorFunction -> "SolarColors", PlotLegends -> Automatic]

Mathematica graphics

Now let's fit it.You'll need a good initial guess for the following to converge in a reasonable time:

model[d_?NumberQ] := First[c /. NDSolve[{eq, ibc}, c, {x, 0, 0.3}, {t, 0, 3.5}]] ; 
fit2 = FindFit[pts, model[d][x, t], {{d, .02}}, {x, t}]

(* {d -> 0.01} *)

Done

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.