Question about FMINCON in optimization
Afficher commentaires plus anciens
Hi, I got a problem in fmincon in the nonlinear constrained optimization problem. Both target and constrain functions are integral functions. So I named four functions to make it clear.
The function is to optimize a -- a 10x1 array. l is a constant, and x is the variable to be integrated.
The problem is that when I call the fmincon, the function returns an error saying fmincon requires all values returned by functions to be of data type double.
I guess this probablly related to the intefral that I used. But I have no idea how to fix that.
Please help me out, thank you.
clc;clear;
a0 = ones(10,1);
l = 100;
seriesNum = 1;
syms x
% Pass fixed parameters to objfun
objfun3 = @(a)objectiveFcn(l,a);
% Set nondefault solver options
options3 = optimoptions('fmincon','PlotFcn','optimplotfvalconstr');
% Solve
[solution,objectiveValue] = fmincon(objfun3,a0,[],[],[],[],[],[],...
@constraintFcn,options3);
% Clear variables
clearvars objfun3 options3
% Functions
function f = divsurf(x,l,a,seriesNum)
fi0 = 3;
f = 1/l*(-fi0);
for i = 1:seriesNum
f = f + a(i)*i*pi/l*cos(i*pi*x/l);
end
f = f^2;
end
function f = surf(x,l,a,seriesNum)
% f = a*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
fi0 = 3;
f = fi0;
for i = 1:seriesNum
f = f + x / l * (-fi0) + a(i)*sin(i*pi*x/l);
end
end
% The following code creates the objective function. Modify this code for your problem.
function f = objectiveFcn(l,a)
% f = a*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
% l = 100;
syms x
seriesNum = 10;
f = int( @(x)divsurf(x,l,a,seriesNum),x,0,l);
end
% The following code creates the constraint function. Modify this code for your problem.
function [c,ceq] = constraintFcn(a)
lout = 10;
seriesNum = 10;
l = 100;
syms x
% c(1) = x(1)^2 + x(2)^2 - 5;
% c(2) = 3 - x(1)^2 - x(2)^2;
c(1) = int(@(x)surf(x,l,a,seriesNum),x,0,l)-lout;
ceq = []; % No equality constraints
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!