Why fmincon did not work when it is provided with gradient
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all; I have a problem with two variables. If the objective function is quadratic and subjected to boundary constraints and dynamic constraints which are consider as a linear constraints. I know the objective function is a function that returns a scalar my question is if i provide the gradient of the objective function which should be a vector why i get this message error.
Warning: Input arguments must be scalar.
> In objfunTetsuro at 27
In @(kc)objfunTetsuro(kc)
In fmincon at 599
In main at 26
??? In an assignment A(:) = B, the number of elements in A and B
must be the same.
Error in ==> fmincon at 599
[initVals.f,initVals.g(:)] = feval(funfcn{3},X,varargin{:});
Error in ==> main at 26
[kc_opt,fopt,flag,output]=fmincon(@(kc)objfunTetsuro(kc),x0,[],[],[],[],lb,ub,[],...
Caused by:
Failure in initial user-supplied objective function evaluation.
FMINCON cannot continue.
where the objective function is
function [val,GObj] = objfunTetsuro(k_c)
load IRI_737b
zdot = calc_zdot(road_x, road_z, 10);
zt=zdot.u;
x00=[0,0,0,0];
opt = odeset('RelTol', 1e-2, 'AbsTol', 1e-3);
[t02,y] = ode23(@(t,x)f(t,x,0, k_c, @(t)lookup_u(zdot,t)), [0 2], x00,opt);
x_frame=y';
val = 0;
R = Weight();
x_frame_zs = zeros(5,length(t02));
xx=size(t02,1);
for i=1:(xx-1)
ddx = ff(t02(i),x_frame(1:4,i), 0, k_c, zt(i));
x_frame_zs(:,i) = [x_frame(:,i); ddx(4)];
val = val+ (t02(i+1)-t02(i))*x_frame_zs(:,i)'*R*x_frame_zs(:,i);
end
if nargout > 1
R_gradient = Weight_gradient(k_c);
GObj = zeros(t02,1); % line 27 which is the line error
for i=1:(t02-1)
GObj((i-1)*4+1:i*4) = 2*(t(i+1)-t(i))*R_gradient*x_frame(:,i);
end
end
end
and fmincon is
[kc_opt,fopt,flag,output]=fmincon(@(kc)objfunTetsuro(kc),x0,[],[],[],[],lb,ub,[],... % line error 28
optimset('GradObj','on','Display','iter','Algorithm','Interior-Point',...
'DiffMinChange',1e-3,'DiffMaxChange',1));
0 Kommentare
Antworten (1)
Walter Roberson
am 4 Jul. 2016
You have
[t02,y] = ode23(@(t,x)f(t,x,0, k_c, @(t)lookup_u(zdot,t)), [0 2], x00,opt);
That is going to result in t02 being a column vector of times within your timespan [0 2].
Then later you have
GObj = zeros(t02,1); % line 27 which is the line error
as if t02 is a scalar integer that is a size. But it is not a scalar and it is not an integer.
Furthermore right after that you have
for i=1:(t02-1)
where again the context would require t02 to be an integer scalar. But it isn't.
Looking at your code, I would suggest it should be
GObj = zeros(xx,1); % line 27 which is the line error
for i=1:(xx-1)
3 Kommentare
Walter Roberson
am 4 Jul. 2016
I would need your complete source and the content of IRI_737b to test.
Siehe auch
Kategorien
Mehr zu Surrogate Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!