You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why unifrnd(lb,ub,[nPop,D]) gives error?
7 views (last 30 days)
Show older comments
Why the following code gives error?
lb=[0 0 0 0]; ub=[10 10 pi pi]; nPop=30; D=4;
unifrnd(lb,ub,[nPop,D])
Error using unifrnd
Size information is inconsistent.
Answers (1)
Ameer Hamza
on 26 Dec 2020
This cannot be done in single call to unifrnd. You need a for-loop
lb = [0 0 0 0]; ub=[10 10 pi pi]; nPop=30;
D = numel(lb);
M = rand(nPop, D);
for i = 1:D
M(:,i) = unifrnd(lb(i),ub(i),[nPop,1]);
end
31 Comments
Sadiq Akbar
on 26 Dec 2020
Thank you very much dear Ameer Hamza. But now it gives another error. Can you help me to run the following algorithm for my fitness function given below?
function [xmin,fmin,histout] = GQPSO(fun,D,nPop,lb,ub,maxit,maxeval)
% INPUT:
% fun : function handle for optimization
% D : problem dimension (number of variables)
% nPop : number of particles in the swarm
% lb : lower bound constrain
% ub : upper bound constrain
% maxit : max number of iterations
% maxeval : max number of function evaluations
% OUTPUT:
% xmin : best solution found
% fmin : function value at the best solution, f(xmin)
% histout : record of function evaluations and fitness value by iteration
% EXAMPLE:
% fun = @griewankfcn;
% D = 30;
% nPop = 50;
% lb = -600;
% ub = 600;
% maxit = 1000;
% maxeval = 10000*D;
% [xmin,fmin,histout] = GQPSO(fun,D,nPop,lb,ub,maxit,maxeval);
% OR DIRECTLY:
% [xmin,fmin,histout] = GQPSO(@griewankfcn,30,50,-600,600,1000,10000*30);
% QPSO parameters:
w1 = 0.5;
w2 = 1.0;
c1 = 1.5;
c2 = 1.5;
nPop=30; % By me
% Initializing solution
% x = unifrnd(lb,ub,[nPop,D]); %This line gave error, so asked question on
%Mathworks site and see below
% Correction by Ameer Hamza sab from Mathworks site
x = rand(nPop, D);
for i = 1:D
x(:,i) = unifrnd(lb(i),ub(i),[nPop,1]);
end
% Evaluate initial population
pbest = x;
histout = zeros(maxit,2);
f_x = feval(fun,x);
fval = nPop;
f_pbest = f_x;
[~,g] = min(f_pbest);
gbest = pbest(g,:);
f_gbest = f_pbest(g);
it = 1;
histout(it,1) = fval;
histout(it,2) = f_gbest;
while it < maxit && fval < maxeval
alpha = (w2 - w1) * (maxit - it)/maxit + w1;
mbest = sum(pbest)/nPop;
for i = 1:nPop
fi = abs(randn(1,D)); % Gaussian QPSO (Coelho, 2010)
p = (c1*fi.*pbest(i,:) + c2*(1-fi).*gbest)/(c1 + c2);
u = abs(randn(1,D)); % Gaussian QPSO (Coelho, 2010)
b = alpha*abs(x(i,:) - mbest);
v = log(1./u);
if rand < 0.5
x(i,:) = p + b .* v;
else
x(i,:) = p - b .* v;
end
% Keeping bounds
x(i,:) = max(x(i,:),lb);
x(i,:) = min(x(i,:),ub);
f_x(i) = fun(x(i,:));
fval = fval + 1;
if f_x(i) < f_pbest(i)
pbest(i,:) = x(i,:);
f_pbest(i) = f_x(i);
end
if f_pbest(i) < f_gbest
gbest = pbest(i,:);
f_gbest = f_pbest(i);
end
end
it = it + 1;
histout(it,1) = fval;
histout(it,2) = f_gbest;
end
xmin = gbest;
fmin = f_gbest;
histout(it+1:end,:) = [];
figure
semilogy(histout(:,1),histout(:,2))
title('Gaussian Q-PSO')
xlabel('Function evaluations')
ylabel('Fitness')
grid on
end
And my fitness function is:
function out=myfitness(b)
u=[1 2 3 4];
v=[b(1) b(2) b(3) b(4)];
out1=u-v
out=sum(out1);
end
When I type the followiong in command window, it does not run
>>[xmin,fmin,histout] = GQPSO(@myfitness,4,30,[0 0 0 0],[10 10 pi pi],100,10000*4)
Walter Roberson
on 26 Dec 2020
What error do you encounter?
Sadiq Akbar
on 26 Dec 2020
Thank you very much for your response. When I type the above in command window, I encounter the following error:
>> [xmin,fmin,histout] = GQPSO(@myfitness,4,30,[0 0 0 0],[10 10 pi pi],100,10000*4)
Attempted to access f_pbest(2); index out of bounds because numel(f_pbest)=1.
Error in GQPSO (line 99)
if f_x(i) < f_pbest(i)
Walter Roberson
on 26 Dec 2020
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to return a vector which is the same size as the population size. Instead your fitness function expects a vector of four elements as input and returns a scalar.
function out=myfitness(b)
u = [1 2 3 4];
out1 = u-b; %needs R2016b or later
out = sum(out1.^2,2);
end
Sadiq Akbar
on 26 Dec 2020
Thank you dear Walter Roberson for your 2nd response. But still it gives the following error after your changes:
[xmin,fmin,histout] = GQPSO(@myfitness,4,30,[0 0 0 0],[10 10 pi pi],100,10000*4)
Index exceeds matrix dimensions.
Error in GQPSO (line 101)
if f_x(i) < f_pbest(i)
Walter Roberson
on 27 Dec 2020
No error when I tried with the GQPSO you posted in https://www.mathworks.com/matlabcentral/answers/702837-why-unifrnd-lb-ub-npop-d-gives-error#comment_1228747 and the myfitness I posted in https://www.mathworks.com/matlabcentral/answers/702837-why-unifrnd-lb-ub-npop-d-gives-error#comment_1229062
Sadiq Akbar
on 27 Dec 2020
Thanks a lot for your 3rd response dear Walter Roberson. But why does it give the above error to me though I am running it in Matlab R2016b?
Sadiq Akbar
on 27 Dec 2020
Even I ran it for this fitness function also, but it gives me an error:
function e=obj(b)
global u
th=pi/180;
u=[2 7 50*th 85*th]; % angles of all four vectors
%%%%%%%%%%%%%%%
% Swapping vector b. Taken help from Mathworks site
%%%%%%%%%%%%%%%
[~, ix] = sort(u);
[~, ix1(ix)] = sort(b);
b = b(ix1);
[~,C]=size(b);
P=C/2;
M=2*C;
%M=10;
xo=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cos(u(P+i)));
end
end
xe=zeros(1,M);
for k=1:M
for i=1:P
xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cos(b(P+i)));
end
end
abc=0.0;
for m1=1:M
abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
end
abc=abc/M;
e=abc;
And stil it gives me the following error:
Subscripted assignment dimension mismatch.
Error in obj (line 12)
[~, ix1(ix)] = sort(b);
Error in GQPSO (line 56)
f_x = feval(fun,x);
Walter Roberson
on 27 Dec 2020
I see no problem in R2020b with the original code.
Could you confirm you are using R2016b and not R2016a ? The fitness function I posted needs R2016b or later.
[xmin,fmin,histout] = GQPSO(@myfitness,4,30,[0 0 0 0],[10 10 pi pi],100,10000*4)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/471407/image.png)
xmin = 1×4
0.9174 1.6567 3.1416 3.1416
fmin = 0.8816
histout = 100×2
30.0000 9.6990
60.0000 5.5803
90.0000 1.2189
120.0000 1.2189
150.0000 1.2189
180.0000 1.2189
210.0000 1.2189
240.0000 1.2189
270.0000 1.2189
300.0000 1.2189
●
function out=myfitness(b)
u = [1 2 3 4];
out1 = u-b; %needs R2016b or later
out = sum(out1.^2,2);
end
function [xmin,fmin,histout] = GQPSO(fun,D,nPop,lb,ub,maxit,maxeval)
% INPUT:
% fun : function handle for optimization
% D : problem dimension (number of variables)
% nPop : number of particles in the swarm
% lb : lower bound constrain
% ub : upper bound constrain
% maxit : max number of iterations
% maxeval : max number of function evaluations
% OUTPUT:
% xmin : best solution found
% fmin : function value at the best solution, f(xmin)
% histout : record of function evaluations and fitness value by iteration
% EXAMPLE:
% fun = @griewankfcn;
% D = 30;
% nPop = 50;
% lb = -600;
% ub = 600;
% maxit = 1000;
% maxeval = 10000*D;
% [xmin,fmin,histout] = GQPSO(fun,D,nPop,lb,ub,maxit,maxeval);
% OR DIRECTLY:
% [xmin,fmin,histout] = GQPSO(@griewankfcn,30,50,-600,600,1000,10000*30);
% QPSO parameters:
w1 = 0.5;
w2 = 1.0;
c1 = 1.5;
c2 = 1.5;
nPop=30; % By me
% Initializing solution
% x = unifrnd(lb,ub,[nPop,D]); %This line gave error, so asked question on
%Mathworks site and see below
% Correction by Ameer Hamza sab from Mathworks site
x = rand(nPop, D);
for i = 1:D
x(:,i) = unifrnd(lb(i),ub(i),[nPop,1]);
end
% Evaluate initial population
pbest = x;
histout = zeros(maxit,2);
f_x = feval(fun,x);
fval = nPop;
f_pbest = f_x;
[~,g] = min(f_pbest);
gbest = pbest(g,:);
f_gbest = f_pbest(g);
it = 1;
histout(it,1) = fval;
histout(it,2) = f_gbest;
while it < maxit && fval < maxeval
alpha = (w2 - w1) * (maxit - it)/maxit + w1;
mbest = sum(pbest)/nPop;
for i = 1:nPop
fi = abs(randn(1,D)); % Gaussian QPSO (Coelho, 2010)
p = (c1*fi.*pbest(i,:) + c2*(1-fi).*gbest)/(c1 + c2);
u = abs(randn(1,D)); % Gaussian QPSO (Coelho, 2010)
b = alpha*abs(x(i,:) - mbest);
v = log(1./u);
if rand < 0.5
x(i,:) = p + b .* v;
else
x(i,:) = p - b .* v;
end
% Keeping bounds
x(i,:) = max(x(i,:),lb);
x(i,:) = min(x(i,:),ub);
f_x(i) = fun(x(i,:));
fval = fval + 1;
if f_x(i) < f_pbest(i)
pbest(i,:) = x(i,:);
f_pbest(i) = f_x(i);
end
if f_pbest(i) < f_gbest
gbest = pbest(i,:);
f_gbest = f_pbest(i);
end
end
it = it + 1;
histout(it,1) = fval;
histout(it,2) = f_gbest;
end
xmin = gbest;
fmin = f_gbest;
histout(it+1:end,:) = [];
figure
semilogy(histout(:,1),histout(:,2))
title('Gaussian Q-PSO')
xlabel('Function evaluations')
ylabel('Fitness')
grid on
end
Walter Roberson
on 27 Dec 2020
With regard to your new fitness function obj : As I posted above:
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to return a vector which is the same size as the population size.
Now let us examine your code:
u=[2 7 50*th 85*th]; % angles of all four vectors
Okay, that is a vector of 4 elements.
%%%%%%%%%%%%%%%
% Swapping vector b. Taken help from Mathworks site
%%%%%%%%%%%%%%%
[~, ix] = sort(u);
That is going to return the vector [3 4 1 2] into ix.
[~, ix1(ix)] = sort(b);
There you are asking to sort a 2D array with 30 rows and 4 columns. The sort information for that is going to require 30 x 4 output locations, but ix1(ix) would be a vector of 4 locations. Also, sort() by default operates along the first non-singleton dimension, which would be the first dimension for a 30 x 4 array, but it seems more likely that you want to sort along the rows, which would be the second dimension, seeing as you expect an array of length 4.
For this step I would suggest instead
[~, ix1] = sort(b, 2);
ix1 = ix1(:,ix);
which would give you a 2D array, in which each row talks about the sort order of b for its corresponding row
Then
b = b(ix1);
is a problem as it does not do per-row reordering.
Now.. what really is that code trying to do??
Let us experiment:
th=pi/180
u=[2 7 50*th 85*th]
[~, ix] = sort(u)
b = [4 1 3 2]
[~, ix1] = sort(b)
ix1 = ix1(:,ix)
new_b = b(ix1)
b_2 = [0 17 -11 -6]
[~, ix1_2] = sort(b_2)
ix1_2 = ix1_2(:,ix)
new_b_2 = b_2(ix1)
ix comes out as [3 4 1 2]
new_b comes out as [3 4 1 2]
new_b_2 comes out as [0 17 -11 -6]
t = sort([b;b_2],2)
t(:,ix)
comes out as
3 4 1 2
0 17 -11 -6
so it looks to me as if it wants to sort b per-row and then re-order according to the sorting of u. In that case I would suggest
th=pi/180;
u=[2 7 50*th 85*th];
[~, ix] = sort(u);
sort_b = sort(b,2);
b = sort_b(:,ix);
Sadiq Akbar
on 27 Dec 2020
Thank you very much dear Walter Roberson for your 4th response. Yes indeed I am using Matlalab R2016b. This time I replaced the swapping section by your above code, i.e.
function e=obj(b)
global u
th=pi/180;
u=[2 7 50*th 85*th]; % angles of all four vectors
%%%%%%%%%%%%%%%
% Swapping vector b. Taken help from Mathworks site
%%%%%%%%%%%%%%%
[~, ix] = sort(u);
sort_b = sort(b,2);
b = sort_b(:,ix);
[~,C]=size(b);
P=C/2;
M=2*C;
%M=10;
xo=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cos(u(P+i)));
end
end
xe=zeros(1,M);
for k=1:M
for i=1:P
xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cos(b(P+i)));
end
end
abc=0.0;
for m1=1:M
abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
end
abc=abc/M;
e=abc;
Again I ran it but this time it gives the following error:
Index exceeds matrix dimensions.
Error in GQPSO (line 60)
f_pbest = min(f_x(i));
Sadiq Akbar
on 27 Dec 2020
Further, dear Walter Roberson!
I have applied my fitness function on several algorithms. it never gave me any problem. But with this it gives error. So I would request if you can do changes in the algorithm instead of doing changes in my fitness function, it will be very good. Because if my fitness function changes, then it will give problem with other algorithms. I mean to say that there has been no problem in my fitness function for several algorithms till now, but it gives error for this. so if you change it, then again it will give problem for other algorithms. So my request is please do changes in the given algorithm so that it can run my fitness function. I hope you understand. Thank you once again.
Walter Roberson
on 27 Dec 2020
As I posted twice before:
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to return a vector which is the same size as the population size.
Your obj is returning a scalar, not a vector of length 30.
It is not possible to change the algorithm to account for the objective function returning the wrong results.
Walter Roberson
on 27 Dec 2020
xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cos(b(P+i)));
How many rows does b have? How many rows of b does that line access?
Where is your code producing one output for each row in b?
Sadiq Akbar
on 27 Dec 2020
Edited: Sadiq Akbar
on 27 Dec 2020
Thanks a lot for your prompt response dear Walter Roberson!
In my fitness function "u" is my desired vector and every algorithm sends one vector of the same size as my u because i give dimension of my u vector to that algorithm. Then that algorithm generates random population of 30 or 50 depends how much i set there. Then out of that population, the algorithm sends one memeber to my algorithm and fitness is determined and saved in a variable there.Then the algorithm sends next memebr of that population to my fitness function and again fitness of that is found and is saved in that old varibale and so on. So up till now my fitness function runs with all the algorithms, but it gives problem here. So can't you make such changes that the algorithm sends one row vector instead of entire population to my fitness funtion as before with other algorithms?
As my u vector is a 4 element row vector, so the algorithm must send "b" the same size as my vector u. Then it will work.
Walter Roberson
on 27 Dec 2020
Read your code!!
x = rand(nPop, D);
for i = 1:D
x(:,i) = unifrnd(lb(i),ub(i),[nPop,1]);
end
So x is the full matrix, not one row of the matrix.
f_x = feval(fun,x);
And right there you call your function on the entire x matrix.
Your objective function must return one value for each population member.
Sadiq Akbar
on 27 Dec 2020
Thanks for very prompt response. You are absolutely right dear. I understand your reason. But I want to do some changes in the given algorithm so that the algorithm sends one row of the full matrix x in one time, fitness is found, stored, then algorithm sends 2nd row of the full matrix x, finds fitness, stores it and so on.
Sadiq Akbar
on 27 Dec 2020
something like this:
% Initialize the population/solutions
for i=1:n,
Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
Fitness(i)=obj(Sol(i,:)); % My fitness function
end
This is some portion I took from some other algorithm to show you.
Walter Roberson
on 27 Dec 2020
% Initialize the population/solutions
for i=1:n,
Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
f_x(i)=obj(Sol(i,:)); % My fitness function
end
Sadiq Akbar
on 27 Dec 2020
You mean to say that if I put this code in given algorithm, it will run with my fitness function? If yes, then what portion of the given algorithm should I replace with this above code?
Walter Roberson
on 27 Dec 2020
remove
f_x = feval(fun,x);
insert
for i = n
f_x(i) = feval(fun,x(i, :))
end
Sadiq Akbar
on 27 Dec 2020
Thanks a lot for your continuous help. I did what you told and ran it, but this time it gives the following error:
Undefined function or variable 'n'.
Error in GQPSO (line 59)
for i =n
Walter Roberson
on 27 Dec 2020
for i = nPop
f_x(i) = feval(fun,x(i, :))
end
Sadiq Akbar
on 27 Dec 2020
Thanks for the correction.Now it ran partially as:
f_x =
Columns 1 through 9
0 0 0 0 0 0 0 0 0
Columns 10 through 18
0 0 0 0 0 0 0 0 0
Columns 19 through 27
0 0 0 0 0 0 0 0 0
Columns 28 through 30
0 0 81.9367
Index exceeds matrix dimensions.
Error in GQPSO (line 106)
if f_x(i) < f_pbest(i)
Walter Roberson
on 27 Dec 2020
for i = 1:nPop
f_x(i) = feval(fun,x(i, :))
end
Sadiq Akbar
on 27 Dec 2020
Thanks for your constant support dear. After running it gives the following:
f_x =
65.4690
f_x =
65.4690 144.7119
f_x =
65.4690 144.7119 29.7280
f_x =
65.4690 144.7119 29.7280 45.6022
f_x =
65.4690 144.7119 29.7280 45.6022 177.2501
f_x =
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253
f_x =
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983
f_x =
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193
f_x =
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Column 10
121.0852
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 11
121.0852 141.2146
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 12
121.0852 141.2146 179.5222
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 13
121.0852 141.2146 179.5222 63.1976
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 14
121.0852 141.2146 179.5222 63.1976 124.7700
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 15
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 16
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 17
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Column 19
158.4781
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 20
158.4781 247.8008
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 21
158.4781 247.8008 52.9352
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 22
158.4781 247.8008 52.9352 143.4848
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 23
158.4781 247.8008 52.9352 143.4848 99.6341
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 24
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 25
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 26
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893 78.8733
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 27
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893 78.8733 90.7319
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 27
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893 78.8733 90.7319
Column 28
94.8255
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 27
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893 78.8733 90.7319
Columns 28 through 29
94.8255 136.7698
f_x =
Columns 1 through 9
65.4690 144.7119 29.7280 45.6022 177.2501 65.6253 149.4983 184.3193 110.8635
Columns 10 through 18
121.0852 141.2146 179.5222 63.1976 124.7700 110.8142 165.7112 86.9005 117.7920
Columns 19 through 27
158.4781 247.8008 52.9352 143.4848 99.6341 11.2423 67.4893 78.8733 90.7319
Columns 28 through 30
94.8255 136.7698 152.8737
Index exceeds matrix dimensions.
Error in GQPSO (line 106)
if f_x(i) < f_pbest(i)
>>
Walter Roberson
on 27 Dec 2020
I am not sure on the index out of range. I will look at it after I sleep as I am not at my desk.
Sadiq Akbar
on 27 Dec 2020
ok dear. Good night. I will wait for you. Thanks for your help.
Walter Roberson
on 28 Dec 2020
Code seems to be working for me.
Invoke obj_driver or myfit_driver as applicable.
Sadiq Akbar
on 28 Dec 2020
Thanks a lot dear Walter Roberson for your efforts. I ran it. Now its working. I get the graph. But when I removed the semicolons from "xmin" and "fmin" to see their values.In this xmin is the estimated vector which must nearly resemble my u vectro in the "obj.m" file. But I see that its not the same as my u vector. A lot of variations in the values and sometimes their values changes positions as well. As you can see my different runs:
>> myfit_driver
xmin =
0.9932 2.1081 2.9842 3.1416
fmin =
0.7489
>> myfit_driver
xmin =
0.9396 1.9000 3.0854 3.1416
fmin =
0.7578
>> myfit_driver
xmin =
0.9186 1.9747 2.9763 3.1416
fmin =
0.7447
>> myfit_driver
xmin =
1.0536 2.0371 3.1416 3.1416
fmin =
0.7612
>> myfit_driver
xmin =
1.0377 2.0226 2.8995 3.1416
fmin =
0.7489
>> myfit_driver
xmin =
0.9598 1.9679 2.8777 3.1416
fmin =
0.7545
>> myfit_driver
xmin =
1.0721 1.8975 3.0554 3.1416
fmin =
0.7556
>> myfit_driver
xmin =
1.0148 1.8979 2.9469 3.1416
fmin =
0.7503
>> myfit_driver
xmin =
0.9659 2.0040 3.1416 3.1416
fmin =
0.7581
>> myfit_driver
xmin =
0.9647 2.0892 3.0875 3.1416
fmin =
0.7537
>> myfit_driver
xmin =
0.8858 1.9868 3.0025 3.1416
fmin =
0.7501
>> obj_driver
xmin =
1.7087 5.7792 1.4902 0.8463
fmin =
1.9548
>> obj_driver
xmin =
3.0668 5.6929 0.8875 1.4876
fmin =
3.0838
>> obj_driver
xmin =
1.4781 5.9054 1.5567 0.8601
fmin =
1.7798
>> obj_driver
xmin =
2.4717 8.4429 0.8501 1.4941
fmin =
3.8352
>> th=pi/180;
u=[2 7 50*th 85*th]
u =
2.0000 7.0000 0.8727 1.4835
>> obj_driver
xmin =
1.4817 7.0154 0.9122 1.5292
fmin =
0.7388
>> obj_driver
xmin =
6.6405 2.0153 0.8459 1.4795
fmin =
0.5694
>>
Now what to do dear?
Sadiq Akbar
on 28 Dec 2020
Further, when i rant it with my own old fitness function (before your chnages), again it gave error as:
Subscripted assignment dimension mismatch.
Error in fun2sn0 (line 13)
[~, ix1(ix)] = sort(b); % temp stores the randomly generated vector "best" by algorithm
Error in GQPSO (line 54)
f_x(i) = feval(fun,x(i, :));
Error in myfit_driver (line 2)
[xmin,fmin,histout] = GQPSO(@fun2sn0,4,30,[0 0 0 0],[10 10 pi pi],2000,10000*4);
See Also
Categories
Find more on Distributed Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)