fmincon problem, help needed
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
While running the code for optimization of a function, I run into the following error after the line:
x=fmincon(objective,x0,[],[],[],[],lb,ub,nonlincon);
Error using fmincon (line 619)
Supplied objective function must return a scalar value.
It is clear that the objective function is not scalar, but a vector. However I cannot find a solution.
Can anyone help?
The code is listed below.
var=importdata('Variables.txt'); % Import the variables [1,1…,low%,up%, low%, up%, …]
PQ=importdata('f.txt'); % Import i-th FEM frequency matrix, [j,i]: j-th variable, i-th mode
PM=importdata('fexp.txt'); % Import the vector of experimental frequencies {f1,f2,...,fm}
[v,m]=size(PQ); % extrapolation of number of variables (v-1)/2 and number of modes considered m
n=(v-1)/2;
C=ones(2*n+1); % generation of the unit coefficient matrix
for i= 2:2*n % loop to create the matrix containing the combination of the variables: base, low and up
if floor(i/2)*2==i % writing terms on the diagonal
C(:,i)=var(i/2);
C(:,i+1)=var(i/2).^2;
end
end
for i= 2:(2*n+1) % writing off-diagonal terms
if floor(i/2)*2==i
C(i,i)= var(n-1+i);
C(i,i+1)= var(n-1+i)^2;
C(i+1,i)= var(n+i);
C(i+1,i+1)= var(n+i)^2;
end
end
K=zeros(m,2*n+1); % matrix with the coefficients Ci,Ai,Bi
for i= 1:m
PQi=PQ(:,i);
K(i,:)=C\PQi; % calculation of the matrix with constants, (i,:) coefficients (Ci; Ai1; Bi1;... ;Aik; Bik)
% K(i,:) = C\PQi solves the system of linear equations C*K(i,:) = PQi
end
W= (1./PM); % diagonal matrix for normalized frequencies
objective = @(x)(sum(abs((PM-(K*x)).*W))); % objective function to be minimized
x0=ones(2*n+1,1); % Generation of the vector containing the variables
for i= 1:2*n
if floor(i/2)*2==i
x0(i)=var(i/2);
x0(i+1)=var(i/2).^2;
end
end
disp(x0);
disp(['initial Objective:' num2str(objective(x0))]);
disp(K*x0) % calculation of PQi
lb=ones(2*n+1,1); % definition of the lower bounds of the variables
ub=ones(2*n+1,1); % definition of the upper bounds of the variables
for i= 2:2*n
if floor(i/2)*2==i
ub(i,1)=var(n+(i),1);
ub(i+1,1)=var(n+(i),1).^2;
lb(i,1)=var(n-1+(i),1);
lb(i+1,1)=var(n-1+(i),1).^2;
end
end
nonlincon=@nlcon; % The nonlinear constraints defined in the function called "nlcon"
x=fmincon(objective,x0,[],[],[],[],lb,ub,nonlincon); % Application of the optimization algorithm
disp(['final Objective:' num2str(objective(x))]);
PQopt=K*x; % Calculation of optimized PQ parameters
erropt =(PQopt-PM)./PM % Percentage error between the PQopt and the experimental PMs
val_opt=x(2:2:end).*val_ini % Visualization of optimized variables
The function 'nlcon' is listed below:
function [c,ceq]=nlcon(x) % Function containing the nonlinear inequality and equality constraints
c = [];
for i= 2:(2*n) % Substitute the number of variables for n
if floor(i/2)*2==i
ceq(i/2,1)= x(i+1)-x(i).^2;
end
end
4 comentarios
Torsten
el 14 de Dic. de 2022
If this is what you want:
objective = @(x)sum(sum(abs((PM-K*x).*W)).^2); % objective function to be minimized
Respuestas (0)
Ver también
Categorías
Más información sobre Linear Least Squares en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!