lsqnonlin matlab stops execution - matlab

I am using the Matlab function lsqnonlin for estimating a vector of 5 values.
Before calling lsqnonlin, I am making a rough estimation of the 3 parameters, and afterwards the lsqnonlin is being initialised using these three parameters (the other two are constant)
In some cases, the following error stops the execution:
Error using eig
Input to EIG must not contain NaN or Inf.
Error in trust (line 30)
[V,D] = eig(H);
Error in trdog (line 110)
[st,qpval,po,fcnt,lambda] = trust(rhs,MM,delta);
Error in snls (line 320)
[sx,snod,qp,posdef,pcgit,Z] = trdog(x,g,A,D,delta,dv,...
Error in lsqncommon (line 150) [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in lsqnonlin (line 237)[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Do you have any suggestion?

Related

Errors out of memory running Matlab Autoencoders on 10^5 sparse matrix

I have a 10^5 square sparse matrix called pbAttack. Each element represents if there is connection between node i and node j. If there is connection, pbAttack(i,j) = 1. Otherwise, pbAttack (i,j) = 0. Then I want to use it following this tutorial: Matlab Autoencoders. I use the same code as in the linked Matlab tutorial. I only change it to use my data.
However, I got following errors:
Error using bsxfun
Out of memory. Type HELP MEMORY for your options.
Error in mapminmax.apply (line 8)
y = bsxfun(#plus,y,settings.ymin);
Error in mapminmax.create (line 44)
y = mapminmax.apply(x,settings);
Error in mapminmax (line 51)
[y,settings] = mapminmax.create(x,param);
Error in nnet.internal.configure.input (line 31)
[x,config] = feval(processFcns{j},x,processParams{j});
Error in network/configure (line 234)
net = nnet.internal.configure.input(net,i,X{i});
Error in nntraining.config (line 116)
net = configure(network(net),X,T);
Error in nntraining.setup>setupPerWorker (line 68)
[net,X,Xi,Ai,T,EW,Q,TS,err] = nntraining.config(net,X,Xi,Ai,T,EW,configNetEnable);
Error in nntraining.setup (line 43)
[net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);
Error in network/train (line 335)
[net,data,tr,err] = nntraining.setup(net,net.trainFcn,X,Xi,Ai,T,EW,enableConfigure,isComposite);
Error in Autoencoder.train (line 490)
net = train(net,X,X,'useGPU',iYesOrNo(useGPU));
Error in trainAutoencoder (line 105)
autoenc = Autoencoder.train(X, autonet, paramsStruct.UseGPU);
Error in workflow_autoencoder (line 8)
autoenc1 = trainAutoencoder(pbAttack,hiddenSize,...
Are all these errors caused by the huge size of the matrix? Is there a work around so that it does not need so much memory? Thank you so much.
It just means that you don't have enough memory for this (since your matrix is not so sparse). You can try to repeat your code by reducing the size of the matrix.

Error: A and B must be floating point scalars

I am trying to optimize the function using fminsearch and function handle
but, I get the error A and B must be floating point scalars. In detalis,
Error using integral (line 86)
A and B must be floating point scalars.
Error in #(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3))
Error in #(x)((x-f2(x,p)).^2)
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);
Error in #(p)(integral(#(x)((x-f2(x,p)).^2),-3,3))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
How can I solve this?
i think x-3 become a problem but i cannot deal with it.
x should be variable in f2 in order to do integral with respect to x in q3
Thank you in advance
sigma=0.1;
f2=#(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3));
q3=#(p)(integral(#(x)((x-f2(x,p)).^2),-3,3));
[p, fval] = fminsearch(q3,[0.1 0.4]);
The problem is that when you integrate over x, the function "integrate" gives f2 a vector to evaluate. Which is the same reason you use the dot notation in the function "q3".
The quick fix is to use arrayfun around the "f2" - but you should really consider to adapt to what user20160 suggested in the comments, namely to make full functions, then it will be easier to debug and as it takes in a vector, you can make a for loop, which runs over the inputted endpoints. Standard for-loops are faster than standard arrayfun.

MATLAB errors when using Lagrange Multipliers?

I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );

fmincon using SQP algorithm not running

I need to run the optimization of a 2 variables function for which I provide the gradients. The code runs normally for the 'interior-point' algorithm but it stucks after the first interaction when I try the 'sqp' algorithm. The first algorithm gives me 5% of error from the values I expected, so I really need to check if SQP gives me better results.
deltaml=10^(-4);
deltaE=10^(-2);
Eo=[0;1];
Elb=[0;0];
Eub=[100;100000000000];
Alg='sqp'
f1=[subs(fdelta,ml,ml+deltaml);subs(fdelta,E,E+deltaE)];
f2=[subs(fdelta,ml,ml+deltaml/2);subs(fdelta,E,E+deltaE/2)];
gradobj = (4*f2-3*f-f1)./[deltaml;deltaE];
objfungrad = matlabFunction(fdelta,gradobj,'vars',{t},'outputs',{'f','gradf'});
opts_grad=optimset('Algorithm',Alg,'TolFun',1e-16,'TolX',1e-16,'Display','off','GradObj','on');
[xgrad fval] = fmincon(objfungrad,Eo,[],[],[],[],[Elb],[Eub],[],opts_grad);>
The error when running sqp is:
Error using deal (line 38)
The number of outputs should match the number of inputs.
Error in C:\Program
Files\MATLAB\R2013a\toolbox\symbolic\symbolic\symengine.p
Error in C:\Program
Files\MATLAB\R2013a\toolbox\optim\optim\private\evalObjAndConstr.p>evalObjAndConstr
(line 135)
Error in C:\Program
Files\MATLAB\R2013a\toolbox\optim\optim\sqpLineSearch.p>sqpLineSearch
(line 287)
Error in fmincon (line 910)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
sqpLineSearch(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
Error in symb_optm_ml_E_mult (line 54)
[xgrad fval] =
fmincon(objfungrad,Eo,[],[],[],[],[Elb],[Eub],[],opts_grad);

Using fgoalattain() or solve() for Matlab

I am trying to solve a problem (think excel "goal seek") where given a specific rate of return, matlab can solve for the variable "y" (which is used to multiply a matrix).
I've tried using fgoalattain (i don't understand it) and solve
here is the function which basically adds some arrays together to make a cashflow with y being a double. ppam_gen is a n*1 matrix. other_tot is a n*1 matrix.
down_pmt is a double.
function ansirr = cirr(y)
cf_gen_oth = y*ppam_gen + oth_tot
dp = [-down_pmt]
cashflow = [dp;cf_gen_oth]
ansirr = irr(cashflow)
%ansirr = irr([dp; y*ppam_gen + oth_tot])
end
x = fgoalattain(#cirr,0.001,.15,abs(.15))
does not work, the following error is given:
Error using roots (line 28)
Input to ROOTS must not contain NaN or Inf.
Error in irr (line 134)
coeff = roots(fliplr(cf(:,loop)')); % Find roots of polynomial
Error in ppa_model/cirr (line 139)
ansirr = irr(cashflow)
Error in goalcon (line 26)
f = feval(funfcn{3},x,varargin{:});
Error in
fgoalattain>#(y,varargin)feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:})
(line 473)
cfun{3} = #(y,varargin)
feval(cfun{3},y,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,errCheck,varargin{:});
Error in nlconst (line 746)
[nctmp,nceqtmp] = feval(confcn{3},x,varargin{:});
Error in fgoalattain (line 519)
[xnew,ATTAINFACTOR,LAMBDA,EXITFLAG,OUTPUT]=...
Error in ppa_model (line 152)
r = fgoalattain(#cirr,0.001,.15,abs(.15))
and using symbolic toolbox doesn't work either.
syms y
solve(irr([-down_pmt ; y*ppam_gen + oth_tot]) == 0.15)
throws the error:
Error using assignin
Attempt to add "y" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in ppa_model (line 152)
syms y