"Index in position 1 is invalid"

1 visualización (últimos 30 días)
FEDERICO ZUCCARI
FEDERICO ZUCCARI el 6 de Abr. de 2020
Comentada: FEDERICO ZUCCARI el 7 de Abr. de 2020
Hello,
running this code always returns the error "Index in position 1 is invalid. Array indices must be positive integers or logical values.".
The error is located in the line expressing obj_1.
It's seems to be independent from the for cycles since the same happens if I try to run the line outside.
obj_1 = @(p) 0;
for jj=1:5 %peaks
n = 40;
for ii = (floor(omega_r_idx_1(jj))-n):(floor(omega_r_idx_1(jj))+n) %funzione obiettivo su un intervallo n punti prima e dopo
obj_1 = @(p) obj_1(p) + ((real (H1(freq(ii),5)) -real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+...
2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(freq(ii),5)) -imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-...
freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
end
end
I already tried to evaluate all the parameters like H1, freq and omega_r_idx_1 in the specified indexes and it works.
Could you please help me?
  6 comentarios
FEDERICO ZUCCARI
FEDERICO ZUCCARI el 7 de Abr. de 2020
Editada: FEDERICO ZUCCARI el 7 de Abr. de 2020
I made tests and now the error is still present but it's different and located in the following part of the code where I use fmincon to minimize the function obj_1.
I report the complete section for the minimization.
clearvars jj ii
obj_1 = @(p) 0; %inizializzazione
for jj=1:2 %peaks
n = 2;
for ii = (floor(omega_r_idx_1(jj))-n):(floor(omega_r_idx_1(jj))+n) %funzione obiettivo su un intervallo n punti prima e dopo
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5)) -real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5)) -imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
end
%fmincon setup
a=[];
b=[];
Aeq=[];
beq=[];
ub(jj,:) = [1,(omega_r_1(jj)+10),0,(damping_ratio_1(jj)+0.1),1]; %upper bound
lb(jj,:)=[1e-10,(omega_r_1(jj)-10),-(2*pi)^2,0.001,1e-10]; %lower bound
M(jj) = 1e-09;
K(jj)= 1e-09;
p0_1(jj,:) = [M(jj),omega_r_1(jj),(A_5_1(jj)),damping_ratio_1(jj),K(jj)]; %first guess matrix
% minimization (MESH 1)
options = optimoptions(@fmincon,'Algorithm','sqp','MaxIterations',10000); %set of the max number of iterations
options = optimoptions(options,'PlotFcns',{@optimplotfval}); %live plot of the iteration process
[p_opt,fval]=fmincon(obj_1,p0_1(jj,:),a,b,Aeq,beq,lb(jj,:),ub(jj,:),[],options);
obj_1 = @(p) 0
end
The error is found at the line showing obj_1 but it happens in the fmincon evaluation; I report the error message for clarity:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in CODE_allmeshes_TEST (line 728)
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5))
-real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5))
-imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
Error in CODE_allmeshes_TEST (line 728)
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5))
-real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5))
-imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
Error in CODE_allmeshes_TEST (line 728)
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5))
-real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5))
-imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
Error in CODE_allmeshes_TEST (line 728)
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5))
-real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5))
-imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
Error in CODE_allmeshes_TEST (line 728)
obj_1 = @(p) obj_1(p) + ((real(H1(ii,5))
-real((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2+((imag(H1(ii,5))
-imag((freq(ii)^2*p(jj,1))+p(jj,3)./(p(jj,2).^2-freq(ii)^2+2*1i*p(jj,4)*p(jj,2).*freq(ii))+p(jj,5))))^2;
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in CODE_allmeshes_TEST (line 749)
[p_opt,fval]=fmincon(obj_1,p0_1(jj,:),a,b,Aeq,beq,lb(jj,:),ub(jj,:),[],options); % p_opt cloumns: (M, RESONANCE FREQ, A, DAMPING
RATIOS, K) for acc 5.
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
FEDERICO ZUCCARI
FEDERICO ZUCCARI el 7 de Abr. de 2020
I found the error: since I am addressing the index jj in the parameters p in my function obj_1 I have to provide the full matrix p0_1 and not just the line p0_1(jj,:).
Now it works.
Thanks a lot for your help!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Wireless Communications en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by