error in fmincon Matlab - matlab

`
function [LLF, LL, H ] = garchlike(data)
p=1;
q=1;
if isempty(q)
m=p;
else
m = max(p,q);
end
%this are the parameters
psi=[0.01;0.01];
gamma=[0;0.02;0.02;0];
lambda=[0;0.03;0.03;0];
A=[1;-0.04;-0.03;1];
stdestimate = std(data,1);
data = [stdestimate ; data];
T = size(data,1);
B=(reshape(A,2,2))^-1;
%I am squaring each element of the inverse of A
B2=((reshape(A,2,2))^-1).^2;
%getting the Bl matrix(squared interactions)
Bl=[B2(1,:);B(1,:).*B(2,:);B2(2,:)];
garchc=Bl*psi;
garcha=Bl*(reshape(gamma,2,2))*reshape(A.^2,2,2);
garchb= Bl*(reshape(lambda,2,2))*reshape(A.^2,2,2);
H(1,1).day=[0.001; 0; 0; 0.002; ];
%convert to matrix
H(1,1).day=ivech(H(1,1).day);
for t = (m + 1):T
H(1,t).day= ivech(garchc + garcha*(diag(H(1,t-1).day))+ garchb*(data((t-1),:).^2)');
end
% Getting the loglikelihood
LLF=zeros(1,T);
%the loklikelihood
for t = (m + 2):T
LLF(t)= log(det(H(1,t).day))+ data(t,:)/(H(1,t).day)*data(t,:)';
end
t=(m+2):T;
LLF=-LLF(t);
LL=sum(LLF);
`
I have this function from up and trying to minimize using fmincon:
` [parameters,LLF,EXITFLAG, OUTPUT,HESSIAN] = fmincon('garchlike', [psi;gamma;lambda;A],sumA ,sumB ,[] , [] , LB , UB,[],options, data, p , q);`
I don't know why is not working and gives me an error :
"Error using garchlike
Error using garchlike
Too many input arguments.
Error in fmincon (line 631)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue."

"Error using garchlike Too many input arguments." is your problem.
fmincon is calling garchlike([psi;gamma;lambda;A],data, p , q), but your garchlike only takes 1 argument.
Pro tip: learn how to use the debugger. Start by setting dbstop if error.
EDIT: okay, here are some more details. Please read carefully.
Your garchlike takes 1 argument as input, while your fmincon is calling it with 4 arguments.
Either (1) you need to remove those 3 extra arguments (namely, data, p, q) from fmincon, or (2) make garchlike take 3 more arguments!!

Related

Optimization with genetic algorithm in matlab

I have written a simple optimization code using genetic algorithm.I don't know why I get error during running the code.Here is my code:
f = #(x1,x2) 1-x1.^2+(x1-x2).^2;
A = [1 1;-1 2;2 1];
b =[2 2 3]' ;
Aeq = [];
beq = [];
Lb = [0 0]';
Ub = [];
[Xopt,Fval] = ga(f,2,A,b,Aeq,beq,Lb,Ub)
I don not know why matlab gives me error.I wrote this programm based on the "Genetic algorithm Documentation" bit still gives me error:
Error using #(x1,x2)1-x1.^2+(x1-x2).^2
Not enough input arguments.
Error in createAnonymousFcn>#(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = #(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 18)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue
Objective functions of all optimization methods in MATLAB only accept 1 argument. According to ga documents:
fun — Objective function
Objective
function, specified as a function handle or function name. Write the
objective function to accept a row vector of length nvars and return a
scalar value.
When the 'UseVectorized' option is true, write fun to accept a
pop-by-nvars matrix, where pop is the current population size. In this
case, fun returns a vector the same length as pop containing the
fitness function values. Ensure that fun does not assume any
particular size for pop, since ga can pass a single member of a
population even in a vectorized calculation.
Change you objective function and it should work:
f = #(x) 1-x(1).^2+(x(1)-x(2)).^2;

fmincon throwing error on input arguments

I am working on an optimization problem where I want to maximize utility while searching over variables lx_init and kx_init. Each time I solve an issue with fmincon another one pops up. The error right now is..
"Not enough input arguments."
"Failure in initial objective function evaluation. FMINCON cannot continue."
I have tried to trace back the error in debug mode but tracing back my error is proving difficult. The function is able to be evaluated on its own and spit back a value so I know the error is in the fmincon function. My Matlab experience is very minimal. I am sure there are other syntax errors I am guilty of and too naive to see. I will provide all variables so the code can be replicated.
A second question: I want to maximize my utility value here but also find the values of x and y given once the utility is maximized. However, we are also maximizing over lx_init and kx_init. How do I get the function to return x and y? Right now it is just returning the utility value.
Here is my function I am optimizing
function [utility, x, y] = utility_with_prod(gamma_X, gamma_Y, alpha_LX, alpha_KX, alpha_LY, alpha_KY, alpha_uX, sigma_U, sigma_X, sigma_Y, L_bar, K_bar, lx_init, kx_init)
% X's production function
x = gamma_X*((alpha_LX*lx_init^((sigma_X-1)/sigma_X)) + ((alpha_KX)*kx_init^((sigma_X-1)/sigma_X)))^(sigma_X/(sigma_X-1));
% Y's production function
y = gamma_Y*((alpha_LY*(L_bar-lx_init)^((sigma_Y-1)/sigma_Y)) + ((alpha_KY)*(K_bar-kx_init)^((sigma_Y-1)/sigma_Y)))^(sigma_Y/(sigma_Y-1));
% utility function with nested production function
utility = -(((alpha_uX*x^((sigma_U-1)/sigma_U)) + ((1-alpha_uX)*y^((sigma_U-1)/sigma_U)))^(sigma_U/(sigma_U-1)));
end
Here are my initial values
sigma_U= 0.5038;
sigma_X= 0.5029;
sigma_Y= 0.5029;
alpha_uX= 0.000236017865342454;
alpha_LX= 0.180813595922536;
alpha_KX= 0.819186404077464;
gamma_X= 1.768587207836113;
alpha_LY= 0.505368332690592;
alpha_KY= 0.494631667309408;
gamma_Y= 1.999942066647923;
lx_init = 1;
kx_init = 2;
L_bar = 3;
K_bar = 3;
x0 = [lx_init, kx_init];
and the optimization
utility = #(lx_init, kx_init)utility_with_prod(gamma_X, gamma_Y, alpha_LX, alpha_KX, alpha_LY, alpha_KY, alpha_uX, sigma_U, sigma_X, sigma_Y, L_bar, K_bar, lx_init, kx_init)
[optimal_lx_kx, utility_max, exitflag] = fmincon(utility, x0, [],[])
x0 in fmincon is a vector, that's an n by 1 matrix or 1 by n,
here 1 by 2 ---> x0 = [lx_init, kx_init];
Function handle #(lx_init, kx_init) is different from #([lx_init, kx_init]).
#([lx_init, kx_init])accepts only one input.
#(lx_init, kx_init) accepts only two inputs, no more, no less
Also input variable should not be predefined value
Change #(lx_init, kx_init) to #(x) where x(1) = lx_init and x(2) = kx_init
To sum up
utility = #(lx_init, kx_init)utility_with_prod(gamma_X, gamma_Y, alpha_LX,alpha_KX, alpha_LY, alpha_KY, alpha_uX, sigma_U, sigma_X, sigma_Y, L_bar, K_bar, lx_init, kx_init)
is replaced by
utility = #(x)utility_with_prod(gamma_X, gamma_Y, alpha_LX, alpha_KX, alpha_LY, alpha_KY, alpha_uX, sigma_U, sigma_X, sigma_Y, L_bar, K_bar, x(1), x(2))

How do I define parameters in a convolution equation?

I am trying to compute the convolution of a sound signal without using the built in conv function but instead using arrays. x is the input signal and h is are the impulse responses. However, when I run my other main function to call onto my_conv I am getting these errors:
Undefined function or variable 'nx'.**
Error in my_conv (line 6)
ly=nx+nh-1;
Error in main_stereo (line 66)
leftchannel = my_conv(leftimp, mono); % convolution of left ear impulse response and mono
This is my function my_conv:
function [y]=my_conv(x,h)
x=x(:);
h=h(:);
lx=length(x);
lh=length(h);
ly=nx+nh-1;
Y=zeros(nh,ny);
for i =1:nh
Y((1:nx)+(i-1),i)=x;
end
y=Y*h;
What changes should I make to fix these errors and get this code running?
I am trying to immplement the function into this code:
input_filename = 'speech.wav';
stereo_filename = 'stereo2.wav';
imp_filename = 'H0e090a.dat';
len_imp = 128;
fp = fopen(imp_filename, 'r', 'ieee-be');
data = fread(fp, 2*len_imp, 'short');
fclose(fp);
[mono,Fs] = audioread(input_filename);
if (Fs~=44100)
end
len_mono = length(mono);
leftimp = data(1:2:2*len_imp);
rightimp = data(2:2:2*len_imp);
leftchannel = my_conv(leftimp, mono);
rightchannel = my_conv(rightimp, mono);
leftchannel = reshape(leftchannel , length(leftchannel ), 1);
rightchannel = reshape(rightchannel, length(rightchannel), 1);
norml = max(abs([leftchannel; rightchannel]))*1.05;
audiowrite(stereo_filename, [leftchannel rightchannel]/norml, Fs);
As pointed out by #SardarUsama in comments, the error
Undefined function or variable 'nx'.
Error in my_conv (line 6) ly=nx+nh-1;
tells you that the variable nx has not been defined before before its usage on the line ly=nx+nh-1. Given the naming of the variables and their usage, it looks like what you intended to do was:
nx = length(x);
nh = length(h);
ny = nx+nh-1;
After making these modifications and solving the first error, you are likely going to get another error telling you that
error: my_conv: operator *: nonconformant arguments
This error is due to an inversion in the specified size of the Y matrix. This can be fixed by initializing Y with Y = zeros(ny, nh);. The resulting my_conv function follows:
function [y]=my_conv(x,h)
nx=length(x);
nh=length(h);
ny=nx+nh-1;
Y=zeros(ny,nh);
for i =1:nh
Y((1:nx)+(i-1),i)=x;
end
y=Y*h;
Note that storing every possible shifts of one of the input vectors in the matrix Y to compute the convolution as a matrix multiplication is not very memory efficient (requiring O(NM) storage). A more memory efficient implementation would compute each element of the output vector directly:
function [y]=my_conv(x,h)
nx=length(x);
nh=length(h);
if (nx < nh)
y = my_conv(h,x);
else
ny=nx+nh-1;
y = zeros(1,ny);
for i =1:ny
idh = [max(i-(ny-nh),1):min(i,nh)]
idx = [min(i,nx):-1:max(nx-(ny-i),1)]
y(i) = sum(x(idx).*h(idh));
end
end
An alternate implementation which can be more computationally efficient for large arrays would make use of the convolution theorem and use the Fast Fourier Transform (FFT):
function [y]=my_conv2(x,h)
nx=length(x);
nh=length(h);
ny=nx+nh-1;
if (size(x,1)>size(x,2))
x = transpose(x);
end
if (size(h,1)>size(h,2))
h = transpose(h);
end
Xf = fft([x zeros(size(x,1),ny-nx)]);
Hf = fft([h zeros(size(h,1),ny-nh)]);
y = ifft(Xf .* Hf);

ode45 not passing arguments (MATLAB)

I'm implementing a filtering algorithm in matlab and having some difficulty with parameter passing.
my dynamics are controlled by the function:
function zdot = VortexDynamics(t, z, vorticity)
for ii=1:length(vorticity),
zdot(ii,1) = 1i/(2*pi) * sum( vorticity([1:ii-1,ii+1:end]) .* ( z(ii) - z([1:ii-1,ii+1:end]) ) ./ (abs(z(ii) - z([1:ii-1,ii+1:end]) ).^2 ) ) ;
end
end
I initially compute the trajectories with ode45, with no issue
[t_true,z_true]=ode45(#VortexDynamics, tspan ,z0_true, OPTIONS, vorticity) ;
To implement the filter, I call a function for each time step:
[z_a , Gam, z_ens] = EnKF_nonlinear(num_tracers, Func_Observation, Observations(:,ii),vorticity, [prev_t t], z_ens, Gam, W,R, VortexDynamics, H, N) ;
EnKF_nonlinear is defined by:
function [z_a, Gam_a, z_ens] = EnKF_nonlinear(num_tracers, Func_Observation, Observations,vorticity, tspan, z0_est, Gam0_est, W,R, dynFunc, H,N )
Within EnKF_nonlinear, I need to integrate the dynamics again with ode45:
for ii = 1:N
[t_out, z_out] = ode45(dynFunc, tspan, z0_est(:,ii), vorticity);
z_ens(:,ii) = z_out(end,:) ;
end
but I get the error:
Error using VortexEnKF>VortexDynamics (line 85)
Not enough input arguments.
Error in VortexEnKF (line 63)
[z_a , Gam, z_ens] = EnKF_nonlinear(num_tracers, Func_Observation,
Observations(:,ii),vorticity, [prev_t t], z_ens, Gam, W,R, VortexDynamics, H,
N) ;
but VortexDynamics only requires t,z,vorticity as parameters. I call ode45 the same way in both functions, but I only get the error when I call it through EnKFnonlinear.
I don't see how its not calling with the right number of parameters. Any ideas?
silly me,
I forgot to include a [] in the ode45 call as a placeholder for the precision options.
ode45(dynFunc, tspan, z0_est(:,ii), [], vorticity);
passes the vorticity parameter to dynFunc, and it works fine.

Fitting model to data in matlab

i have some experimental data and a theoretical model which i would like to try and fit. i have made a function file with the model - the code is shown below
function [ Q,P ] = RodFit(k,C )
% Function file for the theoretical scattering from a Rod
% R = radius, L = length
R = 10; % radius in Å
L = 1000; % length in Å
Q = 0.001:0.0001:0.5;
fun = #(x) ( (2.*besselj(1,Q.*R.*sin(x)))./...
(Q.*R.*sin(x)).*...
(sin(Q.*L.*cos(x)./2))./...
(Q.*L.*cos(x)./2)...
).^2.*sin(x);
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
end
with Q being the x-values and P being the y-values. I can call the function fine from the matlab command line and it works fine e.g. [Q,P] = RodFit(1,0.001) gives me a result i can plot using plot(Q,P)
But i cannot figure how to best find the fit to some experimental data. Ideally, i would like to use the optimization toolbox and lsqcurvefit since i would then also be able to optimize the R and L parameters. but i do not know how to pass (x,y) data to lsqcurvefit. i have attempted it with the code below but it does not work
File = 30; % the specific observation you want to fit the model to
ydata = DataFiles{1,File}.data(:,2)';
% RAdius = linspace(10,1000,length(ydata));
% LEngth = linspace(100,10000,length(ydata));
Multiplier = linspace(1e-3,1e3,length(ydata));
Constant = linspace(0,1,length(ydata));
xdata = [Multiplier; Constant]; % RAdius; LEngth;
L = lsqcurvefit(#RodFit,[1;0],xdata,ydata);
it gives me the error message:
Error using *
Inner matrix dimensions must agree.
Error in RodFit (line 15)
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
i have tried i) making all vectors/matrices the same length and ii) tried using .* instead. nothing works and i am giving the same error message
Any kind of help would be greatly appreciated, whether it is suggestion regading what method is should use, suggestions to my code or something third.
EDIT TO ANSWER Osmoses:
A really good point but i do not think that is the problem. just checked the size of the all the vectors/matrices and they should be alright
>> size(Q)
ans =
1 1780
>> size(P)
ans =
1 1780
>> size(xdata)
ans =
2 1780
>> size([1;0.001]) - the initial guess/start point for xdata (x0)
ans =
2 1
>> size(ydata)
ans =
1 1780
UPDATE
I think i have identified the problem. the function RodFit works fine when i specify the input directly e.g. [Q,P] = RodFit(1,0.001);.
however, if i define x0 as x0 = [1,0.001] i cannot pass x0 to the function
>> x0 = [1;0.001]
x0 =
1.0000
0.0010
>> RodFit(x0);
Error using *
Inner matrix dimensions must agree.
Error in RodFit (line 15)
P = (integral(fun,0,pi/2,'ArrayValued',true))*k+C;
The same happens if i use x0 = [1,0.001]
clearly, matlab is interpreting x0 as input for k only and attempts to multiplay a vector of length(ydata) and a vector of length(x0) which obviously fails.
So my problem is that i need to code so that lsqcurvefit understands that the first column of xdata and x0 is the k variable and the second column of xdata and x0 is the C variable. According to the documentation - Passing Matrix Arguments - i should be able to pass x0 as a matrix to the solver. The solver should then also pass the xdata in the same format as x0.
Have you tried (that's sometimes the mistake) looking at the orientation of your input data (e.g. if xdata & ydata are both row/column vectors?). Other than that your code looks like it should work.
I have been able to solve some of the problems. One mistake in my code was that the objective function did not use of vector a variables but instead took in two variables - k and C. changing the code to accept a vector solved this problem
function [ Q,P ] = RodFit(X)
% Function file for the theoretical scattering from a Rod
% R = radius, L = length
% Q = 0.001:0.0001:0.5;
Q = linspace(0.11198,4.46904,1780);
fun = #(x) ( (2.*besselj(1,Q.*R.*sin(x)))./...
(Q.*R.*sin(x)).*...
(sin(Q.*L.*cos(x)./2))./...
(Q.*L.*cos(x)./2)...
).^2.*sin(x);
P = (integral(fun,0,pi/2,'ArrayValued',true))*X(1)+X(2);
with the code above, i can define x0 as x0 = [1 0.001];, and pass that into RodFit and get a result. i can also pass xdata into the function and get a result e.g. [Q,P] = RodFit(xdata(2,:));
Notice i have changed the orientation of all vectors so that they are now row-vectors and xdata has size size(xdata) = 1780 2
so i thought i had solved the problem completely but i still run into problems when i run lsqcurvefit. i get the error message
Error using RodFit
Too many input arguments.
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
i have no idea why - does anyone have any idea about why Rodfit recieves to many input arguments when i call lsqcurvefit but not when i run the function manual using xdata?