Ask Your Question

Cannot solve simple equations with trig functions?

asked 2023-06-16 00:09:42 +0200

mwm gravatar image

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]

handSoln = {theta:0, x:1}
print('Hand-found solutions')
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:')
edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question.

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

2 Answers

Sort by ยป oldest newest most voted

answered 2023-06-16 15:49:33 +0200

Emmanuel Charpentier gravatar image

updated 2023-06-17 22:05:14 +0200

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}]


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

edit flag offensive delete link 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.

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

answered 2023-06-16 11:11:08 +0200

achrzesz gravatar image

updated 2023-06-16 14:35:44 +0200

The system can be docoupled

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

th == pi*z1698

(z1698 is an arbitrary integer constant)

To obtain nicer output:


th == pi*k


x == (-1)^k
edit flag offensive delete link more


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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2023-06-16 00:09:42 +0200

Seen: 258 times

Last updated: Jun 17 '23