Error: using fmincon: FMINCON requires all values returned by functions to be of data type double.
1 回表示 (過去 30 日間)
古いコメントを表示
I am using symbolic toolbox and fmincon for minimization. But when I am using d, r_g and G_g as symbolic variable I am getting an error. This is the error I am getting "FMINCON requires all values returned by functions to be of data type double."
[q] = optimizeParameters();% Call optimizeParameters to define the objective function q
options = optimset('PlotFcns', @optimplotfval);% Set optimization options
d0 = [-0.5, 24, 3e6];
lb = [-1, 24, 2e6];
ub = [0, 32, 6e6];
q(d0(1),d0(2),d0(3))
%[solution, fval] = fmincon(@(x) q(x(1), x(2), x(3)), d0, [], [], [], [], lb, ub, [], options);
function q = optimizeParameters()
syms d r_g G_g%symbolic variables
theta = pi/4;
p = 2*pi;
Vs = 250;
k = p / Vs;
c = Vs / sin(theta);
w5 = 0.001;
n = 3;
dl = -6;
d1 = dl + 2 * n * d - d;
q = @(d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
end
function answer = calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
r_s= 2000;
G_s= 1.25e8;
n_s= 0.3;
l_s= 2*n_s*G_s/(1-2*n_s);
a_s= sqrt((l_s+2*G_s)/r_s);
b_s= sqrt(G_s/r_s);
g_a_s= sqrt((c/a_s)^2-1);
g_b_s= sqrt((c/b_s)^2-1);
theta_s= 2*(b_s/c)^2;
A_s= k*g_a_s*d;
B_s= k*g_b_s*d;
C_A_s= cos(A_s);
S_A_s= sin(A_s);
C_B_s= cos(B_s);
S_B_s= sin(B_s);
n_g= 0.17;
l_g= 2*n_g*G_g/(1-2*n_g);
a_g= sqrt((l_g+2*G_g)/r_g);
b_g= sqrt(G_g/r_g);
g_a_g= sqrt((c/a_g)^2-1);
g_b_g= sqrt((c/b_g)^2-1);
theta_g= 2*(b_g/c)^2;
A_g= k*g_a_g*d;
B_g= k*g_b_g*d;
C_A_g= cos(A_g);
S_A_g= sin(A_g);
C_B_g= cos(B_g);
S_B_g= sin(B_g);
Gs_inv = [theta_s*C_A_s+(1-theta_s)*C_B_s, 1i*(g_a_s*g_b_s*theta_s*S_B_s+(1-theta_s)*S_A_s)/g_a_s, (-C_A_s+C_B_s)/(c^2*r_s), 1i*(g_a_s*g_b_s*S_B_s+S_A_s)/(c^2*g_a_s*r_s);...
1i*(-g_a_s*g_b_s*theta_s*S_A_s+(1-theta_s)*S_B_s)/g_b_s, theta_s*C_B_s+(1-theta_s)*C_A_s, 1i*(g_a_s*g_b_s*S_A_s+S_B_s)/(c^2*g_b_s*r_s), (-C_A_s+C_B_s)/(c^2*r_s);...
c^2*r_s*theta_s*(theta_s-1)*(C_A_s-C_B_s), 1i*c^2*r_s*(g_a_s*g_b_s*theta_s^2*S_B_s-(theta_s-1)^2*S_A_s)/g_a_s, theta_s*C_B_s+(1-theta_s)*C_A_s, 1i*(g_a_s*g_b_s*theta_s*S_B_s+(theta_s-1)*S_A_s)/g_a_s;...
1i*c^2*r_s*(g_a_s*g_b_s*theta_s^2*S_A_s+(theta_s-1)^2*S_B_s)/g_b_s, c^2*r_s*theta_s*(theta_s-1)*(C_A_s-C_B_s), 1i*(-g_a_s*g_b_s*theta_s*S_A_s+(1-theta_s)*S_B_s)/g_b_s, theta_s*C_A_s+(1-theta_s)*C_B_s];
Gg_inv = [theta_g*C_A_g+(1-theta_g)*C_B_g, 1i*(g_a_g*g_b_g*theta_g*S_B_g+(1-theta_g)*S_A_g)/g_a_g, (-C_A_g+C_B_g)/(c^2*r_g), 1i*(g_a_g*g_b_g*S_B_g+S_A_g)/(c^2*g_a_g*r_g);...
1i*(-g_a_g*g_b_g*theta_g*S_A_g+(1-theta_g)*S_B_g)/g_b_g, theta_g*C_B_g+(1-theta_g)*C_A_g, 1i*(g_a_g*g_b_g*S_A_g+S_B_g)/(c^2*g_b_g*r_g), (-C_A_g+C_B_g)/(c^2*r_g);...
c^2*r_g*theta_g*(theta_g-1)*(C_A_g-C_B_g), 1i*c^2*r_g*(g_a_g*g_b_g*theta_g^2*S_B_g-(theta_g-1)^2*S_A_g)/g_a_g, theta_g*C_B_g+(1-theta_g)*C_A_g, 1i*(g_a_g*g_b_g*theta_g*S_B_g+(theta_g-1)*S_A_g)/g_a_g;...
1i*c^2*r_g*(g_a_g*g_b_g*theta_g^2*S_A_g+(theta_g-1)^2*S_B_g)/g_b_g, c^2*r_g*theta_g*(theta_g-1)*(C_A_g-C_B_g), 1i*(-g_a_g*g_b_g*theta_g*S_A_g+(1-theta_g)*S_B_g)/g_b_g, theta_g*C_A_g+(1-theta_g)*C_B_g];
A1= (Gg_inv*Gs_inv)*(Gg_inv*Gs_inv)*Gg_inv;
A_s0= k*g_a_s*dl;
B_s0= k*g_b_s*dl;
C_A_s0= cos(A_s0);
S_A_s0= sin(A_s0);
C_B_s0= cos(B_s0);
S_B_s0= sin(B_s0);
D= [-(a_s/c)^2*C_A_s0, 1i*(a_s/c)^2*S_A_s0, -theta_s*g_b_s*C_B_s0, 1i*theta_s*g_b_s*S_B_s0;...
-1i*(a_s/c)^2*g_a_s*S_A_s0, (a_s/c)^2*g_a_s*C_A_s0, 1i*theta_s*S_B_s0, -theta_s*C_B_s0;...
-r_s*a_s^2*(theta_s-1)*C_A_s0, 1i*r_s*a_s^2*(theta_s-1)*S_A_s0, -r_s*c^2*theta_s^2*g_b_s*C_B_s0, 1i*r_s*c^2*theta_s^2*g_b_s*S_B_s0;...
1i*r_s*a_s^2*theta_s*g_a_s*S_A_s0, -r_s*a_s^2*theta_s*g_a_s*C_A_s0, -1i*r_s*c^2*theta_s*(theta_s-1)*S_B_s0, r_s*c^2*theta_s*(theta_s-1)*C_B_s0];
D1= D(1,1);
D2= D(1,2);
D3= D(1,3);
D4= D(1,4);
D5= D(2,1);
D6= D(2,2);
D7= D(2,3);
D8= D(2,4);
D9= D(3,1);
D10= D(3,2);
D11= D(3,3);
D12= D(3,4);
D13= D(4,1);
D14= D(4,2);
D15= D(4,3);
D16= D(4,4);
a_bar= ((D10-D9)*(D8-D7)+(D5-D6)*(D12-D11))/((D2-D1)*(D8-D7)+(D4-D3)*(D5-D6));
b_bar= ((D9-D10)*(D4-D3)+(D2-D1)*(D12-D11))/((D2-D1)*(D8-D7)+(D4-D3)*(D5-D6));
c_bar= ((D14-D13)*(D8-D7)+(D16-D15)*(D5-D6))/((D2-D1)*(D8-D7)+(D4-D3)*(D5-D6));
d_bar= ((D13-D14)*(D4-D3)-(D16-D15)*(D1-D2))/((D2-D1)*(D8-D7)+(D4-D3)*(D5-D6));
A_s1= k*g_a_s*d1;
B_s1= k*g_b_s*d1;
C_A_s1= cos(A_s1);
S_A_s1= sin(A_s1);
C_B_s1= cos(B_s1);
S_B_s1= sin(B_s1);
D_l= [-(a_s/c)^2*C_A_s1, 1i*(a_s/c)^2*S_A_s1, -theta_s*g_b_s*C_B_s1, 1i*theta_s*g_b_s*S_B_s1;...
-1i*(a_s/c)^2*g_a_s*S_A_s1, (a_s/c)^2*g_a_s*C_A_s1, 1i*theta_s*S_B_s1, -theta_s*C_B_s1;...
-r_s*a_s^2*(theta_s-1)*C_A_s1, 1i*r_s*a_s^2*(theta_s-1)*S_A_s1, -r_s*c^2*theta_s^2*g_b_s*C_B_s1, 1i*r_s*c^2*theta_s^2*g_b_s*S_B_s1;...
1i*r_s*a_s^2*theta_s*g_a_s*S_A_s1, -r_s*a_s^2*theta_s*g_a_s*C_A_s1, -1i*r_s*c^2*theta_s*(theta_s-1)*S_B_s1, r_s*c^2*theta_s*(theta_s-1)*C_B_s1];
B= A1*D_l;
B1= B(1,1);
B2= B(1,2);
B3= B(1,3);
B4= B(1,4);
B5= B(2,1);
B6= B(2,2);
B7= B(2,3);
B8= B(2,4);
B9= B(3,1);
B10= B(3,2);
B11= B(3,3);
B12= B(3,4);
B13= B(4,1);
B14= B(4,2);
B15= B(4,3);
B16= B(4,4);
w_in= (D_l(2,4)-D_l(2,3))*c*w5;
mag_w_in= abs(w_in);
u_in= (D_l(1,4)-D_l(1,3))*c*w5;
mag_u_in= abs(u_in);
Input= sqrt(mag_u_in^2+m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c8-c6*c7), (a_bar*c2+b_bar*c4-c6)/(a_bar*(c1*c4*d_bar-c1*c8-c2*c3*d_bar+c2*c7)+b_bar*(-c1*c4*c_bar+c2*c3*c_bar-c3*c8+c4*c7)+c_bar*(c1*c6-c2*c5)+d_bar*(c3*c6-c4*c5)+c5*c8-c6*c7);...
(-a_bar*c3*d_bar+a_bar*c7+b_bar*c3*c_bar-c5*c_bar)/(a_bar*(c1*c4*d_bar-c1*c8-c2*c3*d_bar+c2*c7)+b_bar*(-c1*c4*c_bar+c2*c3*c_bar-c3*c8+c4*c7)+c_bar*(c1*c6-c2*c5)+d_bar*(c3*c6-c4*c5)+c5*c8-c6*c7), (a_bar*c1*d_bar-b_bar*c1*c_bar-b_bar*c7-c5*d_bar)/(a_bar*(c1*c4*d_bar-c1*c8-c2*c3*d_bar+c2*c7)+b_bar*(-c1*c4*c_bar+c2*c3*c_bar-c3*c8+c4*c7)+c_bar*(c1*c6-c2*c5)+d_bar*(c3*c6-c4*c5)+c5*c8-c6*c7), (c1*c_bar+c3*d_bar-c7)/(a_bar*(c1*c4*d_bar-c1*c8-c2*c3*d_bar+c2*c7)+b_bar*(-c1*c4*c_bar+c2*c3*c_bar-c3*c8+c4*c7)+c_bar*(c1*c6-c2*c5)+d_bar*(c3*c6-c4*c5)+c5*c8-c6*c7), (-a_bar*c1+b_bar*c3+c5)/(a_bar*(c1*c4*d_bar-c1*c8-c2*c3*d_bar+c2*c7)+b_bar*(-c1*c4*c_bar+c2*c3*c_bar-c3*c8+c4*c7)+c_bar*(c1*c6-c2*c5)+d_bar*(c3*c6-c4*c5)+c5*c8-c6*c7)];
Y2= x2_inv*(B*[0;0;-w5;w5]);
u_out1= c*abs(Y2(1,1));
w_out1= c*abs(Y2(2,1));
Output= sqrt(u_out1^2+w_out1^2);
answer= Output/Input;
end
0 件のコメント
採用された回答
Torsten
2024 年 4 月 30 日
移動済み: Torsten
2024 年 4 月 30 日
As you can see above, your function returns a symbolic NaN, but it must return a numerical defined value.
To change the output to numeric, use
function q = optimizeParameters()
%syms d r_g G_g%symbolic variables
theta = pi/4;
p = 2*pi;
Vs = 250;
k = p / Vs;
c = Vs / sin(theta);
w5 = 0.001;
n = 3;
dl = -6;
d1 = @(d)dl + 2 * n * d - d;
q = @(d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1(d));
end
To avoid NaN output, debug "calculateObjective".
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Special Values についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!