Error with fminsearch: Undefined function 'david' for input arguments of type 'double'

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)));

Answers (2)

The function "david" is not on your path or in some place where MATLAB can see it.

9 Comments

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?
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.
I know that it is not likely to get any results. This is exactly my problem, I need to minimize over that amount of variables. Do you have any idea how I am more likely to get something out? At least I know that p has to be decreasing... But I have a hard time finding any results for more than some 5 variables. That is why I tried fminsearch in the end...
As I said, the Optimization Toolbox will have more robust solvers than FMINSEARCH, and capable of handling many more unknowns. Do you have it?
I have access to it. I never worked with it, the question for me is both which solver to use there, and whether it is able to handle huge amounts of unknowns. Any guess? I will have a look at it now.
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.
I look for a mapping of j (N types with different t) into i (M types with different alp).
The conditions are:
- I(j)=argmax_i(pi(i,j))
where pi(i,j)=p(i) * t(j)^(z-alp(i)) * X(i,j)^(alp(i)) - r1*X(i,j)
and X(i,j)=min(w*(1+lam), (alp(i) * p(i) / r1)^(1/(1-alp(i))) * t(j)^((z-alp(i))/(1-alp(i))));
- sum_j (X(I(j),j)) = N*w;
- (p(1)/p(i))^tau - Yi(1)/Yi(i) = 0 for all types i
where Yi(i)= sum_j II(i,j)*t(j)^(z-alp(i)) * X(i,j)^(alp(i)) with II(j)=1 if i=I(j) and 0 otherwise.
As you can see, in the moment I fix some p(i), search for the r1 that implies [sum_j (X(I(j),j)) = N*w] conditional on I(j)=argmax...., and then I try to update my p(i). I know that p(i) is decreasing in i, as I assume that alp(i) increases in i. Any ideas? Thanks for your help so far, I will take a look at fsolve and fminunc. Problem is my matlab skills are not huge...
I'm starting to think that GA in the Global Optimization Toolbox might be better. Your problem doesn't look smooth at all.
I tried both fsolve and fminunc, they do not get to 0 even with few variables (around 10). Either Matlab says that it has not found 0, but cannot progress in that direction (local minima) or that size of step is smaller than step size tolerance, and has not found a minima.
GA stands for genetic algorithm?

Sign in to comment.

Tags

Asked:

on 9 Jul 2013

Community Treasure Hunt

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

Start Hunting!