 CoCalc Public FilesPublic / Wave-Equation-Test.sagews
Author: Hal Snyder
Views : 80
Description: Test expressions as solutions to classical 1-dimensional wave equation.
Compute Environment: Ubuntu 18.04 (Deprecated)

# Classical Wave Equations in SageMathCloud

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

$\frac{\partial^2\phi}{{\partial}z^2}-\frac{1}{v^2}\frac{\partial^2\phi}{{\partial}t^2}=0$

The variables are

• $z$ - the spatial coordinate
• $t$ - the time

In addition there is a parameter

• $v$ - the wave's velocity, a real-valued constant independent of $z$ and $t$

## 1. Solve for velocity.

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

Use $f$ for $\phi$ in the following discussion.

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

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

## 2. Test possible solutions in the original equation.

Define a second function, try_solution(f,v), that, given symbolic expressions for $f$ and $v$, 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 $f$ and $v$. 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)$ 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.rhs();v

$\left( z, t \right) \ {\mapsto} \ \sin\left(t + z\right)$
$\left[v = \left(-1\right), v = 1\right]$
$1$
# 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


$0$

### Example 2.

Is $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.rhs();v

$\left( z, t \right) \ {\mapsto} \ \sin\left(t z\right)$
$\left[v = -\frac{z}{t}, v = \frac{z}{t}\right]$
$\frac{z}{t}$

The expression for $v$ returned by solve_for_wave_velocity() for Example 2 depends on $z$ and $t$. That suggests this $f$ 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^{\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.rhs();v
try_solution(f,v)

$\left( z, t \right) \ {\mapsto} \ e^{\left(\pi z - 17 \, t\right)}$
$\left[v = -\frac{17}{\pi}, v = \frac{17}{\pi}\right]$
$\frac{17}{\pi}$
$0$

Since the solution for v is a constant and the result checks, Example 3 with $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

$\left( z, t \right) \ {\mapsto} \ 10 \, t^{2} - 48 \, t z + 90 \, z^{2}$
$\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.rhs();v
check = try_solution(f,v);check

$\left( z, t \right) \ {\mapsto} \ 10 \, t^{2} - 46 \, t z + 97 \, z^{2}$
$\left[v = -\frac{1}{97} \, \sqrt{97} \sqrt{10}, v = \frac{1}{97} \, \sqrt{97} \sqrt{10}\right]$
$\frac{1}{97} \, \sqrt{97} \sqrt{10}$
$0$

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

1/(v^2)

$\frac{97}{10}$
(f.diff(z) * 10).expand()
(f.diff(z,z) * 10).expand()

$\left( z, t \right) \ {\mapsto} \ -460 \, t + 1940 \, z$
$\left( z, t \right) \ {\mapsto} \ 1940$
(f.diff(t) * 97).expand()
(f.diff(t,t) * 97).expand()

$\left( z, t \right) \ {\mapsto} \ 1940 \, t - 4462 \, z$
$\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