1
$\begingroup$

I am trying to solve a PDE using the NDsolve with initial and boundary conditions,

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 10,
    u[0, x] == 2,
    u[t, 0] == 1,
    u[t, 2] == 1
    },
    u,
    {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]

it worked. But when I trying to assign a initial condition: u[0,x]=2 when 0.5<=x<=1, u[0,x]=1 elsewhere in [0,2] by means of If

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 10,
    If[0.5 <= x <= 1, u[0, x] == 2, u[0, x] == 1],
    u[t, 0] == 1,
    u[t, 2] == 1
    },
    u,
    {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]

the system returned me

NDSolve::deqn: Equation or list of equations expected instead of If[0.5<=x<=1,u[0,x]==2,u[0,x]==1] in the first argument {(u^(1,0))[t,x]==10,If[0.5<=x<=1,u[0,x]==2,u[0,x]==1],u[t,0]==1,u[t,2]==1}.

I know something is wrong with my initial conditions expression, But how I assign the initial conditions with an if condition. Or a better way to deal with it?

Thank you for your time!

UPDATE:

I followed the advice of @bbgodfrey by using DirichletCondition. it worked.

NDSolve[{D[u[t, x], t] - D[ D[u[t, x], x]] + D[u[t, x], x] == 0,
        DirichletCondition[u[t, x] == 2, 0.5 <= x <= 1],
        DirichletCondition[u[t, x] == 1, {2 >= x > 1, 0 <= x < 0.5}],
    u[t, 0] == 1,
    u[t, 2] == 1
    },
    u,
    {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]

But comparing with the results of @kglr, the results are different. this is the results using DirichletCondition, which also with an alert

NDSolve::femcscd: The PDE is convection dominated and the result may not be stable. Adding artificial diffusion may help.

enter image description here

this is the result using Boole

enter image description here

Thank you!

$\endgroup$
3
  • 1
    $\begingroup$ Check the documentation for DirichletCondition. $\endgroup$
    – bbgodfrey
    Commented Nov 29, 2018 at 6:03
  • $\begingroup$ Thank you @bbgodfrey, I have tried the DirichletCondition and it works! $\endgroup$
    – Rick
    Commented Nov 29, 2018 at 6:16
  • $\begingroup$ Any f[x] that computes the correct values would be fine, if used with an IC in the form u[0, x] == f[x] (à la Bill Watt's, Alex Trounev's, or kglr's solutions), because NDSolve computes these values in the initial ProcessEquations[] phase to generate an IC vector (over the spatial grid) that starts the time-integration in the method of lines. $\endgroup$
    – Michael E2
    Commented Nov 30, 2018 at 15:39

3 Answers 3

3
$\begingroup$

I like UnitStep.

NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10, 
   u[0, x] == 1 + UnitStep[x - 0.5] - UnitStep[x - 1], u[t, 0] == 1, 
   u[t, 2] == 1}, u[t, x], {t, 0, 5}, {x, 0, 2}, 
  MaxStepSize -> 0.01] // Flatten

u[t_, x_] = u[t, x] /. %

Plot3D[u[t, x], {t, 0, 5}, {x, 0, 2}]

enter image description here

Plot[u[0, x], {x, 0, 2}, PlotRange -> All]

enter image description here

Piecewise probably works too.

$\endgroup$
3
$\begingroup$

The third opinion, a simple modification of the author's code immediately leads to a solution. Note the solution near t=0

s = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10, 
   u[0, x] == If[0.5 <= x <= 1, 2, 1], u[t, 0] == 1, u[t, 2] == 1}, 
  u, {t, 0, 5}, {x, 0, 2}]

{Plot3D[u[t, x] /. s, {t, 0, 5}, {x, 0, 2}, PlotRange -> All, 
  Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}], 
 Plot3D[u[t, x] /. s, {t, 0, 1}, {x, 0, 2}, PlotRange -> All, 
  Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}], 
 Plot3D[u[t, x] /. s, {t, 0, .05}, {x, 0, 2}, PlotRange -> All, 
  Mesh -> None, ColorFunction -> Hue, AxesLabel -> {"t", "x", ""}], 
 Plot[Evaluate[Table[u[t, x] /. s, {t, 0, .1, .01}]], {x, 0, 2}, 
  AxesLabel -> {"x", "u"}]}

fig1 We check the solution at t->= 0, and see that everything is not smooth there. We need a special numerical method to solve this problem. To eliminate the oscillations at t-> 0, add the option

s = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10, 
   u[0, x] == If[0.5 <= x <= 1, 2, 1], u[t, 0] == 1, u[t, 2] == 1}, 
  u, {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01]

fig2

$\endgroup$
2
  • $\begingroup$ Thank you so much! $\endgroup$
    – Rick
    Commented Dec 6, 2018 at 8:02
  • $\begingroup$ @Rick, you're welcome! $\endgroup$ Commented Dec 6, 2018 at 15:30
2
$\begingroup$
sol = NDSolve[{D[u[t, x], t] - D[D[u[t, x], x]] + D[u[t, x], x] == 10,
      u[0, x] == 1 + Boole[0.5 <= x <= 1], u[t, 0] == 1, 
     u[t, 2] == 1}, u, {t, 0, 5}, {x, 0, 2}, MaxStepSize -> 0.01][[1]];

{u[0, .75], u[0, .1 .5]} /. sol

{2., 1.}

Plot3D[Evaluate[u[t, x] /. sol], {t, 0, 5}, {x, 0, 2}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you so much! $\endgroup$
    – Rick
    Commented Nov 29, 2018 at 6:15
  • $\begingroup$ @Rick, my pleasure. $\endgroup$
    – kglr
    Commented Nov 29, 2018 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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