ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 03 May 2020 01:55:31 -0500Solving Lagrangians in Sagehttp://ask.sagemath.org/question/51241/solving-lagrangians-in-sage/[Note for mods: I already submitted a question I'd like to update but I can't seem to find it now.]
I have been running the following Sage code on cocalc.com to solve a standard utility maximization problem that is in an intermediate microeconomics course. However I'm having difficulty solving these problems in the context of Sage.
Below is the code that I have run to solve the problem and so far I believe it's correct.
x, y, l = var('x, y, l')
U = x^0.7 * y^0.3; U
m = 2*x+2*y; m
solve(m == 4000, y)
L = U - l * (m - 4000); L
dLdx = L.diff(x); dLdx
dLdy = L.diff(y); dLdy
dLdl = L.diff(l); dLdl
solve([dLdx == 0, dLdy == 0, dLdl == 0], x, y, l)
All my code seems to be running fine, however the last line of:
solve([dLdx == 0, dLdy == 0, dLdl == 0], x, y, l)
is not outputting the final demand equations. Is there any reason why this is happening?EconJohnSun, 03 May 2020 01:55:31 -0500http://ask.sagemath.org/question/51241/Help with defining constraints in Lagrangianhttp://ask.sagemath.org/question/40908/help-with-defining-constraints-in-lagrangian/I am trying to maximize the function P, with constraints of x >= 75,000 and y >= 200,000. However, I do not think I am defining them correctly. I attempt to define them as arguments of the solve() function.
var('x','y', 'lam')
P = (0.05*x*(1-(x/150000) - 0.0000008*x*y)) + (0.08*y*(1-(y/200000) - 0.0000008*x*y))
diff(P, x)
-(4.00000000000000e-8)*x*y - (6.40000000000000e-8)*y^2 - (3.33333333333333e-7)*x*(0.120000000000000*y + 1) - (3.33333333333333e-7)*x + 0.0500000000000000
diff(P, y)
-(4.00000000000000e-8)*x^2 - (6.40000000000000e-8)*x*y - (4.00000000000000e-7)*(0.160000000000000*x + 1)*y - (4.00000000000000e-7)*y + 0.0800000000000000
solve([diff(P,x) == 0, diff(P,y) == 0, x >= 75000, y >= 200000], x, y, lam)
[[75000 < x, 200000 < y, -(4e-08)*x*y - (6.4e-08)*y^2 - (3.33333333333333e-07)*x*(0.12*y + 1) - (3.33333333333333e-07)*x + 0.05 == 0, -(4e-08)*x^2 - (6.4e-08)*x*y - ((6.4e-08)*x + 4e-07)*y - (4e-07)*y + 0.08 == 0], [x == 75000, 200000 < y, -(6.4e-08)*y^2 - 0.006*y == 0, -0.0096008*y - 224.92 == 0], [x == 75000, y == 200000, -3760.0 == 0, -2145.08 == 0], [y == 200000, 75000 < x, -0.01600066666666667*x - 2559.949999999999 == 0, -(4e-08)*x^2 - 0.0256*x - 0.08 == 0]]ds22Wed, 31 Jan 2018 16:26:36 -0600http://ask.sagemath.org/question/40908/Get index of a set constrainthttp://ask.sagemath.org/question/32119/get-index-of-a-set-constraint/ Using SageMath I'm trying to implement a method for the solution of a problem where I in an iterative way add a set of constraints, try to solve the problem to check if exist a solution, if not i remove the constraints and restart.
try:
p.solve()
except RuntimeError:
p.remove_constraint(index)
Given a set of constraint that I add, how to get their indexes so that I can remove them?tom123Sat, 09 Jan 2016 06:04:36 -0600http://ask.sagemath.org/question/32119/How can I programmatically define constraints for minimize_constrained?http://ask.sagemath.org/question/31179/how-can-i-programmatically-define-constraints-for-minimize_constrained/I'd like to run a minimization problem in several different dimensions, and then find a minimum over the results (for more information on the problem see [here](https://math.stackexchange.com/questions/1549895/minimizing-the-non-linear-cost-of-successive-bernoulli-trials)).
I can easily define both the objective function and the main constraint in a dimenson-independent way with a simple combination of `map`, `sum`, and `prod`:
d = 25
t = 0.75
f = lambda p: sum(map(lambda x: d^2 * tan(x * pi/2) + 8*d, p))
c1 = lambda p: 1 - prod(map(lambda q: 1 - q, p)) - t
On the other hand, I'm not sure on how to express the fact that every variable should be an element of the interval `[a,b]`, where `0 < a < b < 1` are to be given beforehand. I thought I could use `min` like so:
a = 0.1
b = 0.95
lower_bound = lambda x: x - a
upper_bound = lambda x: b - x
c_lower = lambda p: min(map(lower_bound, p))
c_upper = lambda p: min(map(upper_bound, p))
but I'm worried that using those instead of a pair of constraints for every variable would have a negative effect on `minimize_constrained`.A.P.Sun, 29 Nov 2015 06:20:53 -0600http://ask.sagemath.org/question/31179/add matrix as a contrainthttp://ask.sagemath.org/question/27275/add-matrix-as-a-contraint/ Is there anyway where we can add a constraint as a matrix.
For example I have my matrix C=matrix(5,6)
I want to add a constraint p.add_constraint(C[i,j]>=1)
If I do this sagemath I get value error.sujataWed, 08 Jul 2015 03:41:30 -0500http://ask.sagemath.org/question/27275/Use of constraint_generation in MixedIntegerLinearProgramhttp://ask.sagemath.org/question/10915/use-of-constraint_generation-in-mixedintegerlinearprogram/I would like to solve an LP problem using row generation (aka constraint generation), where I wish to be able to choose how the constraints are generated (this will be done using SAT, or WPMaxSAT through a binary MIP).
Now, in MixedIntegerLinearProgram, there is a boolean option `constraint_generation`. I do not find an explanation of its use and therefore whether it can be used for my stated purpose. So, can I, and if so, how?
Information found, but which did not make th option clear to me:
From the [documentation](http://www.sagemath.org/doc/reference/numerical/sage/numerical/mip.html):
> constraint_generation whether to require the returned solver to support constraint generation (excludes Coin). False by default.
From the [source code documentation](http://https://github.com/sagemath/sage/blob/master/src/sage/numerical/backends/generic_backend.pyx#L983):
> - ``constraint_generation`` (boolean) -- whether the solver returned is to be used for constraint/variable generation. As the interface with Coin does not support constraint/variable generation, setting ``constraint_generation`` to ``False`` ensures that the backend to Coin is not returned when ``solver = None``. This is set to ``False`` by default.
(N.B.: Shouldn't that be: backend to Coin is not returned -> Coin backend may be returned?)Erik QuaeghebeurWed, 15 Jan 2014 02:51:10 -0600http://ask.sagemath.org/question/10915/TypeError: Computation failed since Maxima requested additional constraintshttp://ask.sagemath.org/question/8340/typeerror-computation-failed-since-maxima-requested-additional-constraints/I have the following code:
N, n, f = var('N, n, f')
assume(N>0)
sum(binomial(N, n)*f^n*(1-f)^(N-n), n, (N+1)/2, N)
It gives me the following error message:
TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(N+1>0)' before integral or limit evaluation, for example): Is N+1 positive, negative, or zero?ckThu, 22 Sep 2011 07:56:11 -0500http://ask.sagemath.org/question/8340/