# Trouble with NSolve

I have a fairly simple program that yields puzzling output. The program is

(* Variables definition *)
T = {{2, 3}, {3, 2}, {1, 4}};
techniques = Dimensions[T][];
factors = Dimensions[T][];
AlphaVec = ConstantArray[3/4, techniques];
LambdaVec = Array[lambda, techniques];
GammaVec = Array[Gamma, factors];
onesVec = ConstantArray[1, techniques];
zerosVec = ConstantArray[0, techniques];
FactorNeeds = Transpose[T].lambdaVec^(1/AlphaVec);

(* Find min and max K/L ratios minKL y maxKL *)
temp = Table[LambdaVec /.
NMinimize[{FactorNeeds[[j]], {onesVec.LambdaVec == 1, LambdaVec >=
zerosVec}}, LambdaVec, WorkingPrecision -> 30,
MaxIterations -> 100, Method -> "RandomSearch"][])^(
1/AlphaVec).T, {j, 1, factors}];
minmax = Table[temp[[j, 2]]/temp[[j, 1]], {j, 1, factors}];
minKL = Min[minmax]
maxKL = Max[minmax]

(* Marg Prod Determination *)
(* 1 Computes FactorNeeds (k) for a given value of K/L *)
(* 2 Uses k in FOC of max prod problem and obtains Gamma *)
prodmarg = {}

steps = 1
Do[
varVec = Flatten[{LambdaVec,{Delta1,Delta2}}];
temp = Flatten[NSolve[D[(FactorNeeds[] - Delta1*(LambdaVec.onesVec - 1) + Delta2*(FactorNeeds[] - i*FactorNeeds[])), {varVec}] ==
ConstantArray[0, Length[varVec]], varVec, Reals]];
kVec = FactorNeeds /. temp ;
LambdaStar = temp[[1 ;; techniques]];
varVec = Flatten[{LambdaVec, GammaVec}];
AppendTo[prodmarg,Flatten[{kVec, GammaVec /.
NSolve[D[LambdaVec.onesVec - GammaVec.(FactorNeeds - kVec), {varVec}] ==
ConstantArray[0, Length[varVec]] /. LambdaStar,GammaVec, Reals]}]];
Print[i], {i, minKL, maxKL, (maxKL - minKL)/steps}]


If steps is set to 1 (as in the code above), the program runs satisfactorily and yields {{2.16366,1.77883,0,0.42162},{0.95117,3.44784,0.78845,0}}. Problems arise when I use any steps>1, then I get ill-conditioning warnings. I cannot understand these warnings as if I set manually the value of i to anything in the interval (minKL,maxKL), NSolve[] has no trouble in finding the solution.

Even more, I tried to run

 s=maxKL
varVec = Flatten[{LambdaVec, Delta1, Delta2}}]
Flatten[NSolve[D[(FactorNeeds[] - Delta1*(LambdaVec.onesVec - 1) + Delta2*(FactorNeeds[] - i*FactorNeeds[])), {varVec}] ==
ConstantArray[0, Length[varVec]], varVec, Reals]];


which is simply trying to obtain temp within the Do[] in its second pass, so I expected to obtain the second element of the previous list. Instead, NSolve[] fails and gives a message about some subsystem not being solved.

Worse still, I created an ad hoc variable and set manually its value to maxKL and run again the program with steps=1. The output was {{2.16366,1.77883,Gamma,Gamma},{0.95117,3.44784,0.78845,0}}, i.e., it failed to compute gammas except in the last iteration of the Do[].

• you have problem at "temp", which looks like you forgot to close the bracket ). Also check what minKL and maxKL yield as they are used in the following steps. – Tugrul Temel Oct 11 '18 at 13:09