Error with fminsearch: Undefined function 'david' for input arguments of type 'double'
Afficher commentaires plus anciens
Dear all,
can anyone help me to understand why the following error occured:
Undefined function 'david' for input arguments of
type 'double'.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
The code is :
clear all
clc
% parameter
z=1.2;
w=20;
lam=0.7;
tau=1;
N=1000;
t_min=1;
t_max=4;
M=100;
a_min=0.6;
a_max=0.8;
t=zeros(1,N);
alp=zeros(1,M);
p=zeros(1,M);
p_min=2;
p_max=1;
for i=1:N
t(i)= t_min + (i-1)*(t_max - t_min)/(N-1);
end
for i=1:M
alp(i)= a_min + (i-1)*(a_max - a_min)/(M-1);
p(i)= p_min + (i-1)*(p_max - p_min)/(M-1);
end
fminsearch(@david,p ,z,w,lam,tau,N,M,t,alp);
|*And the function david is:|*
function crit=david(p, z,w,lam,tau,N,M,t,alp)
X = zeros(M,N);
pi = zeros(M,N);
C = zeros(1,N);
Xa=zeros(1,N)
Z=zeros(1,M);
rl=0.01;
rh=1.99;
EXD=140;
while (abs(EXD)>100)
r1=rl + 0.5*(rh-rl);
for i=1:M
for j=1:N
X(i,j)=min(w*(1+lam), (alp(i) * p(i) / r1)^(1/(1-alp(i))) * t(j)^((z-alp(i))/(1-alp(i))));
pi(i,j)=p(i) * t(j)^(z-alp(i)) * X(i,j)^(alp(i)) - r1*X(i,j);
end
end
[C,I] = max(pi);
Xa(1)=X(I(1),1);
for j=2:N
Xa(j)=X(I(j),j);
end
EXD=sum(Xa)- N*w;
if (abs(EXD)>100 && EXD>0)
rl=r1;
elseif (abs(EXD)>100 && EXD<0)
rh=r1;
end
end
Ya=zeros(M,N);
for j=1:N
Ya(I(j),j)=t(j)^(z-alp(I(j))) * X(I(j),j)^(alp(I(j)));
end
Yi=sum(Ya,2);
if (Yi(1)==0)
Z(1)=-50;
end
for j=2:M
if (Yi(j)==0)
Z(j)=-50;
else
Z(j)=(p(1)/p(j))^tau - Yi(j)/Yi(1);
end
end
crit=(sum(abs(Z)));
Réponses (2)
Matt J
le 9 Juil 2013
0 votes
The function "david" is not on your path or in some place where MATLAB can see it.
9 commentaires
Matt J
le 9 Juil 2013
Josef Commented:
Yes, that makes a lot of sense... Sorry for that stupidity. But now I get another error:
Warning: Input arguments must be scalar.
> In david at 3
In fminsearch at 191
Warning: Input arguments must be scalar.
> In david at 4
In fminsearch at 191
Warning: Input arguments must be scalar.
> In david at 7
In fminsearch at 191
Error using david (line 16)
Not enough input arguments.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
What does this imply? That I cannot work with vectors or matrices within the function? What should I do to circumvent this problem?
Matt J
le 9 Juil 2013
To get rid of the warnings and errors, you should call fminsearch as follows
fun=@(p) david(p ,z,w,lam,tau,N,M,t,alp);
p0=p;
fminsearch(fun,p0);
However, I don't think you're going to have very much luck using FMINSEARCH for minimizing over 100 variables. It's really not meant for dealing with more than 6 unknowns or so. You should look into the Optimization Toolbox.
Josef
le 10 Juil 2013
100 unknowns is not huge. Just a lot more than FMINSEARCH can handle.
To know which solver in the Toolbox to use, I'd probably have to understand your optimization problem better, and to do that I would need to see it in equation form, rather than code form. Since you have no constraints, it would in all likelihood use FMINUNC or FSOLVE.
Josef
le 10 Juil 2013
Matt J
le 10 Juil 2013
I'm starting to think that GA in the Global Optimization Toolbox might be better. Your problem doesn't look smooth at all.
Josef
le 10 Juil 2013
1 commentaire
Jan
le 10 Juil 2013
Commented by Jan
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!