Function Repository Resource:

TrigNSolve

Source Notebook

Solve a system of trigonometric or hyperbolic equations

Contributed by: Daniel Lichtblau

ResourceFunction["TrigNSolve"][eqns,vars]

numerically solves the trigonometric system eqns in vars.

Details and Options

ResourceFunction["TrigNSolve"] also handles hyperbolic functions in the input.
Input can be in the form of trigonometric or hyperbolic polynomials or equations.
ResourceFunction["TrigNSolve"] creates regular polyomials from its input, using a new set of internal variables. It uses NSolve to obtain intermediate solutions in these variables. It then post-processes using arc trigs and arc hyperbolics to solve for the original variables.
ResourceFunction["TrigNSolve"] takes Method and WorkingPrecision options, which it passes to NSolve.
Examples are taken from the Mathematica.StackExchange forum.
Because ResourceFunction["TrigNSolve"] ultimately creates polynomials, it cannot handle mixed variables that appear both in polynomial and in trig form (e.g. x^2+Cos[x]).

Examples

Basic Examples (2) 

Find real solutions for a system of five equations:

In[1]:=
sys = {1 - Cos[theta1] - 2/3 Sin[phi + Pi/6] == 0.227746,
   h + 2/3 Cos[phi + Pi/6] - Sin[theta1] == -0.714585,
   -1. - Cos[theta2] - 2/3 Sin[phi - Pi/6] == (3 Cos[psi2])/4,
   2/5 + h + 2/3 Cos[phi - \[Pi]/6] - Sin[theta2] == (3 Sin[psi2])/4, 0.635187 Cos[phi - \[Pi]/6 - psi2] + 2/3 Cos[1.78586 + phi] Sin[psi2] == 0};
solns = ResourceFunction["TrigNSolve"][
   sys, {h, theta1, theta2, phi, psi2}];
rsolns = Select[solns, FreeQ[#, Complex] &]
Out[3]=

Check that residuals are small:

In[4]:=
Map[Norm, MapApply[Subtract, sys] /. rsolns]
Out[4]=

Scope (4) 

A system can have variables appearing in trigs or as ordinary variables and can also have radical expressions:

In[5]:=
sys = {1/(4 Sin[x]) - (a Sin[x])/(Sqrt[1 + a^2 + 2 a Cos[x]])^3,
   r^3 - 3/a^3 + (2 (a + Cos[x]))/(Sqrt[1 + a^2 + 2 a Cos[x]])^3,
   r^3 - 3 + (1 + a Cos[x])/(Sqrt[1 + a^2 + 2 a Cos[x]])^3 + 1/(4 Sin[x])};
solns = ResourceFunction["TrigNSolve"][sys, {r, a, x}, Method -> Automatic];
Select[solns, FreeQ[#, Complex] &]
Out[6]=

Solve a mixed trig-hyperbolic system:

In[7]:=
eqns = {Cosh[1]*Cosh[r3] - Sinh[1]*Sinh[r3]*Cos[-a3] == 1/2*(Cosh[1]*Cosh[2] - Sinh[1]*Sinh[2]*Cos[-1]), 1/2*(Cosh[1]*Cosh[2] - Sinh[1]*Sinh[2]*Cos[-1]) == Cosh[2]*Cosh[r3] - Sinh[2]*Sinh[r3]*Cos[1 - a3]};
ResourceFunction["TrigNSolve"][eqns, {r3, a3}]
Out[8]=

Solve a system in trigs and ordinary variables:

In[9]:=
tsys = {Cos[ArcTan[1.1496/h]] == Cos[0.0591 Pi]*Cos[y]*Cos[0.44167 Pi - x] + Sin[0.0591 Pi]*Sin[y],
    Cos[ArcTan[1.501428/h]] == Cos[0.0591 Pi]*Cos[y]*Cos[0.4 Pi - x] + Sin[0.0591 Pi]*Sin[y], Cos[ArcTan[1.927918/h]] == Cos[0.0591 Pi]*Cos[y]*Cos[0.358333 Pi - x] + Sin[0.0591 Pi]*Sin[y]};
solns = ResourceFunction["TrigNSolve"][tsys, {h, x, y}];
rsolns = Select[solns, FreeQ[#, Complex] &]
Out[10]=

Numerically solve a difficult trigonometric system and extract real-valued solutions:

In[11]:=
dvalRules = {da -> 1.12, db -> 1.23, dc -> 0.84};
eqns = {2 a b/(a + b) Sin[(aA + aB)/2] == dc, 2 b c/(b + c) Cos[aA/2] == da, 2 c a (c + a) Cos[aB/2] == db, Sin[aA]/Sin[aA + aB] == da/dc, Sin[aB]/Sin[aA + aB] == db/dc} /. dvalRules /. {aA -> 2 aA, aB -> 2 aB};
solns = ResourceFunction["TrigNSolve"][eqns, {a, b, c}];
realSolns = Select[solns, FreeQ[#, Complex] &]
Out[12]=

Options (5) 

Method (5) 

Use the "Endomorphism" method from NSolve on a large trigonometric system:

In[13]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/3500c6ef-670a-42f2-bd37-0b993878a811"]
Out[14]=

Extract real-valued solutions:

In[15]:=
rsolsE = Select[tsolsE, FreeQ[#, Complex] &]
Out[15]=

Compare to the default MethodAutomatic timing:

In[16]:=
First[AbsoluteTiming[
  tsolsA = ResourceFunction["TrigNSolve"][tpolys, {a1, a3, b1, b2, c2, c3}]]]
Out[16]=

And to the "Monodromy" method:

In[17]:=
First[AbsoluteTiming[
  tsolsM = ResourceFunction["TrigNSolve"][tpolys, {a1, a3, b1, b2, c2, c3}, Method -> "Monodromy"]]]
Out[17]=

Timing results strongly suggest that "Homotopy" was used as the Automatic default for this example:

In[18]:=
First[AbsoluteTiming[
  tsolsM = ResourceFunction["TrigNSolve"][tpolys, {a1, a3, b1, b2, c2, c3}, Method -> "Homotopy"]]]
Out[18]=

Possible Issues (1) 

TrigNSolve requires that variables appearing in trig functions not also appear as polynomial variables:

In[19]:=
ResourceFunction[
 "TrigNSolve"][{x^2 + y - Cos[y], Sin[x] + y^2} == 0, {x, y}]
Out[19]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.1 – 20 June 2024
  • 1.0.0 – 17 June 2024

Related Resources

Author Notes

This was written in to fill a need as seen from several questions that have appeared on the Mathematica StackExchange forum over the years. A few were used in the examples above.

License Information