# Solve third degree equation with functions as coefficients

I would need to solve a third degree equation like $$x^3 + a (\theta) x+ b$$ where the coefficient of the term of degree 1 depends on some angle $$\theta$$ (for example, $$a(\theta) = \operatorname{cotg}(\theta)$$). The question is: how can I write the command so that Mathematica recognizes the coefficient $$a(\theta)$$ as a functions and separates the various possible cases?

• Can you please include Mathematica code that you have tried?
– Syed
Commented Apr 15 at 9:26
• The code is Solve[x^3 + Tan[t]*x + 1 == 0, x]. The tangent is just an example Commented Apr 15 at 9:32
• Try: sol = Solve[x^3 + Cot[t]*x + 1 == 0, x]; Plot[Evaluate[x /. sol], {t, 0, 3}] Commented Apr 15 at 10:03

For the example you give there is a solution. However, for more general coefficients you may have to go numeric.

sol = Solve[x^3 + Tan[t]*x + 1 == 0, x]


There are three solutions

{{x -> -((2^(1/3) Tan[t])/(-27 + Sqrt[729 + 108 Tan[t]^3])^(
1/3)) + (-27 + Sqrt[729 + 108 Tan[t]^3])^(1/3)/(
3 2^(1/3))}, {x -> ((1 + I Sqrt[3]) Tan[t])/(
2^(2/3) (-27 + Sqrt[729 + 108 Tan[t]^3])^(
1/3)) - ((1 - I Sqrt[3]) (-27 + Sqrt[729 + 108 Tan[t]^3])^(
1/3))/(6 2^(1/3))}, {x -> ((1 - I Sqrt[3]) Tan[t])/(
2^(2/3) (-27 + Sqrt[729 + 108 Tan[t]^3])^(
1/3)) - ((1 + I Sqrt[3]) (-27 + Sqrt[729 + 108 Tan[t]^3])^(
1/3))/(6 2^(1/3))}}


The first tends to be real with the other two complex conjugate.

Plotting the first solution gives

Plot[Evaluate[x /. sol[[1]]], {t, -\[Pi], \[Pi]}]


Hope that helps.

If you'd accept a numeric solution, you could use ContourPlot:

ContourPlot[x^3 + Tan[t]*x + 1 == 0, {t, -10, 10}, {x, -10, 10}, FrameLabel -> {"t", "x"}, GridLines -> {Range[-10 π/4, 10 π/4, π], None}]


There seems to be some numerical instability, possibly due to the places where tangent blows up (as indicated by the vertical lines). For this reason, let's rewrite the equation as $$\cos(t)\,x^3 + \sin(t)\,x + \cos(t) = 0$$ and plot it again:

p1 = ContourPlot[Cos[t] x^3 + Sin[t]*x + Cos[t] == 0, {t, -10, 10}, {x, -10, 10}, FrameLabel -> {"t", "x"}, GridLines -> {Range[-10 π/4, 10 π/4, π], None}]


Then, as long as you eliminate the roots where tangent blows up (only those points that cross the $$x$$-axis, as far as I can tell here), you can directly extract the points from the plot, using

pts = Cases[Normal@p1, Line[a_] :> a, Infinity];


To see that we've isolated the points:

ListLinePlot@Cases[Normal@p1, Line[a_] :> a, Infinity]


It's periodic, of course, so we can restrict the range of $$t$$:

ContourPlot[Cos[t] x^3 + Sin[t]*x + Cos[t] == 0, {t, -π/2, π/2}, {x, -10, 10}, FrameLabel -> {"t", "x"}]


Here's a general solution:

sol2 = Block[{q = 1, p = a[t], phi, A},
A = 2 Sqrt[-p/3];
phi = ArcCos[(3 q)/(A  p)];
A*Cos@Table[(phi + 2 Pi  k)/3, {k, 0, 2}]
] // TrigExpand


Block[{a = Tan},
ReImPlot[sol2 // Evaluate, {t, 0, 2 Pi}]
]


Block[{a = Tan},
realneg = Piecewise[{{sol2[[2]], a[t] < 0}}, sol2[[3]]];
ReImPlot[realneg, {t, -Pi, 2 Pi}]
]