Ask Your Question
1

Solving Simultaneous Equations: Excluding Imaginary Numbers

asked 2015-11-30 21:42:54 +0100

NickS gravatar image

updated 2015-11-30 23:36:54 +0100

tmonteil gravatar image

Hello all,

I am working on a Calculus problem where you have to find the critical points of a function f(x,y,z) subject to a constraint function c(x,y,z) = constant. I managed to use Sagemath to output the answers, but it is including answers that have complex numbers. How can I exclude the answers with complex numbers?

Here's the code I have written:

var('x y z u p', domain='real')
f = x^6 + y^6 + z^6
c = x^2 + y^2 + z^2
c1 = c == 6
fx = f.diff(x)
fy = f.diff(y)
fz = f.diff(z)
cx = u*c.diff(x)
cy = u*c.diff(y)
cz = u*c.diff(z)
eq1 = fx - cx == 0
eq2 = fy - cy == 0
eq3 = fz - cz == 0
answers = solve([eq1,eq2,eq3,c1],x,y,z,u)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-11-30 23:33:43 +0100

tmonteil gravatar image

The assumption mechanism for symbolic expression is kind of broken, but you can select the real values as follows:

sage: [answer for answer in answers if all([value.rhs() in RDF for value in answer])]
[[x == -sqrt(6), y == 0, z == 0, u == 108],
 [x == sqrt(6), y == 0, z == 0, u == 108],
 [x == 0, y == -sqrt(6), z == 0, u == 108],
 [x == 0, y == sqrt(6), z == 0, u == 108],
 [x == -sqrt(3), y == -sqrt(3), z == 0, u == 27],
 [x == sqrt(3), y == sqrt(3), z == 0, u == 27],
 [x == -sqrt(3), y == sqrt(3), z == 0, u == 27],
 [x == sqrt(3), y == -sqrt(3), z == 0, u == 27],
 [x == 0, y == 0, z == -sqrt(6), u == 108],
 [x == 0, y == 0, z == sqrt(6), u == 108],
 [x == -sqrt(3), y == 0, z == -sqrt(3), u == 27],
 [x == sqrt(3), y == 0, z == sqrt(3), u == 27],
 [x == -sqrt(3), y == 0, z == sqrt(3), u == 27],
 [x == sqrt(3), y == 0, z == -sqrt(3), u == 27],
 [x == 0, y == -sqrt(3), z == sqrt(3), u == 27],
 [x == 0, y == sqrt(3), z == -sqrt(3), u == 27],
 [x == 0, y == -sqrt(3), z == -sqrt(3), u == 27],
 [x == 0, y == sqrt(3), z == sqrt(3), u == 27],
 [x == -sqrt(2), y == -sqrt(2), z == -sqrt(2), u == 12],
 [x == sqrt(2), y == sqrt(2), z == sqrt(2), u == 12],
 [x == -sqrt(2), y == -sqrt(2), z == sqrt(2), u == 12],
 [x == sqrt(2), y == sqrt(2), z == -sqrt(2), u == 12],
 [x == -sqrt(2), y == sqrt(2), z == -sqrt(2), u == 12],
 [x == sqrt(2), y == -sqrt(2), z == sqrt(2), u == 12],
 [x == -sqrt(2), y == sqrt(2), z == sqrt(2), u == 12],
 [x == sqrt(2), y == -sqrt(2), z == -sqrt(2), u == 12]]

Also, since you computation involves only polynomials, why not use them directly (here i work with real algebraic numbers) ?

sage: R.<x,y,z,u> = AA[]
sage: P1 = R(eq1.lhs())
sage: P2 = R(eq2.lhs())
sage: P3 = R(eq3.lhs())
sage: P4 = R(c1.lhs()-c1.rhs())
sage: I = R.ideal([P1,P2,P3,P4])
sage: I
Ideal (6*x^5 - 2*x*u, 6*y^5 - 2*y*u, 6*z^5 - 2*z*u, x^2 + y^2 + z^2 - 6) of Multivariate Polynomial Ring in x, y, z, u over Algebraic Real Field
sage: I.dimension()
sage: V = I.variety()
sage: V
[{x: -1.414213562373095?, z: -1.414213562373095?, u: 12, y: -1.414213562373095?},
 {x: 1.414213562373095?, z: -1.414213562373095?, u: 12, y: -1.414213562373095?},
 {x: -1.414213562373095?, z: -1.414213562373095?, u: 12, y: 1.414213562373095?},
 {x: 1.414213562373095?, z: -1.414213562373095?, u: 12, y: 1.414213562373095?},
 {x: -1.414213562373095?, z: 1.414213562373095?, u: 12, y: -1.414213562373095?},
 {x: 1.414213562373095?, z: 1.414213562373095?, u: 12, y: -1.414213562373095?},
 {x: -1.414213562373095?, z: 1.414213562373095?, u: 12, y: 1.414213562373095?},
 {x: 1.414213562373095?, z: 1.414213562373095?, u: 12, y: 1.414213562373095?},
 {x: 0, z: -1.732050807568878?, u: 27, y: -1 ...
(more)
edit flag offensive delete link more

Comments

Thank you for your help. Could you please explain what's happening in the following line?

"[answer for answer in answers if all([value.rhs() in RDF for value in answer])]"

If you have time to walk through the logic of each for, in, and if, I would really appreciate it. I have some experience programming, but I'm still a novice with Sage.

As far as polynomials go, all the keywords that you need to "set them up" correctly seems kind of daunting. What is the advantage to using polynomials? Actually, what does it mean to use polynomials instead of, err, normal math?

NickS gravatar imageNickS ( 2015-12-01 00:25:10 +0100 )edit

For explanation about list comprehension and the use of all(), you can have a look at https://docs.python.org/2/tutorial/da... and https://docs.python.org/2/library/fun... also rhs() stands for "right-hand side" of the symbolic equality.

tmonteil gravatar imagetmonteil ( 2015-12-02 17:56:14 +0100 )edit

A little more advanced amendment:

all( (value.rhs() in RDF) for value in answer)

It's a trick in python that passes an "iterator" to "all" rather than a fully constructed list. In principle it's a little more efficient because it can abandon computations as soon as it finds the first "false" value. Furthermore, in python2 it has the advantage that the variable value doesn't get clobbered in the global scope.

nbruin gravatar imagenbruin ( 2015-12-02 19:27:55 +0100 )edit

@NickS: as for your second question, the solve()function is much less reliable than the one relying on polynomials, see http://trac.sagemath.org/wiki/symboli...

tmonteil gravatar imagetmonteil ( 2015-12-03 13:31:08 +0100 )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

1 follower

Stats

Asked: 2015-11-30 21:38:04 +0100

Seen: 458 times

Last updated: Nov 30 '15