Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
    
Take the function out of the brackets, i.e. c[x,t] without the curly braces. Your syntax as it stands is incorrect. –  blochwave Aug 29 at 12:09

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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