ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 16 Jun 2023 22:35:30 +0200Cannot solve simple equations with trig functions?https://ask.sagemath.org/question/69190/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)
Fri, 16 Jun 2023 00:09:42 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/Comment by slelievre for <p>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? </p>
<p>The equations are </p>
<ul>
<li>$sin(\theta)=0$ and </li>
<li><p>$cos(\theta)-x=0$</p>
<p>to which the solutions are $\theta=n \pi$ and $x=\cos\theta$. </p></li>
</ul>
<p>Here's my code, which gives <code>TypeError: unable to make sense of Maxima expression</code>. As a sanity check, I put in one solution by hand (which does return 0==0, 0==0 as expected). Note that putting <code>to_poly_solve = 'force')</code> doesn't seem to matter. </p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69208#post-id-69208Welcome to Ask Sage! Thank you for your question.Fri, 16 Jun 2023 09:09:36 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69208#post-id-69208Answer by Emmanuel Charpentier for <p>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? </p>
<p>The equations are </p>
<ul>
<li>$sin(\theta)=0$ and </li>
<li><p>$cos(\theta)-x=0$</p>
<p>to which the solutions are $\theta=n \pi$ and $x=\cos\theta$. </p></li>
</ul>
<p>Here's my code, which gives <code>TypeError: unable to make sense of Maxima expression</code>. As a sanity check, I put in one solution by hand (which does return 0==0, 0==0 as expected). Note that putting <code>to_poly_solve = 'force')</code> doesn't seem to matter. </p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?answer=69220#post-id-69220Possible 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](https://ask.sagemath.org/question/69248/why-does-sagemath-92-give-me-an-error/?answer=69260#post-id-69260) which reports a (more spectacuiar) problem with identical roots and a lengthy explanation...).Fri, 16 Jun 2023 15:49:33 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?answer=69220#post-id-69220Comment by mwm for <p>Possible one-liners :</p>
<pre><code>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}]
</code></pre>
<p>Mathematica does it cleanly, but uses boolean functions not (yet) in Sage :</p>
<pre><code>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]]}}
</code></pre>
<p>Sympy misses the multiplicities of solutions :</p>
<pre><code>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}]
</code></pre>
<p>HTH,</p>
<p><strong>EDIT :</strong> See <a href="https://ask.sagemath.org/question/69248/why-does-sagemath-92-give-me-an-error/?answer=69260#post-id-69260">this question</a> which reports a (more spectacuiar) problem with identical roots and a lengthy explanation...).</p>
https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69233#post-id-69233Thanks, 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.Fri, 16 Jun 2023 22:34:16 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69233#post-id-69233Answer by achrzesz for <p>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? </p>
<p>The equations are </p>
<ul>
<li>$sin(\theta)=0$ and </li>
<li><p>$cos(\theta)-x=0$</p>
<p>to which the solutions are $\theta=n \pi$ and $x=\cos\theta$. </p></li>
</ul>
<p>Here's my code, which gives <code>TypeError: unable to make sense of Maxima expression</code>. As a sanity check, I put in one solution by hand (which does return 0==0, 0==0 as expected). Note that putting <code>to_poly_solve = 'force')</code> doesn't seem to matter. </p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?answer=69213#post-id-69213The 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)^kFri, 16 Jun 2023 11:11:08 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?answer=69213#post-id-69213Comment by mwm for <p>The system can be docoupled</p>
<pre><code>var('th')
s=solve([sin(th)], [th], to_poly_solve='force')[0];s
th == pi*z1698
</code></pre>
<p>(z1698 is an arbitrary integer constant)</p>
<p>To obtain nicer output:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69234#post-id-69234The solution is trivial, and not of interest. The question is why can't a CAS solve them?Fri, 16 Jun 2023 22:35:30 +0200https://ask.sagemath.org/question/69190/cannot-solve-simple-equations-with-trig-functions/?comment=69234#post-id-69234