Ask Your Question
0

how to solve this equation for several variables (with sympy?) [closed]

asked 2014-09-10 05:30:53 -0600

Jannik gravatar image

updated 2014-09-10 10:29:40 -0600

Hi, I found some similar questions but i can´t solve my problem with these answers. I use sagemath since 2 days and want to investigate a equation in dependence for all variables for that i try to solve it for any variables:

var('r h s phi theta d')

s_v=vector([s*cos(theta),-s*sin(theta)])
h_v=vector([h*sin(phi),-h*cos(phi)])
r_v=vector([r*cos(phi),r*sin(phi)])

f_v=r_v+h_v-s_v

def f(r,h,s,phi,theta): return norm(f_v(r=r,h=h,s=s,phi=phi,theta=theta))


eq1(r,h,s,phi,theta)=f(r,h,s,phi,theta)-f(r,h,s,0,theta)

solve([eq1(r,h,s,phi,theta)==4],r)

so i want to solve this equation for r (and later for all other variables) but the solution is shitty like this:

[sqrt(abs(s*cos(theta) - r)^2 + abs(s*sin(theta) - h)^2) ==
sqrt(abs(-h*cos(phi) - r*sin(phi) + s*sin(theta))^2 + abs(-r*cos(phi) +
s*cos(theta) + h*sin(phi))^2) - 4]

so no r=...

as far as i understood does the solve command has problem with sqrt() so i found the sympy command in this forum and tried to use like this(for me its not possible to take the equation befor the sympy announcement so i wrote an other example by hand:

var('r h s phi theta d')

s_v=vector([s*cos(theta),-s*sin(theta)])
h_v=vector([h*sin(phi),-h*cos(phi)])
r_v=vector([r*cos(phi),r*sin(phi)])

f_v=r_v+h_v-s_v

def f(r,h,s,phi,theta): return norm(f_v(r=r,h=h,s=s,phi=phi,theta=theta))

eq1(r,h,s,phi,theta)=f(r,h,s,-phi,theta)-f(r,h,s,0,theta)


from sympy import *            
r,h,s,phi,theta,d=symbols('r h s phi theta d') 

eq1= sqrt(r^2+h^2+s^2+2*s*cos(theta)*(-r*cos(phi)-h*sin(phi))+2*s*sin(theta)*(r*sin(phi)-h*cos(phi))) 

from sympy.solvers import solve
solve(eq1, h)

But now i can´t variate the variables.

That is a very long question, it would be very nice if some of you make the afford and have a look at this. Thank you very much for this

very nice, thank you! how can i convert an sage expression to an sympy expression? i`d like to create the equation in sage and than solve it with sympy or is there a better way?

var('r h s phi theta d')

s_v=vector([s*cos(theta),-s*sin(theta)])
h_v=vector([h*sin(phi),-h*cos(phi)])
r_v=vector([r*cos(phi),r*sin(phi)])

f_v=r_v+h_v-s_v

def f(r,h,s,phi,theta): return norm(f_v(r=r,h=h,s=s,phi=phi,theta=theta))

from sympy import *   

r,h,s,phi,theta,d=symbols('r h s ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Jannik
close date 2014-09-15 03:05:00.069496

1 answer

Sort by » oldest newest most voted
0

answered 2014-09-10 09:15:36 -0600

updated 2014-09-10 10:43:01 -0600

You just need to convert the sympy solution back to a Sage expression:

sage: sol=solve(eq1, h)
sage: sol1=sol[0]._sage_()
sage: sol1
s*sin(phi + theta) - sqrt(2*s^2*cos(phi + theta)*sin(phi)*sin(theta) + s^2*sin(phi)^2 + s^2*sin(theta)^2 + 2*r*s*cos(phi + theta) - r^2 - s^2)

For converting a Sage expression to sympy use sympify:

sage: from sympy import sympify
sage: from sympy.solvers import solve
sage: solve(sympify(x^2-1),x)
[-1, 1]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-09-10 05:30:53 -0600

Seen: 348 times

Last updated: Sep 10 '14