MATLAB: I am getting error in last line. i want to find the value of x1,x2 and x3. please help me..

particle swarm

xdata =[10.^(-5) 10.^(-4) 10.^(-3) 10.^(-2) 10.^(-1) 1];
fun = @(x)x(1)./((x(2).*(xdata.^(1+0.9)))+x(3).*(xdata.^(0.9))+1);
rng default % For reproducibility
nvars = 1;
lb = [0.2;0.2; 0.2];
ub = [1; 1; 1];
ydata=(0.2575./((xdata.^2)+(0.333.*xdata)+1));
x = particleswarm(fun,nvars,ydata,lb,ub);

Best Answer

When you use 5 parameters for particleswarm the syntax is
X = particleswarm(FUN,NVARS,LB,UB,OPTIONS)
Your FUN is the function handle fun; it is fine to have a function handle in this position, but see below.
Your NVARS is nvars, 1, which is a problem because you use up to x(3) implying that you have three variables, but it is not the most immediate problem.
Your LB is the vector of length 6, ydata. This will cause a problem because the length of your lower bound needs to be the same as the number of variables, but it is not the most immediate problem.
Your UB is the vector of length 3, lb. That is a confusing variable name to use for an upper bound, but it would be accepted. However, the first 5 of the 6 elements in your lower bound variable, ydata, are all greater than the upper bound 0.2 you establish here, so you will get an error, but it is not the most immediate problem.
Your OPTIONS is the vector of length 3, ub. That is a confusing variable name to use for an options structure. OPTIONS needs to be a struct but ub is a numeric vector. This is your most immediate problem.
Your FUN needs to return a scalar, but fun returns a vector the same size as xdata, which is a vector of length 6. This will cause an error (but OPTIONS is your most immediate problem.)
Your calling sequence is not appropriate for any routine that I can find. The only routines that I can think of in which you would pass in the ydata would be the curve fitting routines, but they have a quite different calling sequence.
If you are trying to do a fitting against a model using particleswarm, then you need to construct the residue yourself:
fun = @(x) sum((x(1)./((x(2).*(xdata.^(1+0.9)))+x(3).*(xdata.^(0.9))+1) - ydata).^2);
and do not pass ydata in the particleswarm calling sequence.