Matlab - MultiStart gets stuck using fmincon - matlab

I am currently trying to run a minimum-distance estimation of a dynamic search model with the MultiStart routine and fmincon and a bounded parameter space. My problem right now is that the routine gets stuck at a random point of the estimation, i.e. at some point one of the iterations gets stuck in an infinite loop or so and nothing moves forward.
My problem structure is the following:
problem = createOptimProblem('fmincon',...
'objective',#(x)objective(x,fixedInput),...
'x0',x_0,...
'lb',[0 0 0 0 0.001 0], 'ub',[50 1500 30 79 1 1],...
optimoptions(#fmincon,'Algorithm','interior-point','Display','off','UseParallel',true,));
ms = MultiStart('UseParallel',true,'Display','iter','StartPointsToRun','bounds');
The objective function has as output the weighted distance between supplied empirical moments and parametric counterparts of the dynamic model that I aim to fit to the data.
I run into the same problem no matter which algorithm I choose for the fmincon problems (though the process gets stuck much later when I use active-set instead of interior-point or sqp). I know that MultiStart has the MaxTime option, but this is not convenient in my case as I want to run the estimation on a large set of starting values (~2000) and the whole estimation stops too early then.
Does anyone know about a way to circumvent this problem?
Thanks a lot in advance!
Best,
banan
EDIT:
I get the following output when interrupting the routine:
Operation terminated by user during search(line 110)
In objective(line 42)
s_par = search(x, fixed);
In parallel_function>#(x)objective(x,fixedInput)
In finDiffEvalAndChkErr
In parfinitedifferences
In parallel_function (line 478)
consume(base, limit, F(base, limit, supply(base, limit)));
In parfinitedifferences
In nlconst (line 347)
parfinitedifferences(XOUT,lb,ub,f, ...
In fmincon (line 750)
nlconst(funfcn,X,l,u,full(A),B,full(Aeq),Beq,confcn,options,defaultopt, ...
In fmultistart
In fmultistart
In parallel_function>make_general_channel/channel_general (line 914)
O = F(C{:});
In remoteParallelFunction (line 38)
out = parallel.internal.pool.serialize(feval(channel, channelArgs{:}));
Operation terminated by user during distcomp.remoteparfor/getCompleteIntervals (line 127)
In parallel_function>distributed_execution (line 820)
[tags, out] = P.getCompleteIntervals(chunkSize);
In parallel_function (line 587)
R = distributed_execution(...
In fmultistart
In MultiStart/run (line 268)
fmultistart(problem,startPointSets,msoptions);
In MinDist_HM (line 42)
[theta_est,fval,exitflag,outp,manymins] = run(ms,problem,2000)

Related

Solving a system of non linear equations with 3 variables - Error in fsolve (line 230)

Hello im a total matlab beginner and barely know any programming but i gotta solve a certain system of equations and thus resorted to this. So far i've created this script:
function F=caudales(a)
x = a(1);
y = a(2);
z = a(3);
F(1)=6.078*10^-4*z^1.85+8.864*10^-9*x^1.85-0.01078
F(2)=9.018*10^-8*y^1.85+8.864*10^-9*x^1.85-0.009078
F(3)=y+z-x
end
and saved it with the recommended name "caudales"
now on the workspace i think is called i've wrote this:
ag
ag =
1
1
1
>> a = fsolve(#caudales,ag)
yet i get the following error:
Undefined function or variable 'caudales'.
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Okay, thanks to a commenter i learnt that the issue was that fsolve was not running in the same window were i had saved the script for the functions, and thus it could not find it.
I had to modify the current folder of the command window and then the function
ran perfectly.

Invalid setting for parameter 'Gain'

I am trying to create a simple controller, but keep receiving an error for the gain block stating that there is an undefined variable 'u'. I do not understand Simulink well and I am just trying to replicate an old homework problem right now. The code I have here was provided as a solution, but I still receive the error when I try to run it. Any insights as to what might be going on?
I= 10; Wl= 5; k= 2; J= 1;
%set initial conditions
thetaIC= 0; phiIC= 0; x0= zeros(4,1);
%fix theta= 0, check output
[xe, ue]= trim('Ex3_System',x0,0,x0,1)
[A,B,~,~]= linmod('Ex3_System', xe, ue)
%choose your desired poles
p= linspace(-1.2,-1.5,4)
%recall the minus sign
K= -acker(A,B,p)
%perturb initial condition
thetaIC= deg2rad(5);
sim('Ex3_Controller');
Invalid setting in 'Ex3_Controller/Gain' for parameter 'Gain'. Caused
by:
Error using hw12 (line 57)
Error evaluating parameter 'Gain' in 'Ex3_Controller/Gain'
Error using hw12 (line 57)
Undefined function or variable 'u'.
Error using hw12 (line 57)
Variable 'u' does not exist.
Suggested Actions:
• Load a file into 'Base Workspace'. - Fix
• Create a new variable. - Fix
Update: After removing the u term from the gain block, I received a different error:
Error using hw12 (line 57)
Error in port widths or dimensions. Output port 1 of 'Ex3_Controller/Gain' is a one dimensional vector with 4 elements.
Error using hw12 (line 57)
Error in port widths or dimensions. Input port 1 of 'Ex3_Controller/Model1' is a one dimensional vector with 1 elements.
The Gain block takes the value of the input signal and multiplies it by the value of the gain. In your case the gain is K and that is all you need to put into the gain block (i.e. remove the *u, Simulink handles that for you.)
Once that is done, the dimension error you are getting is because your controller requires u to be a scalar, but you are feeding a 4 element vector into it. You need to change the appropriate parameter of the Gain block so that it does a matrix multiplication, taking the 4-by-1 matrix K and (matrix) multiplying it with the 4 element "out" signal to produce a scalar.

getting complex-valued Jacobian Matrix using NonLinearModel.fit in matlab

I am trying to use the NonLinearModel.fit() function in Matlab to regress two variables. However, I am getting the following error:
Error using internal.stats.getscheffeparam>ValidateParameters
(line 182)
If non-empty, JW must be a numeric, real matrix.
Error in internal.stats.getscheffeparam (line 110)
[J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,usingJ] =
ValidateParameters(J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,allowedIntopt);
Error in nlinfit (line 340)
sch =
internal.stats.getscheffeparam('WeightedJacobian',J(~nans,:),'Intopt','observation','VQ',VQ);
Error in NonLinearModel/fitter (line 1121)
[model.Coefs,~,J_r,model.CoefficientCovariance,model.MSE,model.ErrorModelInfo,~]
= ...
Error in classreg.regr.FitObject/doFit (line 219)
model = fitter(model);
Error in NonLinearModel.fit (line 1484)
model = doFit(model);
Error in getMatrix (line 101)
nlm =
NonLinearModel.fit(regressorMatrix',temp2',modelfun,beta0);
My regressorMatrix is 2-by-N (so the transpose is N-by-2), temp2' is N-by-1, and beta0 and model are given by:
model =#(b,x)b(1).*x(:,1).*x(:,2).^b(2);
beta0=[.14 .6];
nlm = NonLinearModel.fit(regressorMatrix',temp2',model,beta0);
Could someone please help me in figuring out what is causing this error?
EDIT: ok, no help so far so I will try to be more specific. I know that this error is referring to the weighted Jacobian matrix. I am just not sure why this Jacobian would not be real-valued.
Here are the first few rows of my regressor matrix:
regressorMatrix =
1.0e+07 *
0.000000000776613 3.762601240855837
0.000000001683014 3.762601240855837
0.000000001496807 3.762601240855837
0.000000000753495 3.762601240855837
and my response matrix:
temp2 =
-0.011811061934317
0.987582922964869
0.010621342764736
0.135001167018444
0.091950680609212
I can see that something is wrong here (the orders of magnitude in col2 of my regressor matrix are off. I will fix this and get back with an explanation if it turns out to the the cause. I am also working on printing out J. and JW
EDIT2:
I was able to print out JW before the error occurs and discovered that JW is a Nx2 complex matrix. So the specific reason the error is occurring is that the weighted Jacobian matrix is not real valued. Not sure why...
After a lot of digging, I found that the answer to this problem is quite obvious. I didn't realize that some of the values for one of my regressor variables was going negative sometimes. This lead (jumping a few steps ahead) to a complex Jacobian.

Function works with lsqcurvefit but not nlinfit

I'm attempting to get a nonlinear least squares fit for the following function:
Nloc = 250;
d = 1 / Nloc;
m = 0.5; %Initial guess
ncmfun = #(m, p) arrayfun(#(p) betainc(d, Nloc*m .* p, Nloc*m .* (1 - p), 'upper'), p);
Where m is the parameter being fit, Nloc and d are constants, and p and freq are vectors of positive real numbers and of the same length (I triple checked). When I use lsqcurvefit everything works fine:
[mfit,resnorm,] = lsqcurvefit(ncmfun, m, p, freq, 0, 1)
Also, if I use any m to evaluate the function, everything works fine as well. However when I use nlinfit:
[mfit,R,Jac,CovB,MSE,ErrorModelInfo] = nlinfit(p, freq, ncmfun, m)
I get the following error(s):
Error using betainc
Z must be real and non-negative.
Error in #(p)betainc(d,Nloc*m.*p,Nloc*m.*(1-p),'upper')
Error in
#(m,p)arrayfun(#(p)betainc(d,Nloc*m.*p,Nloc*m.*(1-p),'upper'),p)
Error in nlinfit>#(b,x)w.*model(b,x) (line 206)
modelw = #(b,x) w.*model(b,x);
Error in nlinfit>LMfit (line 486)
yfit = model(beta,X);
Error in nlinfit (line 207)
[beta,J,~,cause,fullr] = LMfit(X,yw,
modelw,beta,options,verbose,maxiter);
Error in Sloan_NCM_Parameterize_Nm (line 37)
[mfit,R,Jac,CovB,MSE,ErrorModelInfo] = nlinfit(p, freq,
ncmfun, m);
What's especially frustrating was this same script was working a couple weeks ago. I then tried to use it again and it no longer works. I tried to go through and see if I accidentally changed something and didn't remember, but I can't find any errors. Furthermore, I'm confused as to why lsqcurvefit works but not nlinfit. I'd like to use nlinfit because it provides me with more statistical information about the error.
Thanks in advance for any help you can provide!
They both use the same algorithm to identify a solution, however, you are explicitly stating an upper and lower bound for your solution in lsqcurvefit which ensures your incomplete beta function behaves. You don't have this option in nlinfit. I'm not sure what you did before (you can always look at the command history)
You have a few options from this point:
1 - programatically intercept every value that goes to ncmfun, by either expending your anonymous functions in what my nightmares are made of, or creating your own m-file for it. If the value is outside of [0,1] return progressively higher error value (with the boundaries being >>> then any possible value within the set)
2 - Try to force a quicker convergence by playing with some of the parameters (perhaps by using a robust fitting option (cf documentation))

Error using fmincon, hack fixes it but I'm uneasy about it

I'm having a strange error when using fmincon. The details about the objective function and the nonlinear constraint functions are many, so I'm going to try to ask this question without describing those if possible.
The minimization requires a single input. For some but not all inputs, I get the following error:
Subscript indices must either be real positive integers or logicals.
Error in qpsub>eqnsolv (line 888)
ACTSET = A(ACTIND,:);
Error in qpsub (line 157)
[Q,R,A,B,X,Z,how,ACTSET,ACTIND,ACTCNT,aix,eqix,neqcstr,ncstr, ...
Error in nlconst (line 619)
[SD,lambda,exitflagqp,outputqp,howqp,ACTIND] ...
Error in fmincon (line 794)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in PauliApproximate2.unitalChannelApproximate (line 170)
[pc, distance] = fmincon(#objective,x0, A, b, Aeq, beq,lb,ub, #constraint,
options);
When I check to see the value of ACTIND, the error is thrown when it equals [0], hence the indexing error. Similar to the hack that the OP of this thread used, before the line (in qpsub)
ACTSET = A(ACTIND,:);
I put the if statement:
if max(ACTIND) < 1
ACTIND = [1];
end
From preliminary testing this seems to "Fix" the problem. It is giving sensible results for inputs that used to throw the error (in that it is returning a good minimum value and the constraints are satisfied to within tolerance), and is returning the same results it always did for inputs that didn't have the error (obviously, as the if statement won't be invoked). So my question is, for people that know the inner workings of these functions, how bad is this? Am I sidestepping a horrible issue that results in ACTIND being [0] that I shouldn't be?