Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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);

Risposta accettata

Walter Roberson
Walter Roberson il 29 Mar 2018
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.
  4 Commenti
Walter Roberson
Walter Roberson il 30 Mar 2018
It is possible to work this analytically.
First, if you plot a bunch of points, you can see that the minimum value is when x(2) = 1. So say
syms X1 X3
eqn = fun([X1, 1, X3]);
bestX1form = solve(diff(eqn,X1));
eqn3 = fun([bestX1form,, 1, X3));
bestX3 = solve(diff(eqn3, X3));
vpa(bestX3)
only one of the 18 solutions for bestX3 are within the bounds. On my system it is the 5th of them:
bestX1 = subs(bestX1form, X3, bestX3(5));
and you now have analytic solutions for the minima, [bestX1, 1, bestX3(5)] . fun() at that will be about 4.3E-12 different from the x returned by particleswarm.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by