ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 09 Apr 2012 18:45:16 +0200Minimization with symbolic calculations in a functionhttps://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/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.
Sat, 07 Apr 2012 01:15:58 +0200https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/Answer by fidbc for <p>I am a new Sage user, and I am unable to get the constrained minimization function working if I include a symbolic differentiation.</p>
<p>The code below works fine</p>
<pre><code>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])
</code></pre>
<p>This code gives an error</p>
<pre><code>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])
</code></pre>
<p>Any assistance would be greatly appreciated.</p>
https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13433#post-id-13433Hi,
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.Sat, 07 Apr 2012 03:04:19 +0200https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13433#post-id-13433Answer by Husker for <p>I am a new Sage user, and I am unable to get the constrained minimization function working if I include a symbolic differentiation.</p>
<p>The code below works fine</p>
<pre><code>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])
</code></pre>
<p>This code gives an error</p>
<pre><code>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])
</code></pre>
<p>Any assistance would be greatly appreciated.</p>
https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13441#post-id-13441I 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)Mon, 09 Apr 2012 18:45:16 +0200https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13441#post-id-13441Answer by Husker for <p>I am a new Sage user, and I am unable to get the constrained minimization function working if I include a symbolic differentiation.</p>
<p>The code below works fine</p>
<pre><code>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])
</code></pre>
<p>This code gives an error</p>
<pre><code>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])
</code></pre>
<p>Any assistance would be greatly appreciated.</p>
https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13439#post-id-13439Thanks! 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])Sun, 08 Apr 2012 17:45:14 +0200https://ask.sagemath.org/question/8865/minimization-with-symbolic-calculations-in-a-function/?answer=13439#post-id-13439