fmincon with matrix inputs --> nonlinear cost function and constraints
1 次查看(过去 30 天)
显示 更早的评论
I have a myfun.m file that defines the following function:
f = 0.5 * (norm(C*x - d))^2;
and it is called in a main script by:
[x, fval] = fmincon(@myfun, x0, -A, b, Aeq, beq, lb, ub);
C is a 170x72 matrix, d is a 170x1 vector, so x is a 72x1 vector. This part works well.
Now I want to multiply each element of C by a constant raised to a power, and solve for that power. So instead of C(1,1) = 4, now it should be = (2^p)*4. However, I want p to be solved for in the optimization as well, so p = x(73). I realize that augmenting the x matrix means I have to update x0, A, b, Aeq, beq, lb, ub to all include 73 elements, which I've done. The problem arises when I define the elements of the C matrix like C(1,1)= (2^x(73))*4, I get the: "Index exceeds matrix dimensions" error The new x(73) element has no constraints or bounds associated with it. Any thoughts on why this is not working or how to fix it? Is it somehow invalid to use an element of x inside C, then multiply by x as I do in the cost function?
Note: I realize this cost function looks just like least squares. I used fmincon originally since I had multiple objectives, and will continue to use it after I solve this problem because I need to add in nonlinear constraints.
1 个评论
Matt J
2014-5-22
The problem arises when I define the elements of the C matrix like C(1,1)= (2^x(73))*4
If the base appearing in C(i,j)(in this case the 2 in 2^x(73)) is a constant independent of (i,j), then there is no need to be modifying C. You could just rewrite the cost function as
f = @(x) 0.5 * (norm( C* (x(1:72)*2^x(73) ) - d))^2;
and this would be more efficient because 2^x(73) is now multiplied with only the 72 elements of x(1:72) instead of the 12240 elements in C.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!