Matlab: fmincon throws error - matlab

I am implementing the expression given in the image which is the log-likelihood for AR(p) model.
In this case, p=2. I am using fmincon as the optimization tool. I checked the documentation and other examples over internet regarding the syntax of this command. Still, I am unable to mitigate the problem. Can somebody please help in eliminating the problem?
The following is the error
Warning: Options LargeScale = 'off' and Algorithm = 'trust-region-reflective' conflict.
Ignoring Algorithm and running active-set algorithm. To run trust-region-reflective, set
LargeScale = 'on'. To run active-set without this warning, use Algorithm = 'active-set'.
> In fmincon at 456
In MLE_AR2 at 20
Error using ll_AR2 (line 6)
Not enough input arguments.
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in MLE_AR2 (line 20)
[theta_hat,likelihood] =
fmincon(#ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot
continue.
The vector of unknown parameters,
theta_hat = [c, theta0, theta1, theta2] where c = intercept in the original model which is zero ; theta0 = phi1 = 0.195 ; theta1 = -0.95; theta2 = variance of the noise sigma2_epsilon.
The CODE:
clc
clear all
global ERS
var_eps = 1;
epsilon = sqrt(var_eps)*randn(5000,1); % Gaussian signal exciting the AR model
theta0 = ones(4,1); %Initial values of the parameters
low_theta = zeros(4,1); %Lower bound of the parameters
up_theta = 100*ones(4,1); %upper bound of the parameters
opts=optimset('DerivativeCheck','off','Display','off','TolX',1e-6,'TolFun',1e-6,...
'Diagnostics','off','MaxIter', 200, 'LargeScale','off');
ERS(1) = 0.0;
ERS(2) = 0.0;
for t= 3:5000
ERS(t)= 0.1950*ERS(t-1) -0.9500*ERS(t-2)+ epsilon(t); %AR(2) model y
end
[theta_hat,likelihood,exit1] = fmincon(#ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);
exit(1,1)=exit1;
format long;disp(num2str([theta_hat],5))
function L = ll_AR2(theta,Y)
rho0 = theta(1); %c
rho1 = theta(2); %phi1
rho2 = theta(3); %phi2
sigma2_epsilon = theta(4);
T= size(Y,1);
p=2;
mu_p = rho0./(1-rho1-rho2); %mean of Y for the first p samples
%changed sign of the log likelihood expression
cov_p = xcov(Y);
L1 = (Y(3:end) - rho0 - rho1.*Y(1:end-1) - rho2.*Y(1:end-2)).^2;
L = (p/2).*(log(2*pi)) + (p/2).*log(sigma2_epsilon) - 0.5*log(det(inv(cov_p))) + 0.5*(sigma2_epsilon^-1).*(Y(p) - mu_p)'.*inv(cov_p).*(Y(p) - mu_p)+...
(T-p).*0.5*log(2*pi) + 0.5*(T-p).*log(sigma2_epsilon) + 0.5*(sigma2_epsilon^-1).*L1;
L = sum(L);
end

You are trying to pass constant parameters to the objective function (Y) in addition to the optimization variables (theta).
The right way of doing so is using anonymous function:
Y = ...; %// define your parameter here
fmincon( #(theta) ll_AR2(theta, Y), theta0, [],[],[],[],low_theta,up_theta,[],opts);
Now the objective function, as far as fmincon concerns, depends only on theta.
For more information you can read about anonymous functions and passing const parameters.

Related

Matlab Pso Algorithm

I'm trying to solve a symbolic optimization problem using PSO optimizer in MATLAB. The variables r x a c n theta z are symbolic and CD is calculated by integrating r.
The CD is the objective function with free variables a,n, theta and lb and ub are bounds. Full code is as follows:
syms r x a c n theta z
assume(n,'positive');
D=0.24;
L=2;
f=L/D;
b=.8;
a0=0.02;
db=0.05;
V=1;
Re=(V*(D/2))/0.000001;
Cf=(0.075/(((log10(Re))-2)^2))+0.00025;
% Define r(x)
c=L-a-b-a0;
r1=0.5*D*(2*x/a)^(1/n);
I1=simplify(int(2*pi*r1,x,a0,a));
r2=D/2;
I2=simplify(int(2*pi*r2,x,a,a+b));
r3=(0.5*D)-((((3*D)/(2*(c)^2))-(tan(theta)/c))*(x-a-b)^2)+(((D/c^3 ...
(tand(theta)/c^2))*(x-a-b)^3);
I3=simplify(int(2*pi*r3,x,a+b,L));
A=simplify(I1+I2+I3);
Sn=pi*(D^2/4);
Cdstar=Cf*(1+(60*f^-3 )+(0.0025*f))*(A/(L^2));
Cdb=0.029*((db/D)^3)*(Cdstar^-0.5)*(Sn/(L^2));
CD=simplify(Cdstar+Cdb);
%optimization problem
objective=matlabFunction(CD,'Vars',[a,n,theta])
nVar=3;
lb = [deg2rad(5),0.25,a0];
ub = [deg2rad(60),5,L/2];
options =
optimoptions('particleswarm','SwarmSize',100,'HybridFcn',#fmincon);
[z,fval,exitflag,output] = particleswarm(objective,nVar,lb,ub,options)
And this is the error I get:
#(a,n,theta)pi.*4.404634153141517e-4+pi.*1.0./sqrt(pi.4.404634153141517e-4-pi.(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3.(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5-(n.pi.((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e-5)./(n+1.0)).9.440104166666668e-7-pi.(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3.(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5-(n.pi.((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e-5)./(n+1.0)
Not enough input arguments.
Error in
symengine>#(a,n,theta)pi.*4.404634153141517e-4+pi.*1.0./sqrt(pi.4.404634153141517e-4-pi.(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3.(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5-(n.pi.((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e-5)./(n+1.0)).9.440104166666668e-7-pi.(a.*5.0-6.0).*1.0./(a.5.0e+1-5.9e+1).^3.(a.*2.32335e+6+tan((theta.*pi)./1.8e+2).*4.779e+6-tan(theta).*6.2658e+6-a.^2.*tan(theta).*2.655125e+7+a.^3.*tan(theta).*1.4875e+7-a.^4.*tan(theta).*3.125e+6-a.*tan((theta.*pi)./1.8e+2).*1.59975e+7+a.*tan(theta).*2.1063e+7+a.^2.*tan((theta.*pi)./1.8e+2).*2.008125e+7-a.^3.*tan((theta.*pi)./1.8e+2).*1.1203125e+7+a.^4.*tan((theta.*pi)./1.8e+2).*2.34375e+6-a.^2.*1.98e+6+a.^3.*5.625e+5-9.08811e+5).*1.223509486983755e-5-(n.pi.((a.*2.5e+1).^(-1.0./n)-2.0.^(1.0./n+1.0).*a.*2.5e+1).*1.101158538285379e-5)./(n+1.0)
Error in particleswarm>makeState (line 694)
firstFval = objFcn(state.Positions(1,:));
Error in particleswarm>pswcore (line 169) state =
makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
Error in particleswarm (line 151) [x,fval,exitFlag,output] =
pswcore(objFcn,nvars,lbRow,ubRow,output,options);
Error in MYRING_SYMS_optimisation_K (line 56) [z,fval,exitflag,output]
= particleswarm(objective,nVar,lb,ub,options)
Caused by:
Failure in initial objective function evaluation. PARTICLESWARM cannot continue.
The fun takes only one argument, which is a vector with nvars elements. From particleswarm doc:
x = particleswarm(fun,nvars) attempts to find a vector x that achieves a local minimum of fun. nvars is the dimension (number of design variables) of fun.
So you need to declare a new objective function that only takes 1 argument:
[z,fval,exitflag,output] = particleswarm( ...
#(x) objective(x(1), x(2), x(3)), ...
nVar,lb,ub,options)

Unable to meet integration tolerances in fmincon to solve ODE optimization problem

I have constrained ODE optimization problem to be solved using Matlab, I started by solving the ODE using ode15s which working will this initial values, also I had very good results without constraints using fminsearch, the problem started when I used fmincon it gave me these warnings:
Warning: Length of lower bounds is < length(x); filling in missing lower bounds with -Inf.
> In checkbounds (line 33)
In fmincon (line 318)
In Optimization (line 64)
Warning: Length of upper bounds is < length(x); filling in missing upper bounds with +Inf.
> In checkbounds (line 47)
In fmincon (line 318)
In Optimization (line 64)
Warning: Failure at t=2.340250e+01. Unable to meet integration tolerances without reducing the step size below the smallest value
allowed (5.684342e-14) at time t.
> In ode15s (line 668)
In ObjFun (line 6)
In barrier
In fmincon (line 813)
In Optimization (line 64)
I tried to remove constraints, but nothing changed...
y1_max = 5; y2_max = 2.3;
y01 = 1; y02 = 1.6;
%Control function parameter
Umin = -0.3; Umax = -0.1;
%Creation the structure of model parameters
params.T0=T0; params.Tf=Tf;
params.y1d=y1d; params.y2d=y2d;
params.y01=y01; params.y02=y02;
params.y2_max=y2_max;
n=2;
U0=-0.2*ones(1,n);
params.n=n;
params.U=U0; params.Umax=Umax; params.Umin=Umin;
params.dt=(Tf-T0)/n;
%for initial value of optimization parameters
options = odeset('RelTol',1e-7,'AbsTol',1e-7);
U=U0;
[t,y]=ode15s(#ODEsolver,[T0,Tf],[y01 y02],options,params);
%adding the ode solution as input prameters
params.t=t; params.y1= y(:,1); params.y2= y(:,2);
U0=-0.2*ones(1,n);
params.n=n; params.U=U0;
params.dt=(Tf-T0)/n;
A = [];
B = [];
Aq = []; Bq = [];
options1 = optimset('MaxIter',5000);
[x,fval,exitflag,output] = fmincon(#ObjFun,U0,A,B,Aq,Bq,Umin,Umax,IneqConst(U0,params),options1,params)
function y = ODEsolver(t,y,params)
dt = params.dt;
nu = floor(t/dt)+1;
nu = min(nu,params.n-1);
t1 = (nu-1)*dt;
Ut = params.U(nu) + ((t-t1)/dt)*(params.U(nu+1) - params.U(nu));
dy1a = ((0.63*y(1))/(1 + y(1)));
dy1b = (Ut*y(1)*y(2))/(1+0.6*y(1));
dy1 = dy1a + dy1b;
dy2a = (0.15*y(2))/(0.05-0.2*y(2));
dy2 = -0.2*y(2) + dy1b * dy2a;
y = [dy1; dy2];
end
function ObjFun1 = ObjFun(u,params)
% Calculating the value of the optimization criteria
params.U=u;
options = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y]=ode15s(#ODEsolver,[0,100],[params.y01,
params.y02],options,params);
ObjFun1 =trapz(t,(y(:,1)-params.y1d).^2);
end
function [c,Const] = IneqConst(u, params)
params.U=u;
options = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y]=ode15s(#ODEsolver,[0,100],[params.y01, params.y02],options,params);
c =[];
yCon = y(:,2)-params.y2_max;
Const = trapz(t,(abs(yCon)+yCon).^2);
end
The bug in this code is that the dimension of the bounds (Umin, Umax) should be an array with the same size of the control function U
so if the control function parameters n = 2 then Umin = [-0.3 -0.3] & Umax = [-0.1 -0.1]

Solving a system of 5 nonlinear equations in Matlab

I'm having difficulty using the fsolve function to solve a set of 5 equations in Matlab.
Here are the 5 equations:
y = a + d + e
y + x = c + d + 2e
2x = 4a + 2b + 2c
k1 = (d * b^3 / (a * c) ) * ((P/Pref)/(a+b+c+d+e))^2
k2 = be/(dc)
y,x,k1,k2,P,Pref are all parameters that I set, but would like to leave them in the function so that I can change them quickly in my code to find new answers. a,b,c,d,e are the variables that I'd like to solve for (they are compositions of a reaction equilibrium equation)
I tried to hard code the parameters in the function, but that didn't work. I'm just not sure what to do. Every thing I change creates a new error. The most common is that the data type has to be "double".
Edit: adding code
first the function:
function F = myfun(Q,I)
a = Q(1);
b = Q(2);
c = Q(3);
d = Q(4);
e = Q(5);
x = I(1);
y = I(2);
k1 = I(3);
k2 = I(4);
P = I(5);
Pref = I(6);
F(1) = a + d + e - y;
F(2) = c + d + 2*e - y - x;
F(3) = 4*a + 2*b + 2*c - 2*x;
F(4) = ((d * b^3)/(a*c))*((P/Pref)/(a+b+c+d+e))^2 - k1;
F(5) = (b*e)/(c*d);
next is the program:
%Q = [a,b,c,d,e]
%I = [x,y,k1,k2,P,Pref]
%The values for the inputs will be changed to vary the output
%Inputs:
x=5;
y=1;
k1=5;
Pref=1;
P=1;
k2=-0.01;
syms K
k1 = solve(log10(k1) - k1);
syms L
k2 = solve(log10(k2) - k2);
x = double(x);
y = double(y);
Pref = double(Pref);
P = double(P);
k1 = double(k1);
k2 = double(k2);
%Solving:
I = [x,y,k1,k2,P,Pref];
q = [0,0,0,0,0]; %initial guess
Q = fsolve(#myfun,[q,I])
when I run this, these errors comes up:
Error using myfun (line 7)
Not enough input arguments.
Error in fsolve (line 218)
fuser = feval(funfcn{3},x,varargin{:});
Error in Coal (line 27)
Q = fsolve(#myfun,[q,I])
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Edit 2: changed the fsolve line, but still got errors:
Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 376)
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in Coal (line 27)
fsolve(#(q) myfun(q,I),q)
Edit3: changed a couple parameters and the initial guess, I am now getting an answer, but it also comes up with this:
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 500 (the default value).
ans =
0.0000 2.2174 3.7473 1.4401 3.8845
how can I get it to not stop prematurely?
Re: how can I get it to not stop prematurely?
Pass non-default options in your call to fsolve.
Refer to the signature, fsolve(myfun,q,options) here,
http://www.mathworks.com/help/optim/ug/fsolve.html
And read about creating the options using optimoptions here,
http://www.mathworks.com/help/optim/ug/optimoptions.html
You should be able to get it to "not stop prematurely" by increasing the values of convergence criteria such as TolFun and TolX.
However, it's advisable to read up on the underlying algorithms you're relying upon to perform this numerical solution here,
(EDIT: I tried to fix non-linky links, but I'm not allowed to provide more than two ... Boo) www.mathworks.com/help/optim/ug/fsolve.html#moreabout
The error you received simply indicates that after 500 function evaluations the algorithm has not yet converged on an acceptable solution conforming to the default solver options. Just increasing MaxFunEvals may allow the algorithm extra iterations needed to converge within default tolerances. For example,
options = optimoptions('MaxFunEvals',1000); % try something bigger than 500
fsolve(#(q) myfun(q,I),q,options);

Create flexible function handle

I am using numerical integration in MATLAB, with one varibale to integrate over but the function also contains a variable number of terms depending on the dimension of my data. Right now this looks like the following for the 2-dimensional case:
for t = 1:T
fxt = #(u) exp(-0.5*(x(t,1)-theta*norminv(u,0,1)).^2) .* ...
exp(-0.5*(x(t,2) -theta*norminv(u,0,1)).^2);
f(t) = integral(fxt,1e-4,1-1e-4,'AbsTol',1e-3);
end
I would like to have this function flexible in the sense that there could be any number of data points in, each in the following term:
exp(-0.5*(x(t,i) -theta*norminv(u,0,1)).^2);
I hope this is understandable.
If x and u have a valid dimension match (vector-vector or array-scalar) for the subtraction, you can put the whole matrix x into the handle and pass it to the integral function using the name-parameter pair ('ArrayValued',true):
fxt = #(u) exp(-0.5*(x - theta*norminv(u,0,1)).^2) .* ...
exp(-0.5*(x - theta*norminv(u,0,1)).^2);
f = integral(fxt,1e-4,1-1e-4,'AbsTol',1e-3,'ArrayValued',true);
[Documentation]
You may need a loop if integral ever passes a vector u into the handle.
But in looking at how the integral function is written, the integration nodes are entered as scalars for array-valued functions, so the loop shouldn't be necessary unless some weird dimension-mismatch error is thrown.
Array-Valued Output
In response to the comments below, you could try this function handle:
fx = #(u,t,k) prod(exp(-0.5*(x(t,1:k)-theta*norminv(u,0,1)).^2),2);
Then your current loop would look like
fx = #(u,t,k) prod(exp(-0.5*(x(t,1:k)-theta*norminv(u,0,1)).^2),2);
k = 2;
for t = 1:T
f(t) = integral(#(u)fx(u,t,k),1e-4,1-1e-4,'AbsTol',1e-3,'ArrayValued',true);
end
The ArrayValued flag is needed since x and u will have a dimension mismatch.
In this form, another loop would be needed to sweep through the k indexes.
However, we can improve this function by skipping the loop altogether since each iterate of the loop is independent by using the ArrayValued mode:
fx = #(u,k) prod(exp(-0.5*(x(:,1:k)-theta*norminv(u,0,1)).^2),2);
k = 2;
f = integral(#(u)fx(u,k),1e-4,1-1e-4,'AbsTol',1e-3,'ArrayValued',true);
Vector-Valued Output
If ArrayValued is not desired, which may be the case if the integration requires a lot of subdivisions and a vector-valued u is preferable, you can also try a recursive version of the handle using cell arrays:
% x has size [T,K]
fx = cell(K,1);
fx{1} = #(u,t) exp(-0.5*(x(t,1) - theta*norminv(u,0,1)).^2);
for k = 2:K
fx{k} = #(u,t) fx{k-1}(u,t).*exp(-0.5*(x(t,k) - theta*norminv(u,0,1)).^2);
end
f(T) = 0;
k = 2;
for t = 1:T
f(t) = integral(#(u)fx{k}(u,t),1e-4,1-1e-4,'AbsTol',1e-3);
end
ThanksTroy but now I run into the follwing:
x = [0.3,0.8;1.5,-0.7];
T = size(x,1);
k = size(x,2);
theta= 1;
fx = #(u,t,k) prod(exp(-0.5*(x(t,1:k) - theta*norminv(u,0,1))^2));
for t = 1,T
f(t) = integral(#(u)fx(u,t,k),1e-4,1-1e-4,'AbsTol',1e-3);
end
Error using -
Matrix dimensions must agree.
Error in #(u,t,k)prod(exp(-0.5*(x(t,1:k)-theta*norminv(u,0,1))^2))
Error in #(u)fx(u,t,k)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);

Using fsolve to solve Differential Equation with Varying Parameters

I am using the following code to produce a numerical solution to a system of ODE's with 6 boundary conditions.
I am using the initial conditions to obtain a solution but I must vary three other conditions in order to find the true solution. The function I have is as follows:
function diff = prob5diff(M,Fx,Fy)
u0 = [pi/2 0 0]';
sSpan = [0 13];
p = #(t,u) prob5(t,u,M,Fx,Fy);
options = odeset('reltol',1e-6,'abstol',1e-6);
[s,u] = ode45(p,sSpan,u0,options);
L = length(s);
x = u(:,2); y = u(:,3); theta = u(:,1);
diff(1) = x(L) - 5;
diff(2) = y(L);
diff(3) = theta(L) + pi/2;
end
Ultimately, different values of M,Fx, and Fy will produce different solutions and I would like a solution such that the values in diff are as close to zero as possible so I want fsolve to iterate through different values of M,Fx, and Fy
I am receiving the following error: when I call it in this way:
opt = optimset('Display','iter','TolFun',1e-6);
guess = [1;1;1];
soln = fsolve(#prob5diff,guess,opt);
Error in line:
soln = fsolve(#prob5diff,guess,opt);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Thanks!
One problem is that you have to call fsolve on prob5diff which takes a single vector input since your guess is a single vector:
prob5diff(x)
M = x(1);
Fx = x(2);
Fy = x(3);