Problem with optimization function fcn2optimexpr

조회 수: 11 (최근 30일)
Andrea Mazzetto
Andrea Mazzetto 2021년 6월 14일
댓글: Andrea Mazzetto 2021년 6월 15일
Hi, everyone.
I guess I have a simple problem but there's something that i miss. I'm experiencing this error when i try to optimize a function with 8 optimization variables:
"Error using fcn2optimexpr (line 103)
Function evaluation failed while attempting to determine output size. See error cause below. The function might contain an error, or might not be
well-defined at the automatically-chosen point. To specify output size without function evaluation, use 'OutputSize'.
Error in curvatura_V7 (line 27)
obj = fcn2optimexpr(@objfun,rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4);
Caused by:
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix
powers, use '.^'."
The function is as follows:
function erroredistanza = objfun(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4)
deltay = 60;
deltax = 3;
tau0 = 90;
tau0 = deg2rad(tau0);
dmin = 3;
roi1=0;
si1=0;
PSI1 = @(roi1,rof1,si1,sf1,s) tau0 + ((rof1*si1-roi1*sf1)/(si1-sf1))*s + ((roi1-rof1)/(si1-sf1))*(s^2)/2;
PSI2 = @(rof1,rof2,sf1,sf2,s) ((rof2*sf1-rof1*sf2)/(sf1-sf2))*s + ((rof1-rof2)/(sf1-sf2))*(s^2)/2;
PSI3 = @(rof2,rof3,sf2,sf3,s) ((rof3*sf2-rof2*sf3)/(sf2-sf3))*s + ((rof2-rof3)/(sf2-sf3))*(s^2)/2;
PSI4 = @(rof3,rof4,sf3,sf4,s) ((rof4*sf3-rof3*sf4)/(sf3-sf4))*s + ((rof3-rof4)/(sf3-sf4))*(s^2)/2;
funx1 = @(roi1,rof1,si1,sf1,s) integral(@(s) cos(PSI1(roi1,rof1,si1,sf1,s)),0,sf1);
funx2 = @(rof1,rof2,sf1,sf2,s) integral(@(s) cos(PSI2(rof1,rof2,sf1,sf2,s)),sf1,sf2);
funx3 = @(rof2,rof3,sf2,sf3,s) integral(@(s) cos(PSI3(rof2,rof3,sf2,sf3,s)),sf2,sf3);
funx4 = @(rof3,rof4,sf3,sf4,s) integral(@(s) cos(PSI4(rof3,rof4,sf3,sf4,s)),sf3,sf4);
funy1 = @(roi1,rof1,si1,sf1,s) integral(@(s) sin(PSI1(roi1,rof1,si1,sf1,s)),0,sf1);
funy2 = @(rof1,rof2,sf1,sf2,s) integral(@(s) sin(PSI2(rof1,rof2,sf1,sf2,s)),sf1,sf2);
funy3 = @(rof2,rof3,sf2,sf3,s) integral(@(s) sin(PSI3(rof2,rof3,sf2,sf3,s)),sf2,sf3);
funy4 = @(rof3,rof4,sf3,sf4,s) integral(@(s) sin(PSI4(rof3,rof4,sf3,sf4,s)),sf3,sf4);
funx = @(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) funx1(roi1,rof1,si1,sf1) + funx2(rof1,rof2,sf1,sf2) + funx3(rof2,rof3,sf2,sf3) + funx4(rof3,rof4,sf3,sf4);
funy = @(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) funy1(roi1,rof1,si1,sf1) + funy2(rof1,rof2,sf1,sf2) + funy3(rof2,rof3,sf2,sf3) + funy4(rof3,rof4,sf3,sf4);
fun2x =@(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) funx(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) - deltax;
fun2y =@(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) funy(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) - deltay;
fun3x =@(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) fun2x(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4).^2;
fun3y =@(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) fun2y(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4).^2;
d =@(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) sqrt(fun3x(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) + fun3y(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4));
erroredistanza = d(rof1,rof2,rof3,rof4,sf1,sf2,sf3,sf4) - dmin;

채택된 답변

Matt J
Matt J 2021년 6월 14일
편집: Matt J 2021년 6월 14일
It is likely that you meant to do element-wise exponentiation .^ somewhere in your function, but have ^ instead.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by