Error using sqpInterface Objective function is undefined at initial point.
Show older comments
while using nmpc example of the inverted pendulum.I am getting this error. I just change model to double pendulum model in pendulumCT.m file. what should I do?
Error using sqpInterface
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 808)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpInterface(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
Error in nmpc (line 52)
uopt = fmincon(COSTFUN,uopt,[],[],[],[],LB,UB,CONSFUN,options);
Accepted Answer
More Answers (3)
kamilya MIMOUNI
on 26 Oct 2019
0 votes
error using sqpInterface
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 823)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpInterface(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
Error in Test3 (line 89)
Control_optimal = fmincon(problem)
i have this problem in matlab
please help me
kamilya MIMOUNI
on 26 Oct 2019
0 votes
function Test3
clear;close all; clc
% For starting
fprintf ( 1, '\n' );
fprintf ( 1, 'Test1:\n' );
fprintf ( 1, ' MATLAB version:\n' );
fprintf ( 1, ' A program to demonstrate the finite element method.\n' );
% Geometry data
rin = 0.2; rex = 1;
gd1 = [1;1.6;0;rex];
gd2 = [1;1.6;0;rin];
gd = [gd1,gd2];
ns = (char('Cext','Cint'))';
sf = 'Cext - Cint';
[dl, bt] = decsg(gd, sf, ns);
model = createpde;
geometryFromEdges(model,dl);
pdegplot(model,'EdgeLabels','on','FaceLabels','on')
msh = generateMesh(model,'Hmax',0.05,'GeometricOrder','linear','Jiggle','on');
[p,e,t] = meshToPet(msh);
% [p,e,t] = refinemesh(dl,p,e,t);
% return
coordinates = p';
elements3 = t(1:3,:)';
Ne = findNodes(msh,'region','Edge',(1:4));
Ni = findNodes(msh,'region','Edge',(5:8));
neumann = [Ni(:),[Ni(2:end)';Ni(1)]];
dirichlet = [Ne(:),[Ne(2:end)';Ne(1)]];
BoundNodes = unique(dirichlet);
% Export Initial Data
load flux_initial.mat flux_initial resultss Nee
uintrp = interpolateSolution(resultss,p(1,:),p(2,:));
uintrp(Ne) = 1;
[uintrpx,uintrpy] = pdegrad(p,t,uintrp); % au centre des triangles
uintrpxn = pdeprtni(p,t,uintrpx); % au noeuds
uintrpyn = pdeprtni(p,t,uintrpy); % au noeuds
Nx = @(z)2*z(1,:);
Ny = @(z)2*z(2,:);
z = p(:,Ne);
nx = Nx(z); nx = nx(:);
ny = Ny(z); ny = ny(:);
epsilon = 3/10;
h_flux = 1./(sqrt(nx.^2+ny.^2).*sqrt(z(1,:)'.^2+z(2,:)'.^2).*uintrpxn(Ne).*nx+uintrpyn(Ne).*ny);
% Direct problem
RHS_step = zeros(size(elements3,1));
a_x = zeros(size(elements3,1));
c_x = zeros(size(elements3,1));
Control = zeros(size(neumann,1),1);
for j=1:size(elements3,1)
xc = sum(coordinates(elements3(j,:),:))/3;
RHS_step(j) = 0;
c_x(j) = 1./(1+epsilon*xc(1));
a_x(j) = 0;
end
psi_d = ones(size(BoundNodes,1),1); % fct g sur Gamma_ex
for j=1:size(neumann,1)
x_m = sum(coordinates(neumann(j,:),:))/2; % vecteur ligne: coordonnées du milieu du segment
Control(j) = 1; % le controle v sur Gamma_in
end
[psi,psi_flux] = Direct_Problem(elements3,neumann,coordinates,c_x,a_x,RHS_step,Control,psi_d,BoundNodes,p,t, ...
epsilon,nx,ny,Ne,z);
% Cost function
regu = 1/1000;
Integrand = psi_flux - h_flux;
Cost_J = Cost_Function(Control,Integrand,coordinates,Ne,Ni,rex,rin,regu);
if(Cost_J==0)
disp('Your intial guess v = 1 is exactely your solution')
disp('There is no need for optimization')
end
disp('fmincon start')
% options = optimoptions('fmincon','Display','iter','Algorithm','sqp','PlotFcns', ...
% {@optimplotfval,@optimplotfirstorderopt},'TolFun',4.0e-4);
% options = optimoptions(@fmincon,'GradObj','on','Display','iter','TolFun',1.0e-4, ...
% 'PlotFcns',{@optimplotfval,@optimplotfirstorderopt});
lb = 4.0e-2*ones(size(Control,1),1);
ub = 5*ones(size(Control,1),1);
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
problem.options = options;
problem.solver = 'fmincon';
problem.objective = @(u_epsilon)Moncef_Cost_Function(u_epsilon,elements3,neumann,coordinates,c_x,a_x,RHS_step, ...
BoundNodes,p,t,epsilon,nx,ny,Ne,Ni,z,rex,rin,regu,h_flux);
problem.x0 = Control;
problem.nonlcon = [];
Control_optimal = fmincon(problem)
Control_optimal = fmincon(@(u_epsilon)Moncef_Cost_Function(u_epsilon,elements3,neumann,coordinates,c_x,a_x,RHS_step, ...
BoundNodes,p,t,epsilon,nx,ny,Ne,Ni,z,rex,rin,regu,h_flux), ...
Control,[],[],[],[],lb,ub,[],options);
% Retour au probleme direct final
[psi,psi_flux] = Direct_Problem(elements3,neumann,coordinates,c_x,a_x,RHS_step,Control_optimal,psi_d,BoundNodes,p,t, ...
epsilon,nx,ny,Ne,z);
% ep = 0.2;
% Nb = findNodes(msh,'box',[-1-ep -1],[-0.5 0.5]);
%[i,c] = pdesdp(p,e,t,sdl);
figure(10)
pdemesh(model_initial)
hold on
plot(msh.Nodes(1,Nee),msh.Nodes(2,Nee),'or','MarkerFaceColor','g')
psi_full = full(psi);
psi_max = max(psi_full(Nee));
indice2 = find(psi_full<=psi_max+0.07 & psi_full>=psi_max-0.07);
k = convhull(p(1,indice2),p(2,indice2));
figure(12)
pdemesh(model)
hold on
plot(p(1,k),p(2,k),'r-')
figure(11)
pdegplot(model)
hold on
plot(msh.Nodes(1,indice2),msh.Nodes(2,indice2),'.r','MarkerFaceColor','r')
kamilya MIMOUNI
on 26 Oct 2019
0 votes
function probleme_initial
close all; clear all; clc
R0 = 3/2;
a1 = 0.5;
N = 40;
angles = 0:2*pi/N:2*pi; angles = angles(1:end-1);
absi = (R0*sqrt(1+2*a1*cos(angles)/R0)-R0)./a1; absi = absi(:);
ord = a1*R0*sin(angles)./a1; ord = ord(:);
% Geometry data
poly = [absi,ord];
gd1 = [2;length(poly);poly(:,1);poly(:,2)];
abs = linspace(-1,1,N); abs = abs(end:-1:1);
abs = [abs,abs(end-1:-1:2)];
poly1 = [absi(:),ord(:)];
%======================================
ep = 0.1;
abs2 = [-1;-1;-1-ep;-1-ep];
ord2 = [-0.5;0.5;0.5;-0.5];
poly2 = [abs2,ord2];
gd2 = zeros(size(gd1));
gd2(1) = 2;
gd2(2) = length(poly2);
gd2(3:length(poly2(:,1))+2) = poly2(:,1);
gd2(length(poly2(:,1))+3:length(poly2(:,1))+2+length(poly2(:,2))) = poly2(:,2);
% gd2 = [2;length(poly2);poly2(:,1);poly2(:,2)];
%=============================================
gd3 = zeros(size(gd1)); gd3(1) = 1; gd3(4) = 2;
gd = [gd1,gd3];
ns = (char('D','Cext'))';
sf = 'Cext - D';
[dl, bt] = decsg(gd, sf, ns);
model= createpde;
geometryFromEdges(model,dl);
pdegplot(model,'EdgeLabels','on','FaceLabels','on')
% return
applyBoundaryCondition(model,'dirichlet','Edge',(41:44),'h',1,'r',1);
% applyBoundaryCondition(model,'dirichlet','Edge',(1:198),'h',1,'r',1);
% applyBoundaryCondition(model,'dirichlet','Edge',(1:40),'h',1,'r',20);
% applyBoundaryCondition(model,'dirichlet','Edge',(1:40),'h',1,'r',40);
applyBoundaryCondition(model,'dirichlet','Edge',(1:40),'h',1,'r',50);
axis equal
msh = generateMesh(model,'Hmax',0.05,'GeometricOrder','linear','Jiggle','on');
[p,e,t] = meshToPet(msh);
z1 = p(:);
C=1./sqrt(z1(1,:)'.^2+z1(2,:)'.^2);
specifyCoefficients(model, 'm', 0, 'd', 0, 'c', C, 'a', 0, 'f', 0);
resultss = solvepde(model);
u= resultss.NodalSolution;
figure(3)
pdeplot(p, e, t, 'xydata', u, 'contour', 'on', 'mesh', 'off');
axis equal
ux = resultss.XGradients;
uy = resultss.YGradients;
Nee = findNodes(msh,'region','Edge',(41:44));
psi_fr = psi(Nee);
Nx = @(z) 2*z(1,:);
Ny = @(z) 2*z(2,:);
z = p(:,Nee);
nx = Nx(z); nx = nx(:);
ny = Ny(z); ny = ny(:);
Flux11 = (ux(Nee).*nx+uy(Nee).*ny)./sqrt(nx.^2+ny.^2);
Flux1 = Flux11./sqrt(z(1,:)'.^2+z(2,:)'.^2);
flux_initial = [z',Flux1];
save flux_initial.mat flux_initial resultss Nee
Categories
Find more on Tuning, Analysis, and Validation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!