# DSolve with piecewise coefficeints wrong, why?

sorry if this is a duplicate (I am pretty sure) but I was not able to find the question. I am playing around with DSolve with variable coefficients. In the documentation it is said, that DSolve can handle piecewise defined coefficients, see screenshot below.

Now I am comparing DSolve with NDSolve (with the FEM package) and I get the following results

(*Domain*)
xmin = 0;
xmax = 10;
reg = ImplicitRegion[xmin <= x <= xmax, {x}];
(*Field equation*)
feq = D[A[x]*sig[x], x] + n[x];
sig[x_] := Em[x]*eps[x];
eps[x_] := D[u[x], x];
(*Paremeters*)
Em[x_] :=
Piecewise[{{100,xmin <= x < (xmax - xmin)/3}, {1, (xmax - xmin)/3 <= x <= xmax}}];
A[x_] := 7;
(*Inhomogeneity*)
n[x_] := 3*x;
(*Analytical solution*)
conds = {u[xmin] == 0, u[xmax] == 1};
uds = DSolveValue[{feq == 0, conds}, u, {x, xmin, xmax}];
(*Fem solution*)
Needs["NDSolveFEM"]
conds = {DirichletCondition[u[x] == 0, x == xmin],DirichletCondition[u[x] == 1, x == xmax]};
ufem = NDSolveValue[{feq == 0, conds}, u, Element[x, reg]];
(*Plot*)
Plot[{uds[x], ufem[x]}, {x, xmin, xmax}, PlotLegends -> "Expressions"]


And the following plot

The results of uds are wrong, while ufem is correct. Already for n=0 I get the plot

where uds is wrong.

Question: do you see where my error in DSolve is? Is DSolve not able to handle piecewise defined coefficients? Thank you!

Maybe I misunderstood something but I don't see any difficulty with DSolve and NDSolve. The results are identical (version 8)

The ODE is solved explicitly

yd[x_] = y[x] /.
DSolve[y'[x] + Clip[x]^2 y[x] == 0 && y[0] == 1, y[x], x][[1]]


$\begin{array}{ll} \{ & \begin{array}{ll} e^{-\frac{2}{3}-x} & x\leq -1 \\ e^{-\frac{x^3}{3}} & -1<x\leq 1 \\ e^{\frac{2}{3}-x} & \text{True} \\ \end{array} \\ \end{array}$

NDSolve gives

yn[x_] = y[x] /.
NDSolve[y'[x] + Clip[x]^2 y[x] == 0 && y[0] == 1,
y[x], {x, -2, 2}][[1]]


$\text{InterpolatingFunction}[\{\{-2.,2.\}\},<>][x]$

Plotting both functions in one plot (with a small difference of 0.1 added to see both curves) shows the identity

Plot[{yd[x], 0.1 + yn[x]}, {x, -2, 2}]
(* 150603_plot _(n)dsolve_clipx.jpg *)


The same goes for other values of y[0], e.g. -1.

Also, the example case of a piecewise forcing function shows agreement between DSolve and NDSolve.

EDIT #1 Your example of a piecewise function

Your equation is solved by DSolve but not by NDSolve

xmin = 0;
xmax = 10;

Em[x_] :=
Piecewise[{{100,
xmin <= x < (xmax - xmin)/3}, {1, (xmax - xmin)/3 <= x <= xmax}}];

ud[x_] = Assuming[0 < x < 10,
u[x] /. DSolve[{7 D[u'[x] Em[x], x] + 3 x == 0, u[0] == 0,
u[10] == 1}, u[x], x][[1]]]


$\begin{array}{ll} \{ & \begin{array}{ll} \frac{568 x}{105}-\frac{x^3}{1400} & x\leq \frac{10}{3} \\ -\frac{110}{21}+\frac{233 x}{30}-\frac{x^3}{14} & \text{True} \\ \end{array} \\ \end{array}$

Plot[ud[x], {x, 0, 10}]
(* 150604_Plot _ud.jpg *)


This is ok, but NDSolve[] gives the error message with respect to the derivative:

Assuming[0 < x < 10,
NDSolve[{7 D[u'[x] Em[x], x] + 3 x == 0, u[0] == 0, u[10] == 1},
u[x], {x, 0, 10}][[1]]]


NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.. >>

$\left\{3 x+7 \left(\left( \begin{array}{ll} \{ & \begin{array}{ll} 0 & x<0\left\|0<x<\frac{10}{3}\right\|\frac{10}{3}<x<10\|x>10 \\ \text{Indeterminate} & \text{True} \\ \end{array} \\ \end{array} \right) u'[x]+\left( \begin{array}{ll} \{ & \begin{array}{ll} 100 & 0\leq x<\frac{10}{3} \\ 1 & \frac{10}{3}\leq x\leq 10 \\ 0 & \text{True} \\ \end{array} \\ \end{array} \right) u''[x]\right)==0,u[0]==0,u[10]==1\right\}$

\$Version

(* Out[245]= "8.0 for Microsoft Windows (64-bit) (October 7, 2011)" *)
`
• But why is my uds solution wrong? The case n=0 can be solved easily by hand and you will get the solution of the ufem and not what is returned by DSolve. Until now I cannot find my error. – Mauricio Fernández Jun 3 '15 at 13:03
• @ Mauricio Lobos : It seems to be the derivative of the function Em[x] which causes the difficulty in NDSolve. – Dr. Wolfgang Hintze Jun 3 '15 at 23:16
• I dont think NDSolve has a problem, it can handle self-defined piecewise functions on finite domains, DSolve cannot, at least for my example. – Mauricio Fernández Jun 4 '15 at 5:58
• @ Mauricio Lobos : my EDIT #1 shows you in detail what I said before. – Dr. Wolfgang Hintze Jun 4 '15 at 6:59
• I dont agree with the solution of DSolve, which apparently assumes continuity and differentiability at the point at which the parameters change. But maybe that's exactly the difference, in the fem solution only considers continuity but not necessarily differentiability, the conditions at that kind of points are others. – Mauricio Fernández Jun 5 '15 at 10:18