All is correct but this code gives error?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I downloaded a file from the Mathworks file exchange and tried to run it, but it was giving error. Then I commented the function definition line and defined what was used ahead. All seems correct, but when I run it, again it gives error. The piece of code is:
clear all
clc
nvars=4;% By Me
fitnessfun=@main3;
%%if nargin<3 | isempty(options)==1
options=hPSOoptions;
%%end
if size(options.space,1)==1
for i=1:nvars
s(i,:)=options.space;
end
options.space=s;
elseif size(options.space,1)~=nvars
error('The rows of options.space are not equal to nvars.');
end
if size(options.maxv,1)==1
for i=1:nvars
v(i,1)=options.maxv;
end
options.maxv=v;
elseif size(options.maxv,1)~=nvars
error('The rows of options.maxv are not equal to nvars.');
end
c1 = options.c1;
c2 = options.c2;
w = options.w;
maxv = options.maxv;
space = options.space;
popul = options.bees;
flights = options.flights;
HybridIter = options.HybridIter;
Show = options.Show;
StallFliLimit = options.StallFliLimit;
StallTimeLimit = options.StallTimeLimit;
TimeLimit = options.TimeLimit;
Goal = options.Goal;
% Define the options for the hybrid approach
options = optimset('LargeScale','off','Display','off','MaxIter',HybridIter);
% Initial population (random start)
ru=rand(popul,size(space,1));
pop=ones(popul,1)*space(:,1)'+ru.*(ones(popul,1)*(space(:,2)-space(:,1))');
% Hill climb of each solution (bee)
for i=1:popul*sign(HybridIter)
[pop(i,:),fxi(i,1)]=fminsearch(fitnessfun,pop(i,:),options,varargin{:});
end
pop=min(pop,ones(popul,1)*space(:,2)');
pop=max(pop,ones(popul,1)*space(:,1)');
fxi=feval(fitnessfun,pop,varargin{:});
Thre required functions are as below:
function e=main3(b)
u=[-30 0 40 60].';% Desired vector
M=5; %1st constant
N=10; %2nd constant
K=3; 3rd constant
d = 0.5; % 5th constant
vec = @(MAT) MAT(:);
vecH = @(MAT) MAT(:).';
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang))); % Tx steer matrix of size 10 x 3
steerVecR = @(ang) exp(1j*2*pi*d*(0:N-1).'*sin(vecH(ang)));
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
A = ones(K, 1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of yo i.e., observed response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
steerMo = steerMatTR(deg2rad(u), steerVecT, steerVecR);
ro = steerMo*A;
yo = ro;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of ye i.e., estimated y
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
steerMe = steerMatTR(deg2rad(b), steerVecT, steerVecR);
re = steerMe*A;
ye = re;
%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%
abc=0.0;
for m1=1:M*N
abc=abc+(abs(yo(m1,1)-ye(m1,1))).^2;
end
abc=abc/(M*N);
e=abc;
end %%%%%%%%%%% END of our main3s function %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
% function called inside main3
%%%%%%%%%%%%%%%%%%%%%
function steerM = steerMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
steerM = zeros(size(steerA, 1)*size(steerB, 1), length(targetAngle));
for idxK = 1 : 1 : length(targetAngle)
steerM(:, idxK) = kron(steerB(:, idxK), steerA(:, idxK));
end
end
After running the above peice of code, it gives the following erro though all seems ok:
Brace indexing into the result of a function call is not supported. Assign the result of
'varargin' to a variable first, then brace index into it.
Error in hPSO1 (line 94)
fxi=feval(fitnessfun,pop,varargin{:});
2 comentarios
Jan
el 19 de Nov. de 2022
Which FileExchange submission do you mean?
The error occurs inside hPSO1, but this is not part of the posted code and not called also. Please post a copy of the complete error message, not just a part of it.
" All seems correct, but when I run it, again it gives error." :-)
Respuestas (1)
Jan
el 19 de Nov. de 2022
I guess boldly, that the code fails here:
[pop(i,:),fxi(i,1)]=fminsearch(fitnessfun,pop(i,:),options,varargin{:});
...
fxi=feval(fitnessfun,pop,varargin{:});
The posted code seems to be a script. Or did you crop the initial line
function hPSO1
? If so, this was confusing.
The initial clear all will delete all existing variables. On top of a function, this is a crude waste of time. Unfortunately it deletes all loaded functions from the memory also and the reloading is time-consuming also without having any benefits.
What is the meaning of "vargargin{:}" in the two shown lines of code? Is there a variable number of inputs to the shown piece of code (which cannot be a script then)?
By the way, youn simplify
abc=0.0;
for m1=1:M*N
abc=abc+(abs(yo(m1,1)-ye(m1,1))).^2;
end
abc=abc/(M*N);
e=abc;
to
e = mean((yo - ye).^2);
7 comentarios
Walter Roberson
el 21 de Nov. de 2022
function [x,fval,gfx,output] = main(varargin)
[x,fval,gfx,output] = hPSO(@main3, 4, hPSOoptions, varargin{:})
end
Ver también
Categorías
Más información sobre Whos 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!