Skip to main content

Numerically solve the initial value problem for the 1-D wave equation


I want to solve the standard 1-dimensional wave equation $y_{xx}=y_{tt}$ using NDSolve (for $y(x,t)$) with the following conditions:


cond1 = Piecewise[{{1 - Abs[x - 1], Abs[x - 1] < 1}, {0, Abs[x - 1] > 1}}]

cond2 = Piecewise[{{1, 3 < x < 4}}, 0]

Where $y_{t}(x,0)=\mathrm{cond2}$ and $y(x,0)=\mathrm{cond1}$. I used the following code:


WaveEquation = D[y[x, t], {x, 2}] - D[y[x, t], {t, 2}] == 0;
cond1 = Piecewise[{{1 - Abs[x - 1], Abs[x - 1] < 1}, {0, Abs[x - 1] > 1}}];
cond2 = Piecewise[{{1, 3 < x < 4}}, 0];
sol1 = NDSolve[{WaveEquation, y[x, 0] == cond1,
Derivative[0, 1][y][x, 0] == cond2}, {y[x, t]}, {x, 0, 10}, {t, 0,
10}];


I would as well like to find the profiles when $t=0,1,2$. However, when I try to run the code for $\mathrm{sol1}$, I get the following error:


NDSolve::mxsst: Using maximum number of grid points 10000 allowed by the MaxPoints or MinStepSize options for independent variable x. >>
Warning: an insufficient number of boundary conditions have been specified for the direction of independent variable x. Artificial boundary effects may be present in the solution. >>

What would I be doing wrong in this case? I'm not sure on how to figure it out. The program has been running for a while, so I'm sure it crashed. As well, what would be a simple way to plot the time profiles for the solution to the PDE with the specified initial conditions? Thanks!



Answer



I've waited for this question for a long time :)





There actually exist 2 issues here:





  1. NDSolve can't handle unsmooth i.c. very well by default.




  2. NDSolve can't add proper artificial b.c. for the initial value problem (Cauchy problem) for the 1-dimensional wave equation.




The first issue is easy to solve: just make the spatial grid dense enough and fix its size to avoid NDSolve trying using too much points to handle those roughness. I guess finite element method in and after v10 can handle this issue in a even better way, but since I'm still in v9, I'd like not to explore this more.


What's really… big is the second issue. It's easy to solve the initial value problem for the 1-D wave equation analytically, we just need to use DSolve in and after v10.3 or d´Alembert's formula or do a Fourier transform, but when solving it numerically, we need to add proper artificial b.c., which NDSolve doesn't know how to. (NDSolve does add artificial b.c. when b.c. isn't enough, but as far as I know, it seldom works well, actually it's even unclear that what artificial b.c. is added, see this post for more information. )



Adding proper artificial b.c. for wave equation can be troublesome when the equation becomes more complicated (2D, 3D, nonlinear, etc.), but luckily, what you want to solve is just a simple 1D wave equation, then the corresponding artificial b.c. (usually called absorbing boundary condition) is quite simple:


{lb, rb} = {-10, 10};
(* absorbing boundary condition *)
abc = D[y[x, t], x] + direction D[y[x, t], t] == 0 /.
{{x -> lb, direction -> -1}, {x -> rb, direction -> 1}}

mol[n_Integer, o_: "Pseudospectral"] := {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> n,
"MinPoints" -> n, "DifferenceOrder" -> o}}


WaveEquation = D[y[x, t], {x, 2}] - D[y[x, t], {t, 2}] == 0;
cond1 = Piecewise[{{1 - Abs[x - 1], Abs[x - 1] < 1}, {0, Abs[x - 1] > 1}}];
cond2 = Piecewise[{{1, 3 < x < 4}}, 0];
ic = {y[x, 0] == cond1, Derivative[0, 1][y][x, 0] == cond2};

nsol = NDSolveValue[{WaveEquation, ic, abc}, y, {x, lb, rb}, {t, 0, 10},
Method -> mol[600, 4](* fix the spatial grid and make it dense enough *)]

Animate[Plot[nsol[x, t], {x, lb, rb}, PlotRange -> {0, 2}], {t, 0, 10}]


NDSolve still spits out eerri and eerr warning, but it's not a big deal in this case:


enter image description here





As shown by rewi, DSolve can solve the problem after v10.3. Here I just want to show how to solve it with Fourier transform (the ft function is from here):


teqn = ft[{WaveEquation, ic}, x, w] /. HoldPattern@FourierTransform[a_, __] :> a

tsol = y[x, t] /. First@DSolve[teqn, y[x, t], t]

asol[x_, t_] = InverseFourierTransform[tsol, w, x]

(* 1/4 ((2 + t - x) Sign[2 + t - x] + (4 + t - x) Sign[
4 + t - x] + (3 + t - x) Sign[-3 - t + x] +
2 (1 + t - x) Sign[-1 - t + x] + (-t + x) Sign[-t + x] - (-4 + t + x) Sign[-4 + t +
x] + (-3 + t + x) Sign[-3 + t + x] + (-2 + t + x) Sign[-2 + t + x] -
2 (-1 + t + x) Sign[-1 + t + x] + (t + x) Sign[t + x]) *)

Compared to the solution given by DSolve, this solution doesn't involve Integrate so it's more suitable for numeric evaluation.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...