Error using fmincon, Supplied objective function must return a scalar value.

I have written following matlab code using fmincon function
Please some one help me to solve the following error that I am obtaining while running the code.
Xr=0:0.1:250;
X=load('meta22.txt');
t=X(:,1);
sw=X(:,2);
sws=X(:,3);
hd=X(:,4);
Yr=X(:,6);
Yp = 3.5/2*(1+tanh((3/30*(Xr+15-70))-1.2))-3.5/2*(1+tanh((3/30*(Xr+15-125))-1.5));
Jd = @(x) trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2);
lb = [0.5;0.03;0.6];
ub = [1.2;0.3;1.2];
x0 = [0.6;0.1;0.8];
[x1,fval1] = fmincon(Jd,x0,[],[],[],[],lb,ub);
Error using fmincon
Supplied objective function must return a scalar value.
Gh_f=x1(1);
Td_f=x1(2);
Tp_f=x1(3);
Jdm = fval1;

 Respuesta aceptada

Xr=0:0.1:250;
That is a row vector
Yp = 3.5/2*(1+tanh((3/30*(Xr+15-70))-1.2))-3.5/2*(1+tanh((3/30*(Xr+15-125))-1.5));
This is in terms of a row vector Xr so Yp is a row vector
t=X(:,1);
sw=X(:,2);
sws=X(:,3);
hd=X(:,4);
Yr=X(:,6);
Those are column vectors (not row vectors)
Jd = @(x) trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2);
The sw part is a column vector, the Yp part is a row vector, so you are doing operations on a mix of row vectors and column vectors, so the second parameter to trapz() is a 2D array, and therefore trapz() is going to return a vector.

4 comentarios

Thanks for the reply, according to what you said, should I turn t, sw, sws, hd, Yr, these vectors into row vectors and substitute them into calculations?
Easier would be
Xr = (0:0.1:250).';
to turn Xr into a column vector.
I think there may be other problems with my code, can you please help me again?
Xr = (0:0.1:250).';
X=load('meta22.txt');
t=X(:,1);
sw=X(:,2);
sws=X(:,3);
hd=X(:,4);
Yr=X(:,6);
Yp = 3.5/2*(1+tanh((3/30*(Xr+15-70))-1.2))-3.5/2*(1+tanh((3/30*(Xr+15-125))-1.5));
Jd = @(x) trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2);
lb = [0.5;0.03;0.6];
ub = [1.2;0.3;1.2];
x0 = [0.6;0.1;0.8];
[x1,fval1] = fmincon(Jd,x0,[],[],[],[],lb,ub);
Arrays have incompatible sizes for this operation.

Error in solution>@(x)trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2) (line 9)
Jd = @(x) trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2);

Error in fmincon (line 573)
initVals.f = feval(funfcn{3},X,varargin{:});

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
Gh_f=x1(1);
Td_f=x1(2);
Tp_f=x1(3);
Jdm = fval1;
CH = sprintf('G_h=%f, T_d=%f, T_p=%f',Gh_f,Td_f,Tp_f);
disp(CH);
Xr = (0:0.1:250).';
Xr is a fixed length.
X=load('meta22.txt');
X and derived variables are whatever size happens to be in the file.
Yp = 3.5/2*(1+tanh((3/30*(Xr+15-70))-1.2))-3.5/2*(1+tanh((3/30*(Xr+15-125))-1.5));
Yp is derived from Xr, so it is the fixed size.
sw is derived from the input, so it will typically be a different size than the fixed-length Xr.
Jd = @(x) trapz(t,(sw+x(2).*sws-x(1).*(Yp-Yr-15.*x(3).*hd)).^2);
You have operations that combine sw (size determined by file) with Yp (fixed size). Those are incompatible.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2019b

Preguntada:

el 19 de Abr. de 2023

Comentada:

el 19 de Abr. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by