# Are the following Mathematica codes correct for solving wave equation PDE? [duplicate]

I wanna solve the following PDE of wave equation using Mathematica.

$$u_{tt}=u_{xx}$$

$$00$$

Initial Conditions:

$$\begin{cases}u(x,0)=sin(x) \\u_{t}(x,0)=1\end{cases}$$

Boundary Conditions:

$$\begin{cases}u(0,t)+u_{x}(0,t)=1\\u(\pi,t)+u_{x}(\pi,t)=-1\end{cases}$$

• I know the boundary and initial conditions are inconsistent.

Are the following codes correct?

weqn = D[u[x, t], {t, 2}] == D[u[x, t], {x, 2}];
ic = {u[x, 0] == Sin[x],Derivative[0, 1][u][x, 0] == 1 };
bc = {u[0, t] + Derivative[1, 0][u][0, t] == 1,
u[Pi, t] + Derivative[1, 0][u][Pi, t] == -1};
sol = NDSolve[{weqn, ic, bc}, u, {x, 0, Pi}, {t, 0, 10}];


## marked as duplicate by xzczd differential-equations StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jan 19 at 6:38

• @zhk In this case, the solution happens to be correct, but it's no more than a coincidence. Just change u[0, t] + Derivative[1, 0][u][0, t] == 1 to e.g. u[0, t] + Derivative[1, 0][u][0, t] == 100, you'll find the solution doesn't change at all. To make the solution always correct, option like Method -> {"MethodOfLines", "DifferentiateBoundaryConditions" -> {True, "ScaleFactor" -> 20}} is necessary. One can also turn to FiniteElement method, of course. Please check the linked question for more information. – xzczd Jan 19 at 6:41
• Are the b.c. correct? Only for bc = {u[0, t] - Derivative[1, 0][u][0, t] == 1 , u[Pi, t] + Derivative[1, 0][u][Pi, t] == -1}; I get finite symmetric periodic solution! – Ulrich Neumann Jan 19 at 13:36