fmincon for multiple variables

조회 수: 7 (최근 30일)
Guan Hao
Guan Hao 2023년 8월 23일
댓글: Guan Hao 2023년 8월 25일
Hi,
I want to perform a constrained optimization,that makes fmincon a good option.
However, it returns the error '' fmincon requires all values returned by the functions to be of data type double''.
Y is the objective that I want to optimize,with three variables a1,a2,a3.
function Y = optfun(z)
a1 = z(1);
a2 = z(2);
a3 = z(3);
y1 = abs(vpa("0.129")*a1*sym(1i) + vpa("0.01")*a2*sym(1i) + vpa("8.57e-4")*a3*sym(1i) - vpa("2410.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) - vpa("2410.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) - vpa("2410.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("25.0") + vpa("96.4i"))/abs(vpa("0.129")*a1*sym(1i) + vpa("0.01")*a2*sym(1i) + vpa("8.57e-4")*a3*sym(1i) + vpa("2410.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) + vpa("2410.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) + vpa("2410.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("125.0") + vpa("96.4i"));
y2 = abs(vpa("0.173")*a1*sym(1i) + vpa("0.0135")*a2*sym(1i) + vpa("0.00115")*a3*sym(1i) - vpa("3250.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) - vpa("3250.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) - vpa("3250.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("25.0") + vpa("130.0i"))/abs(vpa("0.173")*a1*sym(1i) + vpa("0.0135")*a2*sym(1i) + vpa("0.00115")*a3*sym(1i) + vpa("3250.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) + vpa("3250.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) + vpa("3250.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("125.0") + vpa("130.0i"));
y3 = abs(vpa("0.197")*a1*sym(1i) + vpa("0.0153")*a2*sym(1i) + vpa("0.00131")*a3*sym(1i) - vpa("3690.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) - vpa("3690.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) - vpa("3690.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("25.0") + vpa("148.0i"))/abs(vpa("0.197")*a1*sym(1i) + vpa("0.0153")*a2*sym(1i) + vpa("0.00131")*a3*sym(1i) + vpa("3690.0i")/(vpa("50.0") + vpa("3.7e-5")*a3 + vpa("0.00111")*a2 + vpa("0.0333")*a1) + vpa("3690.0i")/(vpa("50.0") + vpa("2.96e-4")*a3 + vpa("0.00444")*a2 + vpa("0.0667")*a1) + vpa("3690.0i")/(vpa("50.0") + vpa("0.001")*a3 + vpa("0.01")*a2 + vpa("0.1")*a1) + vpa("125.0") + vpa("148.0i"));
Y = (1-y1)+(1-y3)+(y2);
end
By the way,I want to know is there possibilities to add constration to my objective value?
For example, 0<[y1,y2,y3]<1.
Any help will be appreciate,thx!

채택된 답변

Matt J
Matt J 2023년 8월 23일
편집: Matt J 2023년 8월 23일
Y = double( (1-y1)+(1-y3)+(y2) );
But I don't see why you are using vpa and other sym operations. I don't think it really increases precision for you since all your coefficients have substantially less than 32 decimal places.
  댓글 수: 20
Steven Lord
Steven Lord 2023년 8월 24일
You're calling the optfun to generate the intermediate values of your objective function, then after the fprintf lines (which I added just to show you that the constraint function had correctly obtained the values from the objective function) you throw away those intermediate values and replace them with elements of the input with which fmincon called the constraint function. Don't do that. Delete these lines from your constraint function:
y1 = z(1);
y2 = z(2);
y3 = z(3);
Guan Hao
Guan Hao 2023년 8월 25일
@Steven Lord Thank you sir !!! It's working now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by