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.
this is the result using Boole
Thank you!
DirichletCondition
. $\endgroup$f[x]
that computes the correct values would be fine, if used with an IC in the formu[0, x] == f[x]
(à la Bill Watt's, Alex Trounev's, or kglr's solutions), becauseNDSolve
computes these values in the initialProcessEquations[]
phase to generate an IC vector (over the spatial grid) that starts the time-integration in the method of lines. $\endgroup$