Error using function handles in genetic algorithm.
Mostrar comentarios más antiguos
I am getting error:
Unrecognized function or variable 'p'.
clc; clear; close all;
Vs = 250;
k = @(p) p/Vs;
c = @(theta) Vs/sin(theta);
w = 0.001;
d_h = -2;
numElements = 10;
ES = d_h/numElements;
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);
r_g= 24;
G_g= 3e6;
n_g= 0.17;
dl = -5;
%% Genetic Algorithm Parameters
options = optimoptions(@ga,'PopulationSize', 30, 'MaxGenerations', 200,'PlotFcn','gaplotbestf');
% Objective Function
objective_function = @(x_optimal) calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k(p), c(theta), r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
A = [];
b = [];
lb = zeros(1, numElements-2);
ub = ones(1, numElements-2);
nonlcon = [];
[x_optimal, fval] = ga(objective_function, numElements-2, A, b, [], [], lb, ub,nonlcon, options);
% function to calculate objective
function answer = calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k, c, r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
x = [1, x_optimal, 1];
disp(x_optimal);
% rest of the code
answer = integral2(@(theta,p)arrayfun(@(theta,p)q(theta,p),theta,p),pi/18,pi/6,2*pi,20*pi); % minimize
end
6 comentarios
Star Strider
el 13 de Mayo de 2024
The missing ‘p’ (used in function ‘k’) is not the only problem.
The ‘answer’ integration uses function ‘q’, however ‘q’ is also nowhere to be found.
Manoj Manoj
el 13 de Mayo de 2024
objective_function = @(x_optimal) calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k, c, r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
instead of
objective_function = @(x_optimal) calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k(p), c(theta), r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
Remember that in "calculateObjective", k and c will be function handles, not values. Use them correctly in function q.
Star Strider
el 13 de Mayo de 2024
The only reference I found to ‘q’ was in the ‘answer’ assignment. I still can’t find the actual function anywhere. I’d have trtied to run the code otherwise.
Manoj Manoj
el 14 de Mayo de 2024
Try this.
I wonder why your function does not depend on "x_optimal" . From your code, you will always get the same value for "answer" from the "integral2" function, and thus "ga" will terminate with the initial condition vector for x_optimal.
Vs = 250;
k = @(p) p/Vs;
c = @(theta) Vs/sin(theta);
w = 0.001;
d_h = -2;
numElements = 10;
ES = d_h/numElements;
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);
r_g= 24;
G_g= 3e6;
n_g= 0.17;
dl = -5;
%% Genetic Algorithm Parameters
options = optimoptions(@ga,'PopulationSize', 30, 'MaxGenerations', 200,'PlotFcn','gaplotbestf');
% Objective Function
objective_function = @(x_optimal) driver_calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k, c, r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
A = [];
b = [];
lb = zeros(1, numElements-2);
ub = ones(1, numElements-2);
nonlcon = [];
[x_optimal, fval] = ga(objective_function, numElements-2, A, b, [], [], lb, ub,nonlcon, options);
function answer = driver_calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k, c, r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
q = @(theta,p)calculate_objective(theta,p,x_optimal,Vs, w, r_s, G_s, n_s, k(p), c(theta), r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);)
answer = integral2(@(theta,p)arrayfun(@(theta,p)q(theta,p),theta,p),pi/18,pi/6,2*pi,20*pi); % minimize
end
% function to calculate objective
function answer = calculateObjective(x_optimal, Vs, w, r_s, G_s, n_s, k, c, r_g, G_g, n_g, dl, d_h, numElements, ES, l_s, a_s, b_s);
disp(x_optimal);
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*dl;
B_g = k*g_b_g*dl;
C_A_g= cos(A_g);
S_A_g= sin(A_g);
C_B_g= cos(B_g);
S_B_g= sin(B_g);
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+(theta_g-1)*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];
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_h;
B_s = k*g_b_s*d_h;
C_A_s= cos(A_s);
S_A_s= sin(A_s);
C_B_s= cos(B_s);
S_B_s= sin(B_s);
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+(theta_s-1)*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];
B= Gs_inv*Gg_inv;
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);
c1= -(B1+B2);
c2= -(B3+B4);
c3= -(B5+B6);
c4= -(B7+B8);
c5= -(B9+B10);
c6= -(B11+B12);
c7= -(B13+B14);
c8= -(B15+B16);
x2_inv= [(-b_bar*c3*c8+b_bar*c4*c7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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;-w;w]);
u_in= (B(1,4)-B(1,3))*c*w;
mag_u_in= abs(u_in);
w_in= (B(2,4)-B(2,3))*c*w;
mag_w_in= abs(w_in);
Input= sqrt(mag_u_in^2+mag_w_in^2);
u_out= c*abs(Y2(1,1));
w_out= c*abs(Y2(2,1));
Output= sqrt(u_out^2+w_out^2);
answer = Output/Input;
end
Respuestas (0)
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!