CoCalc Shared FilesPublic / Wave-Equation-Test.sagewsOpen in CoCalc with one click!
Author: Hal Snyder
Views : 19
Description: Test expressions as solutions to classical 1-dimensional wave equation.

Classical Wave Equations in SageMathCloud

Suppose we want to find out whether a function ϕ(z,t)\phi(z,t) is a solution to the one-dimensional classical wave equation

2ϕz21v22ϕt2=0\frac{\partial^2\phi}{{\partial}z^2}-\frac{1}{v^2}\frac{\partial^2\phi}{{\partial}t^2}=0

The variables are

  • zz - the spatial coordinate
  • tt - the time

In addition there is a parameter

  • vv - the wave's velocity, a real-valued constant independent of zz and tt

This page offers a two-step process using Sage to answer the question.

1. Solve for velocity.

If Sage finds one or more solutions for real vv for the above differential equation, then we know ϕ\phi is a wave function. The value for vv must not depend on zz or tt. If Sage does not find a solution, we don't know for sure either way.

Use ff for ϕ\phi in the following discussion.

In Sage, 2fz2\frac{\partial^2f}{{\partial}z^2} is given by f.diff(z,z).

Start by defining one function of ff, solve_for_wave_velocity() that attempts to solve the general wave equation for vv.

2. Test possible solutions in the original equation.

Define a second function, try_solution(f,v), that, given symbolic expressions for ff and vv, allows us to see whether substituting these expressions in the wave equation gives a tautology, i.e. that the left-hand-side is identically zero for the given definitions of ff and vv. The value returned is a symbolic expression. Sometimes the result of substitution will need additional simplification to show that it is identically zero.

%typeset_mode True %var v def solve_for_wave_velocity(f): r""" Solve for velocity of wave equation. INPUT: - ``f`` -- function of z and t OUTPUT: List of solutions for velocity. If f is a solution to the wave equation, the expression(s) returned for velocity will be real and independent of z and t """ eqn = f.diff(z,z) - (1/v^2)*f.diff(t,t) == 0 return eqn.solve(v) def try_solution(f,v): r""" substitute v in the wave equation for f INPUT - ``f`` -- symbolic expression, a function of z and t - ``v`` -- symbolic expression for velocity OUTPUT Symbolic expression. If this expression can be simplified to zero v is a solution. """ ex = f.diff(z,z) - (1/v^2)*f.diff(t,t) return ex(z,t)

Example 1.

Is f(z,t)=sin(z+t)f(z,t)=sin(z+t) a solution to the wave equation?

# define f f(z,t) = sin(z+t);f # solve for expressions for v vvals = solve_for_wave_velocity(f);vvals # take the right-hand-side of the positive solution v = vvals[1].rhs();v
(z,t)  sin(t+z)\left( z, t \right) \ {\mapsto} \ \sin\left(t + z\right)
[v=(1),v=1]\left[v = \left(-1\right), v = 1\right]
11
# substitute the expressions for f and v in the wave equation # if the resulting expression, which is a function of z and t, is identically zero, # then f and v give a solution # so Example 1 gives a solution check = try_solution(f,v);check
00

Example 2.

Is f(z,t)=sin(zt)f(z,t)=sin(zt) a solution to the wave equation?

f(z,t) = sin(z*t);f vvals = solve_for_wave_velocity(f);vvals v = vvals[1].rhs();v
(z,t)  sin(tz)\left( z, t \right) \ {\mapsto} \ \sin\left(t z\right)
[v=zt,v=zt]\left[v = -\frac{z}{t}, v = \frac{z}{t}\right]
zt\frac{z}{t}

The expression for vv returned by solve_for_wave_velocity() for Example 2 depends on zz and tt. That suggests this ff is not a wave function. In this case, it is not helpful to check the result with try_solution(f,v).

Example 3.

Is f(z,t)=eπz17tf(z,t)=e^{\pi{z}-17t} a solution to the wave equation?

f(z,t) = exp(pi*z -17*t);f vvals = solve_for_wave_velocity(f);vvals v = vvals[1].rhs();v try_solution(f,v)
(z,t)  e(πz17t)\left( z, t \right) \ {\mapsto} \ e^{\left(\pi z - 17 \, t\right)}
[v=17π,v=17π]\left[v = -\frac{17}{\pi}, v = \frac{17}{\pi}\right]
17π\frac{17}{\pi}
00

Since the solution for v is a constant and the result checks, Example 3 with v=17πv=\frac{17}{\pi} is a solution to the wave equation.

Example 4. A surprise.

Start with a known wave function. Change one of the constants in an attempt to "break" the function so that it is no longer a solution. Run the tests and find out the change created a new wave function.

# known wave function f(z,t) = ((3*t-9*z)^2 + (t+3*z)^2).expand();f vvals = solve_for_wave_velocity(f);vvals
(z,t)  10t248tz+90z2\left( z, t \right) \ {\mapsto} \ 10 \, t^{2} - 48 \, t z + 90 \, z^{2}
[v=(13),v=(13)]\left[v = \left(-\frac{1}{3}\right), v = \left(\frac{1}{3}\right)\right]
# try to make a non-wave function from f by using different velocity in the two terms # note the change to coefficient of z in second term f(z,t) = ((3*t-9*z)^2 + (t+4*z)^2).expand();f vvals = solve_for_wave_velocity(f);vvals v = vvals[1].rhs();v check = try_solution(f,v);check
(z,t)  10t246tz+97z2\left( z, t \right) \ {\mapsto} \ 10 \, t^{2} - 46 \, t z + 97 \, z^{2}
[v=1979710,v=1979710]\left[v = -\frac{1}{97} \, \sqrt{97} \sqrt{10}, v = \frac{1}{97} \, \sqrt{97} \sqrt{10}\right]
1979710\frac{1}{97} \, \sqrt{97} \sqrt{10}
00

So the second definition in Example 4 is also a wave function. How did that happen? Here are a few calculations.

1/(v^2)
9710\frac{97}{10}
(f.diff(z) * 10).expand() (f.diff(z,z) * 10).expand()
(z,t)  460t+1940z\left( z, t \right) \ {\mapsto} \ -460 \, t + 1940 \, z
(z,t)  1940\left( z, t \right) \ {\mapsto} \ 1940
(f.diff(t) * 97).expand() (f.diff(t,t) * 97).expand()
(z,t)  1940t4462z\left( z, t \right) \ {\mapsto} \ 1940 \, t - 4462 \, z
(z,t)  1940\left( z, t \right) \ {\mapsto} \ 1940
%md ## References - See Week 1 content from the online course, [QMSE01 Quantum Mechanics for Scientists and Engineers](https://lagunita.stanford.edu/courses/Engineering/QMSE01./Autumn2015/about). - There is a matching textbook from Cambridge University Press, [Quantum Mechanics for Scientists and Engineers](http://www-ee.stanford.edu/~dabm/QMbook.html). - The general solution to the one-dimensional wave equation, in the form $\phi(z,t)=F(z+ct)+G(z-ct)$, is provided by [d´Alembert's formula](https://en.wikipedia.org/wiki/D%27Alembert%27s_formula).

References