2
$\begingroup$

So, it's my first time using Mathematica. I was trying to get the solutions to some PDE and thought about Klein-Gordon equation: $$ \left(\frac{1}{c^2}\frac{\partial^2}{\partial t^2}-\frac{\partial^2}{\partial x^2}+\frac{m^2 c^2}{\hbar^2}\right)\psi(x,t)=0 $$ Then I wrote this code (where I renamed h=$\hbar$ and f=$\psi$ for simplicity) :

eqn = (1/c^2) D[f[t, x], {t, 2}] - D[f[t, x], {x, 2}] + ((m^2 * c^2)/(h^2))*f[t, x]
sol = DSolve[eqn == 0, f, {t, x}]

But didn't get the solution I expected. Is this code wrong? Or could I evaluate the solution in another way? Thanks for your help.

$\endgroup$
2
  • 1
    $\begingroup$ You also need to give appropriate boundary conditions to get exactly the solution you want, I guess? $\endgroup$
    – Hans Olo
    Commented Aug 26, 2023 at 21:27
  • 1
    $\begingroup$ To help answer your question, please explain the solution you expected. $\endgroup$
    – creidhne
    Commented Aug 26, 2023 at 21:49

1 Answer 1

5
$\begingroup$

without boundary conditions you can't really obtain solution. PDE's are different from ode's. The general solution depends on the boundary conditions because those determine the eigenvalues and eigenfunctions to use.

May be this explains why Mathematica did not solve it? Or may be it did not have templates for this pde.

But here is a workaround using separation of variables. But this solution contains unknown eigenvalue $\lambda$ (which if you had BC, you could determine it), then determine the eigenfunctions $X_n(x)$ and then obtain the general solution as series of solutions of $T_n X_n$ which you add up, then use the initial conditions to find the final unknown constants for the time part.

Quit[]
pde0  = 1/c^2*D[f[t,x],{t,2}]-D[f[t,x],{x,2}]+m^2*c^2/h^2*f[t,x]
pde   = pde0/.f->Function[{t,x},T[t]*X[x]]
Expand[pde/(T[t]*X[x])]==0

Mathematica graphics

So we have two ode's equal each others, one depends on $x$ and the other on $t$, hence they both must be equal some constant, say $\lambda^2$. So the two odes' are

ode1 = (c^2*m^2)/h^2 + T''[t]/(c^2*T[t]) == λ^2
ode2 = X''[x]/X[x] == λ^2

Mathematica graphics

Solve them:

 solX = DSolveValue[ode2,X[x],x,GeneratedParameters->C]
 solT = DSolveValue[ode1, T[t], t, GeneratedParameters -> K]
 sol  = solX*solT

Mathematica graphics

Verify the solution

 sol = f -> Function[{t, x}, Evaluate[sol]]
 pde0 /. sol // Simplify

 (* 0 *)

Maple 2023 solves this partially giving the solution as products of two functions which are solutions of two ode's (basically the same as above).

pde := 1/c^2* diff(f(t,x),t$2)-  diff(f(t,x),x$2) + m^2*c^2/h^2*f(t, x);
pdsolve(pde=0,f(t,x))

enter image description here

Update

The general solution depends on the boundary conditions because those determine the eigenvalues and eigenfunctions to use.

May be this explains why Mathematica did not solve it?

my suspicion was correct! When adding bc and ic, Mathematica is now able to solve it and give the expected series solution by separation of variables

pde=1/c^2*D[f[t,x],{t,2}]-D[f[t,x],{x,2}]+m^2*c^2/h^2*f[t,x]
bc={f[t,0]==1,(D[f[t,x],x]/.x->Pi)==0}
ic={f[0,x]==0,(D[f[t,x],t]/.t->0)==0}
sol:=DSolve[{pde == 0, bc, ic}, f[t, x], {t, x}]

Mathematica graphics

Next step will be to make a small animation showing the wave moving in time. I made up some numerical values for the unknowns in the pde and made up some BC and IC's.

enter image description here

Code for the above

ClearAll["Global`*"]
pde = 1/c^2*D[f[t, x], {t, 2}] - D[f[t, x], {x, 2}] + 
  m^2*c^2/h^2*f[t, x]
bc = {f[t, 0] == 1, (D[f[t, x], x] /. x -> Pi) == 0}
ic = {f[0, x] == 0, (D[f[t, x], t] /. t -> 0) == 0}
sol = DSolveValue[{pde == 0, bc, ic}, f[t, x], {t, x}];
sol = sol[[1, 1, 1]]
sol = sol /. {m -> 1, c -> 2, h -> 3, Infinity -> 20} (*make up some values*)    

Mathematica graphics

solActive = Activate[sol];

Manipulate[
 Grid[{{Row[{"time = ", t0}]},
   {Plot[solActive /. t -> t0, {x, 0, 5}, 
     PlotRange -> {Automatic, {-1, 2}}, GridLines -> Automatic, 
     GridLinesStyle -> LightGray, PlotStyle -> Red, 
     ImageSize -> 300]}}],
 {{t0, 0, "time"}, 0, 10, .1, Appearance -> "Labeled"}
 ]
$\endgroup$
1
  • $\begingroup$ Great answer, my +1! Just to add that as the Klein-Gordon equation is in fact a wave equation, the actual solution is a sum of the monochromatic ones given above :-) $\endgroup$
    – Hans Olo
    Commented Aug 27, 2023 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.