0
$\begingroup$

This question already has an answer here:

Consider these code, which solve the same equation using DSolve and NDSolve, why do they give different answer? I'm using version 11.0 on Windows 8.1.

eq = D[u[x, t], t] == D[u[x, t], x, x];
icsbc = {u[x, 0] == 1, u[0, t] == 0, u[1, t] == 0};

numer1 = NDSolve[{eq, icsbc}, u, {x, 0, 1}, {t, 0, 1}]

numer2 = NDSolve[{eq, icsbc}, u, {x, 0, 1}, {t, 0, 1}, 
Method -> {"MethodOfLines", 
"SpatialDiscretization" -> "FiniteElement"}]

numer3 = NDSolve[{eq, icsbc}, u, {x, 0, 1}, {t, 0, 1}, 
Method -> {"MethodOfLines", 
"SpatialDiscretization" -> {"TensorProductGrid", 
  "MinPoints" -> 200}}]

symbol = DSolve[{eq, icsbc}, u[x, t], {x, t}]

symbol = {{u[x, t] -> Inactive[Sum][-(( 2 (-1 + (-1)^K[1]) E^(-\[Pi]^2 t K[1]^2) Sin[\[Pi] x K[1]])/(\[Pi] K[1])), {K[1], 1, \[Infinity]}]}}

Plot[{Evaluate[u[x, 1/2] /. numer1], Evaluate[u[x, 1/2] /. numer2], 
Evaluate[u[x, 1/2] /. numer3], 
Evaluate[(u[x, t] /. symbol[[1]] /. t -> 1/2 /. {Infinity -> 10} // 
 Activate)]}, {x, 0, 1}, 
PlotLegends -> {"numeric1", "numeric2", "numeric3", "symbolic"}, 
PlotStyle -> {{Green, Dashing[{0.1, 0.1}], Thin}, {Blue, 
Dashing[{0.05, 0.05}], Thin}, {Yellow, Dashing[{0.01, 0.01}], 
Thin}, {Black, Thickness[0.02]}}]

enter image description here

Another example:

L = 2;
eq = D[u[x, t], t] == D[u[x, t], x, x];
opts = Method -> {"MethodOfLines", 
"SpatialDiscretization" -> {"FiniteElement", 
  "MeshOptions" -> {"MaxCellMeasure" -> 0.001}}};

sol1 = NDSolveValue[{eq, u[x, 0] == x, u[0, t] == 1, u[2, t] == 1}, 
u, {x, 0, L}, {t, 0, L}, opts];

sol2 = u[x, t] /. 
First@DSolve[{eq, u[x, 0] == x, u[0, t] == 1, u[2, t] == 1}, 
u[x, t], {x, t}]

(* 1 + Inactive[Sum][-((
2 (1 + (-1)^K[1]) E^(-(1/4) \[Pi]^2 t K[1]^2)
 Sin[1/2 \[Pi] x K[1]])/(\[Pi] K[1])), {K[1], 1, \[Infinity]}] *)

T = 1/10;
Plot[{Evaluate[sol1[x, T]], 
Evaluate[sol2 /. {Infinity -> 100} /. t -> T // Activate]}, {x, 0, 
L}, PlotLegends -> {"numeric solution", "symbolic solution"}]

enter image description here

$\endgroup$

marked as duplicate by Mariusz Iwaniuk, Community Dec 13 '16 at 9:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Didn't you get a NDSolve::ibcinc: Warning: boundary and initial conditions are inconsistent. error? Your DSolve code returns unevaluated for me. $\endgroup$ – march Dec 12 '16 at 18:41
  • 1
    $\begingroup$ Anyway, I actually suspect that the numerical solutions are wrong, given your choice of boundary conditions. The solution should decay to zero very quickly, and yours aren't. As a slight kluge to match the BC's try, icsbc = {u[x, 0] == UnitStep[x - 0.001] UnitStep[0.999 - x], u[0, t] == 0, u[1, t] == 0}; instead, evaluate the numerical solutions, and plot a time-sequence of plots of the temperature profile. You'll see it decay to zero very quickly. $\endgroup$ – march Dec 12 '16 at 18:46
  • $\begingroup$ With numer2 solution I'm don't get any error messages $\endgroup$ – Mariusz Iwaniuk Dec 12 '16 at 18:47
  • 1
    $\begingroup$ Which suggests that Mathematica is making some assumptions behind the scenes to try and reconcile the inconsistent boundary and initial conditions. I would suggest thinking carefully about the fact that your BC's and IC's are inconsistent, and see if you can re-work your problem. $\endgroup$ – march Dec 12 '16 at 18:51
  • $\begingroup$ Add {"MethodOfLines", "DifferentiateBoundaryConditions" -> {True, "ScaleFactor" -> 100}} as the method of NDSolve will resolve the problem. $\endgroup$ – xzczd Dec 13 '16 at 3:16