Question: what is the correct way to define a function with indexed variables?

Actually, I have two questions here, I don't know if they are connected.

I don't know if it's the right way to deifne function with indexed variables, is there any difference between f and g?

I assume they are the same since neither of them work with Maximize.

Finally, I use the most original way in (5), why give the wrong answer? (no way the answer is -infinity)

restart

f := proc (x) options operator, arrow; sum(ln(x[i]), i = 1 .. 4) end proc

proc (x) options operator, arrow; sum(ln(x[i]), i = 1 .. 4) end proc``

(1)

g := proc (x) options operator, arrow; ln(x[1])+ln(x[2])+ln(x[3])+ln(x[4]) end proc

proc (x) options operator, arrow; ln(x[1])+ln(x[2])+ln(x[3])+ln(x[4]) end proc

(2)

f([1, 1, 1, 1])

0

(3)

cons := {x[1]+x[2] <= 1, x[1]+x[3] <= 2}

{x[1]+x[2] <= 1, x[1]+x[3] <= 2}

(4)

with(Optimization)

Maximize(f, cons, assume = nonnegative)

Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures

 

Maximize(f, {x[1]+x[2] <= 1, x[1]+x[3] <= 2}, assume = nonnegative)

Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures

 

Maximize(g, cons, assume = nonnegative)

Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures

 

Maximize(g, {x[1]+x[2] <= 1, x[1]+x[3] <= 2}, assume = nonnegative)

Error, (in Optimization:-NLPSolve) constraints must be specified as a set or list of  procedures

 

 

Maximize(sum(ln(x[i]), i = 1 .. 4), {x[1]+x[2] <= 1, x[1]+x[3] <= 2}, assume = nonnegative)

[Float(-infinity), [x[1] = HFloat(1.0), x[2] = HFloat(0.0), x[3] = HFloat(1.0), x[4] = HFloat(1.0)]]

(5)

NULL

Download mre.mw

Please Wait...