Processing math: 100%
Ask Your Question
1

Cannot solve simple equations with trig functions?

asked 1 year ago

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(θ)=0 and
  • cos(θ)x=0

    to which the solutions are θ=nπ and x=cosθ.

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)
Preview: (hide)

Comments

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 1 year ago )

2 Answers

Sort by » oldest newest most voted
1

answered 1 year ago

Emmanuel Charpentier gravatar image

updated 1 year ago

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...).

Preview: (hide)
link

Comments

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 ( 1 year ago )
0

answered 1 year ago

achrzesz gravatar image

updated 1 year ago

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
Preview: (hide)
link

Comments

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

mwm gravatar imagemwm ( 1 year ago )

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 1 year ago

Seen: 649 times

Last updated: Jun 17 '23