Ask Your Question
0

Converting expression into a number and evaluating function

asked 2019-10-12 04:18:44 +0200

JGC gravatar image

updated 2019-10-12 04:19:21 +0200

I was trying to use a lagrange multiplier to find the local maxima of a simple function over the circle:

x,y,c = var('x y c')

f(x,y) = x**2 + y**2 - 1
g(x,y) = x+y
F = diff(f)
G = diff(g)

polys = [f]
for i in range(len(F)):
    p = F[i] - c * G[i]
    polys.append(p)

list_solutions = solve(polys,x,y,c)

for solution in list_solutions:
    print solution
    print f({x : solution[0] , y : solution[1]})
    print '\n'

At 'print solution' i get the first extremum:

[x == 1/2*sqrt(2), y == 1/2*sqrt(2), c == sqrt(2)]

But at the 'print f({x : solution[0] , y : solution[1]})' line the following error occurs:

TypeError: no canonical coercion from <type 'dict'> to Callable function ring with arguments (x, y)

So i guess x == 1/2*sqrt(2) and the like can't be seem as numbers. I would like to be able to evaluate f(x,y) at the point, so what can i do? Also, the lagrange multiplier algorithm must be on Sage already, but Google gives me nothing. If you can show its command that would be a plus.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-10-12 08:30:42 +0200

ortollj gravatar image

Hi JGC

I propose to replace print f({x : solution[0] , y : solution[1]}) by print f(x,y).subs(solution) ?

edit flag offensive delete link more

Comments

Thanks, it works.

JGC gravatar imageJGC ( 2019-10-15 02:29:08 +0200 )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: 2019-10-12 04:18:44 +0200

Seen: 197 times

Last updated: Oct 12 '19