Ask Your Question
0

How to pick out the largest root of an equation?

asked 2015-01-20 05:22:48 +0100

Phoenix gravatar image

updated 2015-01-20 06:34:59 +0100

I tried the following but it didn't work,

p = x^2 - 7ax + 5; a=5; m = max((p == 0).solve([x]))

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-01-20 16:35:25 +0100

tmonteil gravatar image

updated 2015-01-20 16:38:10 +0100

First, here is a classical way to get solutions of you equation:

sage: a = 5
sage: p = x^2 - 7*a*x + 5
sage: p.solve(x)
[x == -1/2*sqrt(1205) + 35/2, x == 1/2*sqrt(1205) + 35/2]

So, you have a list of solutions. Each solution is of the form x == -1/2*sqrt(1205) + 35/2 which is a symbolic expression. You can get the right hand side of such an equality with the rhs() method:

sage: [s.rhs() for s in p.solve(x)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]

Then, you can take the maximal element of this list:

sage: max([s.rhs() for s in p.solve(x)])
1/2*sqrt(1205) + 35/2

Alternatively, instead of getting solutions as symbolic expressions, you can get them as Python dictionaries:

sage: p.solve(x, solution_dict=True)
[{x: -1/2*sqrt(1205) + 35/2}, {x: 1/2*sqrt(1205) + 35/2}]

So, you can get each solution by looking at the x values:

sage: [s[x] for s in p.solve(x, solution_dict=True)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]

Then, as before, you can take the maximal element of this list:

sage: max([s[x] for s in p.solve(x, solution_dict=True)])
1/2*sqrt(1205) + 35/2
edit flag offensive delete link more

Comments

If some of the roots turned out to be complex then would "max" throw up some kind of an error message which as an user I can catch ?

phoenix gravatar imagephoenix ( 2015-01-20 21:53:22 +0100 )edit

No, since Sage gives an ordering between complex numbers, you will get the maximum for this ordering:

sage: max([1+I, 2*I])
I + 1

A possibility is to assume that x is real:

sage: p = x^2 + 1
sage: p.solve(x)
[x == -I, x == I]
sage: assume(x, 'real')
sage: p.solve(x)
[]
tmonteil gravatar imagetmonteil ( 2015-01-21 09:20:03 +0100 )edit
0

answered 2015-01-20 10:29:00 +0100

rws gravatar image

updated 2015-07-15 15:49:19 +0100

Use subs (wrong solution):

sage: p = x^2 - 7*a*x + 5; p.subs(a=5)
x^2 - 35*x + 5
sage: max(p.solve([x]))
x == -1/2*sqrt(1205) + 35/2
edit flag offensive delete link more

Comments

But this not the largest root...

Phoenix gravatar imagePhoenix ( 2015-01-20 14:50:29 +0100 )edit

The problem with your approach is that you take the maximum of symbolic expressions, which are equalities (not values of solutions), so the ordering of those is not very clear.

tmonteil gravatar imagetmonteil ( 2015-01-20 16:36:26 +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-01-20 05:22:48 +0100

Seen: 2,280 times

Last updated: Jul 15 '15