4

I have a very large expression:

j - Sqrt[q^2 + qp^2 - 
   2 q qp Cos[\[Theta]]] - \[Sqrt](qp^2 + 
     1/2 (16 m5^2 + ma^2 + mp^2 - 
        Sqrt[(-(16 m5^2) - ma^2 - mp^2)^2 - 
         4 (ma^2 mp^2 - 16 m5^2 qp^2)])) == 0

where

\[Theta] = Pi/6; ma = 980; mp = 139;
 j = \[Sqrt](q^2 + 
    1/2 (16 m5^2 + ma^2 + mp^2 + 
       Sqrt[(-(16 m5^2) - ma^2 - mp^2)^2 - 
        4 (ma^2 mp^2 - 16 m5^2 q^2)])) 

And $qp$ is real and positive. I want to find qp as function $q>0$ and $m5>0$ (if it is imposible then function only $q>0$ and $m5=constant>0$). I atemted to solve this the next way:

Solve[ j - Sqrt[
     q^2 + qp^2 - 
      2 q qp Cos[\[Theta]]] - \[Sqrt](qp^2 + 
        1/2 (16 m5^2 + ma^2 + mp^2 - 
           Sqrt[(-(16 m5^2) - ma^2 - mp^2)^2 - 
            4 (ma^2 mp^2 - 16 m5^2 qp^2)])) == 0 && qp > 0 , qp, 
  Reals];

Mathematica hangs after two hour calculation. Where I am wrong? After colculation I want to plot function form: $z=q^2+m5^2$ (in reality this is more complicated)


Link to Wolfram Community version.

illuminato
  • 299
  • 2
  • 9
  • 2
    Mathematica's inability to find a closed-form solution for qp doesn't necessarily mean you did something wrong. Maybe there isn't one. You might need to substitute numerical values for q and m5 before solving for qp. – Simon Woods Apr 09 '17 at 18:50
  • Solve[expr, qp] returns in a few of seconds for me, if I leave the parameters as undefined symbols. – Michael E2 Apr 09 '17 at 19:05
  • @MichaelE2 Yes. But unfortunately it is too large answer for ploting this. It is possible create table (qp,p) for this? (m5 - constant and let = 100) – illuminato Apr 09 '17 at 19:34
  • @user2975438 See my answer. It seemed faster to show you than to try to describe it. Note the reason it seems not to plot for you is probably because of complex values. – Michael E2 Apr 09 '17 at 20:17

1 Answers1

6

It is feasible to solve the general equation with symbolic parameters and then substitute their numeric values.

eqn = j - Sqrt[q^2 + qp^2 - q qp Cos[θ]] -
      √(qp^2 + 1/2 (16 m5^2 + ma^2 + mp^2 - 
          Sqrt[(-(16 m5^2) - ma^2 - mp^2)^2 - 
            4 (ma^2 mp^2 - 16 m5^2 qp^2)])) == 0;

With[{gensol = Solve[eqn, qp]},
  Block[{θ = Pi/6, ma = 980, mp = 139, j},    (* subs vals when gensol is evaluated *)
   j = √(q^2 + 1/2 (16 m5^2 + ma^2 + mp^2 + 
          Sqrt[(-(16 m5^2) - ma^2 - mp^2)^2 - 
            4 (ma^2 mp^2 - 16 m5^2 q^2)]));
   sols = gensol
   ]];

Compiling the long expression is a good thing to do, if machine precision is sufficient. Note the function is complex in some parts. Even where the solution appears to be real, the imaginary parts can be great enough that Plot3D won't draw them. One can use Re, Im or Chop to plot the parts as desired. Chop will result in a plot of the real part only where the imaginary part is less than 10^-10 in magnitude.

There are four solutions. Below we plot the 4th.

qpC = Compile[{{q, _Complex}, {m5, _Complex}}, 
  Evaluate[qp /. sols[[4]]],                   (* index = 1, 2, 3, or 4 *)
  RuntimeOptions -> "EvaluateSymbolically" -> False]

Plot3D[Re@qpC[q, m5],
 {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic]

Mathematica graphics

Plot3D[Im@qpC[q, m5],
 {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic]

Mathematica graphics

Plot3D[Chop@qpC[q, m5],
 {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic]

Mathematica graphics

One can plot the expression without compiling, but it takes 104 sec. instead of just 0.14 sec.

Plot3D[Evaluate[Chop@qp /. sols[[4]]],
 {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic]

Mathematica graphics


Update

This is pretty nice. Optimizing the expression saves a lot of time and memory! And it's fast, much faster than Simplify, which ran longer than I would wait.

opt = Experimental`OptimizeExpression[qp /. sols[[4]]]; // AbsoluteTiming
(*  {0.015315, Null}  *)

ByteCount /@ {qp /. sols[[4]], opt}
(*  {2858680, 25912}  *)

Plotting it is not as fast as plotting the compiled version (3.7 sec. vs. 0.14 sec), but much faster than qp /. sols[[4]] (176 sec.).

Another advantage is one can evaluate with arbitrary precision. Higher precision makes a nicer plot, too.

Here's an example that implements the OP's desired constraint via a pattern restriction on a function. One problem is that the function in the constraint is sometimes complex-valued. This causes an error in the inequality. Hends I added a condition Im[qp] == 0. I would have used WorkingPrecision instead of SetPrecision; but somehow machine precision numbers got passed and numerical errors resulted that gave messages.

Block[{θ = Pi/6, f},
 f[q0_, m50_] := 
  Block[{q = SetPrecision[q0, 40], m5 = SetPrecision[m50, 40], qp}, 
   qp = First@opt;
   Re@qp /; Im@qp == 0 && q^2 + Re@qp^2 - q Re@qp Cos[θ] > 0];
 Plot3D[f[q, m5], {q, 0, 10000}, {m5, 0, 1000},
  MaxRecursion -> 3, AxesLabel -> Automatic]
 ]

Mathematica graphics

The higher setting for MaxRecursion bumps the plotting time up to 14.5 sec.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 1
    Thank you very much. May I ask you about add condition? It is imposible to put q^2 + qp^2 - q qp Cos[[Theta]]>0? – illuminato Apr 09 '17 at 21:42
  • @illuminates Do you mean ConditionalExpression? – Michael E2 Apr 09 '17 at 22:01
  • Yes. But if I write Plot3D[ConditionalExpression[Re@qpC[q, m5], q^2 + qp^2 - q qp Cos[\[Theta]] > 0], {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic] that it doesn't work. – illuminato Apr 10 '17 at 18:51
  • @illuminates Isn't the condition equivalent to Im[qp] == 0? Both your condition and the condition Im[qp] < 10^-10 give the same plots as the Chop one above. – Michael E2 Apr 11 '17 at 02:24
  • Yes, you are right. I have another question. Is it possible to create functions where is qpC? For example if I want to create function: 'z=qpC^2+qpC^3+m5' and after that to plot it: Plot3D[Re@z[q, m5], {q, 0, 10000}, {m5, 0, 1000}, AxesLabel -> Automatic], for some reason it doesn't plot . – illuminato Apr 11 '17 at 10:44
  • @illuminates If you add RuntimeOptions -> "EvaluateSymbolically" -> False, as I just did in the update, you can use z = qpC[q, m5]^2 + qpC[q, m5]^3 + m5. – Michael E2 Apr 11 '17 at 10:53
  • Nice! And could you be so kind to prompt how to plot 2D graph for you code? I attempt to write Plot[Re@z /. m5 = 50, {q, 0, 10000}] or Manipulate[Plot[Re@z, {q, 0, 10000}], {m5, 0, 2000}] but it does not work. I know that it is possible to do the next one: m5=0; Plot[Re@z, {q, 0, 10000}] but in that case variable m5 will be constant in overall notebook. – illuminato Apr 12 '17 at 13:47
  • @illuminates Try Plot[Re@z /. m5 -> 50, {q, 0, 10000}]. Manipulate is tougher. See this. – Michael E2 Apr 12 '17 at 22:36
  • if you don't mind, can you please tell about yours Update about restriction on a function. The point is that yours method works only for one solution. If I want to add two solutions Block[{[Theta] = Pi/3, f}, f[q0_, m50_] := Block[{q = SetPrecision[q0, 40], m5 = SetPrecision[m50, 40], qp}, qp = (First@opt2 + First@opt4); than it doesn't work. – illuminato Apr 13 '17 at 13:13
  • @illuminates I think the θ in my updated code is misleading, because θ is set to π/6 in the solutions sols already at the top. If you change that one to π/3 and recalculate, it should work. If you want to change θ often, you should think about making sols depend on θ by omitting it from the first Block that contains sols = gensol. But then you have to remember to set its value every time you use sols. – Michael E2 Apr 13 '17 at 14:09
  • No, I set π/3 in the solutions sols at the top. I think problem is in somewhere else. I attempt to write the next code: – illuminato Apr 13 '17 at 16:37
  • Block[{[Theta]1 = Pi/3, f2, f4}, f2[ki0_, M50_] := Block[{ki = SetPrecision[ki0, 40], M5 = SetPrecision[M50, 40], q2}, q2 = First@opt2; y12 /; Im@q2 == 0 && ki^2 + Re@q2^2 - ki Re@q2 Cos[[Theta]1] > 0]; f4[ki0_, M50_] := Block[{ki = SetPrecision[ki0, 40], M5 = SetPrecision[M50, 40], q4}, q4 = First@opt4; y14 /; Im@q4 == 0 && ki^2 + Re@q4^2 - ki Re@q4 Cos[[Theta]1] > 0]; Plot3D[f2[ki, M5] + f4[ki, M5], {ki, 0, 10000}, {M5, 0, 1000}, MaxRecursion -> 3, AxesLabel -> Automatic]] – illuminato Apr 13 '17 at 16:37
  • Where y12[q2] is qp for second solution (in previous designations) and y14[q4] is qp for fourth solution (in previous designations) – illuminato Apr 13 '17 at 16:46