Ask Your Question
0

Minimization with symbolic calculations in a function

asked 2012-04-06 18:15:58 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am a new Sage user, and I am unable to get the constrained minimization function working if I include a symbolic differentiation.

The code below works fine

c=var('c')
y=5*c^3
def f(c):
    #z=dy^2
    w=5*(c-10)^3
    z=w^2
    return z
minimize_constrained(f, [(-10,10)],[9])

This code gives an error

c=var('c')
y=5*c^3
def f(c):
    dy=y.diff(c)
    w=5*(c-10)^3
    z=dy+w
    return z
minimize_constrained(f, [(-10,10)],[9])

Any assistance would be greatly appreciated.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2012-04-06 20:04:19 -0600

fidbc gravatar image

Hi,

The problem seems to be that sage's minimize_contrained function will call the function f with c being a 1-dimensional array and the diff function expects a variable name.

A workaround to this would be to give a different name to the parameter of f, say x. Then we could differentiate y and substitute c=x[0], as shown below

def f(x): 
   dy=y.diff(c)
   w=5*(c-10)^3 
   z=dy+w 
   return z.subs(c=x[0])

It might be the case that sage is differentiating y each time f is called, which might end up consuming some time. To avoid this maybe just defining z=dy+w and calling minimize_constrained(z,[(-10,10)],[9]) would do the job.

edit flag offensive delete link more
0

answered 2012-04-08 10:45:14 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Thanks! That was the problem. I also moved the derivative out of the optimization loop.

a,b,c,z,dy=var('a b c z dy')

y=5*c^3

dy=y.diff(c)

def f(x):

    w=5*(c-10)^3 
    z=dy+w 
    return z.subs(c=x[0])

minimize_constrained(f, [(-10,10)],[9])

edit flag offensive delete link more
0

answered 2012-04-09 11:45:16 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I am trying to expand this solution to multiple variables, but I am having no success. What is the syntax for the problem below? I am unsure of the syntax for the minimize_constrained function, and the syntax in the return statement. Also, is there a way to add a constraint to a value returned by the function? In this case Vt? I would like to minimize Vs while forcing Vt to equal the variable Target.

P, T, Ps, Ts, yTarget = var('P, T, Ps, Ts, yTarget')
y=25+200*P-2*P^2+14*T
dP=y.diff(P)
dT=y.diff(T)
dP2=y.diff(P,2)
dT2=y.diff(T,2)
Psn=5
Tsn=10
yTarget=7000
def f(Pn,Tn):
    ya=y+(1/2)*(dP2*Psn^2+dT2*Tsn^2)
    ys=(dP^2*Ps^2+dT^2*Ts^2)^0.5
    return ys.subs(P=Pn,T=Tn,Ps=Psn,Ts=Tsn), ya.subs(P=Pn,T=Tn,Ps=Psn,Ts=Tsn)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-06 18:15:58 -0600

Seen: 230 times

Last updated: Apr 09 '12