I have an optimisation script (below), which gives me the error:
"Undefined function 'cos' for input arguments of type 'optim.problemdef.OptimizationExpression'."
Just to make sure it wasn't a problem with the cos
function, I changed the cos
in confn2
to a sin
and then got:
"Undefined function 'sin' for input arguments of type 'optim.problemdef.OptimizationExpression'."
However, if I put cos(pi)
in the Command Window, I do get -1.
I have successfully run this script before with the exception being that the sigma (which appears in the cos()
) was not an optimisation variable and thus the trig functions were evaluated leaving something linear. Is this type of optimisation script not tractable for constraints with trigonometric functions? Are there alternatives that are within MATLAB?
k1 = optimvar('k1', 'LowerBound', -3, 'UpperBound', 3);
k2 = optimvar('k2', 'LowerBound', -3, 'UpperBound', 3);
f = optimvar('f', 'LowerBound', -3, 'UpperBound', 3);
sigma = optimvar('sigma', 'LowerBound', 0, 'UpperBound', 6.28318530718);
obj = fcn2optimexpr(@eq1, k1, k2, f, sigma);
confn1 = obj == 0;
confn2 = -0.9313129901097519*k1 - 1.4693755421886672*k2 - 0.18532000686683578*f*cos((1/2)*(-0.372795 + 2*sigma)) + 0.9826782255931369*f*sin((1/2)*(-0.372795 + 2*sigma)) <= 0;
prob = optimproblem('Objective', obj);
prob.Constraints.confn1 = confn1;
prob.Constraints.confn2 = confn2;
k0.k1 = 0;
k0.k2 = 0;
k0.f = 0;
k0.sigma = 0;
[sol, fval, exitflag, output] = solve(prob, k0)
function f1 = eq1(k1, k2, f, sigma)
f1 = 0.01308996938995749 - 0.3642198710296203*k1 - 0.6784053942919677*k2 + 0.37064001373367156*f*sin((1/2)*(-0.372795 + 2*sigma));
end
cos
is a recognised function, but not for"input arguments of type optim.problemdef.OptimizationExpression"
. You're essentially doingcos(sigma)
, which isn't defined becausesigma
isn't a numeric variable, you created it usingoptimvar
. - Wolfiesigma
to remain anoptimvar
, or will I need to write something quite different? - Cameron F.optimproblem
. - Wolfie