# Solving for a system of unknowns with Mathematica

I have the following code to solve a system of 3 equations for 3 unknowns:

k = Sqrt[k1^2 + k2^2]
m = {{-Sqrt[pi/2]*i*(k1/(2*k))*Exp[-k*h],
Exp[-k*h] + Sqrt[pi/2]*Kn*k*Exp[-k*h],
0}, {-Sqrt[pi/2]*i*(k2/2*k)*Exp[-k*h], 0,
Exp[-k*h] +
Sqrt[pi/2]*Kn*k*Exp[-k*h]}, {(1/2*Kn)*Exp[-k*r3] - (k/2*Kn)*r3*
Exp[-k*r3] + (k/2*Kn)*h*Exp[-k*r3] + (k1^2/2*Kn*k)*r3*
Exp[-k*r3] + (k2^2/2*Kn*k)*r3*Exp[-k*r3] - (k1^2/2*Kn*k)*h*
Exp[-k*r3] + (k2^2/2*Kn*k)*h*Exp[-k*r3], -i*k1*Exp[-k*r3], -i*k2*
Exp[-k*r3]}};


This creates the matrix on the left-hand side for the coefficients of x,y,z and on the right-hand side I create the vector of constant terms and try to solve.

 m.{x, y, z} == {i*(h/4*pi*Kn)*(k1/k)*Exp[-k*h] -  Sqrt[pi/2]*(h/2*pi)*i*k1*Exp[-k*h],
i*(h/4*pi*Kn)*(k2/k)*Exp[-k*h] - Sqrt[pi/2]*(h/2*pi)*i*k2*Exp[-k*h],0}
Solve[%, {x, y, z}];


However, when I try to run this Mathematica seems unable to evaluate it (or at least it is taking a very long time).

• In Mathematica yours pi is Pi with capital letter. Dec 27 '19 at 17:29
• Ah thanks, I was trying Maple first. With Maple I seem to get a solution instantly but the expression which it gives for A is very long so I am checking if I get the same with Mathematica.
– Tom
Dec 27 '19 at 17:32
• The semicolon at the end of the Solve command inhibits the output of the result. Dec 27 '19 at 17:56
• In Mathematica the imaginary unit is I (not ì) Dec 27 '19 at 18:02
• The semicolon is not in my original code for some reason, it just seems to run without ever generating a result.
– Tom
Dec 27 '19 at 18:07

Clear["Global*"]

k = Sqrt[k1^2 + k2^2];


Simplify as you go along

m = {{-Sqrt[Pi/2]*I*(k1/(2*k))*Exp[-k*h],
Exp[-k*h] + Sqrt[Pi/2]*Kn*k*Exp[-k*h], 0},
{-Sqrt[Pi/2]*I*(k2/2*k)*Exp[-k*h], 0,
Exp[-k*h] + Sqrt[Pi/2]*Kn*k*Exp[-k*h]},
{(1/2*Kn)*Exp[-k*r3] - (k/2*Kn)*r3*
Exp[-k*r3] + (k/2*Kn)*h*Exp[-k*r3] + (k1^2/2*Kn*k)*r3*
Exp[-k*r3] + (k2^2/2*Kn*k)*r3*Exp[-k*r3] - (k1^2/2*Kn*k)*h*
Exp[-k*r3] + (k2^2/2*Kn*k)*h*Exp[-k*r3], -I*k1*Exp[-k*r3],
-I*k2*Exp[-k*r3]}} // FullSimplify;

eqn = m.{x, y, z} == {I*(h/4*Pi*Kn)*(k1/k)*Exp[-k*h] -
Sqrt[Pi/2]*(h/2*Pi)*I*k1*Exp[-k*h],
I*(h/4*Pi*Kn)*(k2/k)*Exp[-k*h] - Sqrt[Pi/2]*(h/2*Pi)*I*k2*Exp[-k*h], 0} //
FullSimplify;

sol = Solve[eqn, {x, y, z}][[1]] // FullSimplify

(* {x -> (h (k1^2 + k2^2) π (-Kn +
Sqrt[k1^2 + k2^2] Sqrt[2 π]))/((k2^4 + k1^2 (1 + k2^2)) Sqrt[
2 π] -
h (-1 + k1^2 - k2^2) (k1^2 + k2^2) Kn (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) +
Kn (2 Sqrt[
k1^2 + k2^2] + (k1^2 + k2^2) (2 (-1 + k1^2 + k2^2) r3 +
Kn Sqrt[2 π] (1 + (-1 + k1^2 + k2^2) Sqrt[k1^2 + k2^2] r3)))),
y -> (I h k1 π (-Kn +
Sqrt[k1^2 + k2^2] Sqrt[2 π]) (-k2^2 (-1 + k1^2 + k2^2) Sqrt[
2 π] +
h (-1 + k1^2 - k2^2) (k1^2 + k2^2) Kn (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) -
Kn (2 Sqrt[
k1^2 + k2^2] + (k1^2 + k2^2) (2 (-1 + k1^2 + k2^2) r3 +
Kn Sqrt[2 π] (1 + (-1 + k1^2 + k2^2) Sqrt[k1^2 + k2^2]
r3)))))/(2 Sqrt[
k1^2 + k2^2] (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) ((k2^4 + k1^2 (1 + k2^2)) Sqrt[
2 π] -
h (-1 + k1^2 - k2^2) (k1^2 + k2^2) Kn (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) +
Kn (2 Sqrt[
k1^2 + k2^2] + (k1^2 + k2^2) (2 (-1 + k1^2 + k2^2) r3 +
Kn Sqrt[2 π] (1 + (-1 + k1^2 + k2^2) Sqrt[k1^2 + k2^2]
r3))))),
z -> -((I h k2 π (-Kn +
Sqrt[k1^2 + k2^2] Sqrt[2 π]) (2 Sqrt[k1^2 + k2^2] Kn -
k1^2 (-1 + k1^2 + k2^2) Sqrt[2 π] -
h (-1 + k1^2 - k2^2) (k1^2 + k2^2) Kn (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) + (k1^2 +
k2^2) Kn (2 (-1 + k1^2 + k2^2) r3 +
Kn Sqrt[2 π] (1 + (-1 + k1^2 + k2^2) Sqrt[k1^2 + k2^2]
r3))))/(2 Sqrt[
k1^2 + k2^2] (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) ((k2^4 + k1^2 (1 + k2^2)) Sqrt[
2 π] -
h (-1 + k1^2 - k2^2) (k1^2 + k2^2) Kn (2 +
Sqrt[k1^2 + k2^2] Kn Sqrt[2 π]) +
Kn (2 Sqrt[
k1^2 + k2^2] + (k1^2 + k2^2) (2 (-1 + k1^2 + k2^2) r3 +
Kn Sqrt[2 π] (1 + (-1 + k1^2 + k2^2) Sqrt[k1^2 + k2^2]
r3))))))} *)


Verifying,

eqn /. sol // Simplify

(* True *)

• Version 12.0.0 for Mac OS X (macOS 10.15.2) on my laptop (MacBook Pro (Retina, 13-inch, Mid 2014); 3 GHz Dual-Core Intel Core i7; 16 GB 1600 MHz DDR3) using AbsoluteTiming: defining m with FullSimplify takes 0.483898 sec; FullSimplify for the equation takes 6.39681 sec; solving the equation and FullSimplify the result takes 40.422 sec; and verifying the solution with Simplify takes 0.129644 sec. The 40.422 sec is slow but not "forever". Dec 28 '19 at 20:12
• I noticed as well that if I set the right-hand side to 0 by writing eqn = m.{x, y, z} == {0, 0, 0} // FullSimplify; that it gives the solution as x=0, y=0,z=0, this can't be right?
– Tom
Dec 28 '19 at 20:15
• Evaluate m.{x, y, z} /. {x -> 0, y -> 0, z -> 0} and you will get {0, 0, 0}` Dec 28 '19 at 20:19