# Cannot solve simple equations with trig functions?

I'm pretty new to Sage, so forgive if this is a silly question. I'm trying to solve some relatively simple, but nonlinear and coupled equations. I've found that the below, very simple 2-variable problem trips up Sage; is there no way of solving such equations?

The equations are

• $sin(\theta)=0$ and
• $cos(\theta)-x=0$

to which the solutions are $\theta=n \pi$ and $x=\cos\theta$.

Here's my code, which gives TypeError: unable to make sense of Maxima expression. As a sanity check, I put in one solution by hand (which does return 0==0, 0==0 as expected). Note that putting to_poly_solve = 'force') doesn't seem to matter.

x = var('x')
theta = var('theta')

e1 = sin(theta)
e2 = cos(theta)-x

equations = [e1==0, e2==0]
variables = [theta, x]

print('Equations:')
print(equations)
handSoln = {theta:0, x:1}
print('Hand-found solutions')
print(handSoln)
print('Evaluating at hand-found solutions')
print([eq.subs(handSoln) for eq in equations])

soln = solve(equations, variables) #, to_poly_solve = 'force')

print('Found solutions:')
print(soln)

edit retag close merge delete

( 2023-06-16 09:09:36 +0200 )edit

Sort by ยป oldest newest most voted

The system can be docoupled

var('th')
s=solve([sin(th)], [th], to_poly_solve='force')[0];s

th == pi*z1698


(z1698 is an arbitrary integer constant)

To obtain nicer output:

var('k')
assume(k,'integer')
z=s.rhs().variables()[0]
th==s.rhs().subs(z==k)

th == pi*k

x==cos(s.rhs().subs(z==k)).full_simplify()

x == (-1)^k

more

The solution is trivial, and not of interest. The question is why can't a CAS solve them?

( 2023-06-16 22:35:30 +0200 )edit

Possible one-liners :

sage: Sys=[sin(theta), cos(theta)-x]
sage: Vars=var("x, theta")
sage: solve(t:=Sys[1].subs(solve(Sys[0], Sys[0].variables(), to_poly_solve="force")), t.variables())
[[x == cos(pi*r7), z309005 == r7]]
sage: solve(t:=Sys[1].subs(solve(Sys[0], Sys[0].variables(), to_poly_solve="force", solution_dict=True)), t.variables(), solution_dict=True)
[{x: cos(pi*r8), z309048: r8}]


Mathematica does it cleanly, but uses boolean functions not (yet) in Sage :

sage: mathematica.Solve([u==0 for u in Sys], Vars)
{{x -> -1, theta -> ConditionalExpression[Pi + 2*Pi*C[1],
Element[C[1], Integers]]},
{x -> 1, theta -> ConditionalExpression[2*Pi*C[1], Element[C[1], Integers]]}}


Sympy misses the multiplicities of solutions :

sage: solve(Sys, Vars, algorithm="sympy")
[{theta: 0, x: 1}, {theta: pi, x: -1}]
sage: solve(Sys, Vars, algorithm="sympy", solution_dict=True)
[{theta: 0, x: 1}, {theta: pi, x: -1}]


HTH,

EDIT : See this question which reports a (more spectacuiar) problem with identical roots and a lengthy explanation...).

more

Thanks, but I guess the point is that I'm actually trying to solve a much more complicated system of equations (which also fails). The equations are generated, and the whole hope is to avoid having to inspect the equations to have them solved.

( 2023-06-16 22:34:16 +0200 )edit