0
$\begingroup$

I have a little program that should find values that Solve a system of polynomic equations and then substitute those values function. It solves the system but then, the substitution doesn't happen. I can't see what I'm doing wrong

T = {{2, 3}, {3, 2}, {1, 4}};
techniques = Dimensions[T][[1]];
factors = Dimensions[T][[2]];
αVec = ConstantArray[1/2, techniques];
λVec = Array[λ, techniques];
γVec = Array[γ, factors];
onesVec = ConstantArray[1, techniques];
FactorNeeds = Transpose[T].λVec^(1/αVec)
Quiet[Solve::ztest1];

varVec = Flatten[{λVec, Subscript[δ, 1]}];
temp = Table[FactorNeeds /.
Flatten[
 Solve[D[FactorNeeds[[j]] - 
     Subscript[δ, 
      1] (onesVec.λVec - 1), {varVec}] == 
   ConstantArray[0, Length[varVec]], varVec, Reals]], {j, 1, 
factors}];
minmax = Table[temp[[j, 2]]/temp[[j, 1]], {j, 1, factors}];
minKL = Min[minmax];
maxKL = Max[minmax];

i = Rationalize[minKL + .3 (maxKL - minKL)];

varVec = Flatten[{λVec, {Subscript[δ, 1], 
 Subscript[δ, 2]}}];
zerosVec = ConstantArray[0, Length[varVec]];
temp = N[Flatten[
Solve[{D[(FactorNeeds[[1]] - 
     Subscript[δ, 1] (λVec.onesVec - 1) + 
     Subscript[δ, 
      2] (FactorNeeds[[2]] - i*FactorNeeds[[1]])), {varVec}] == 
  ConstantArray[0, Length[varVec]], varVec >= zerosVec}, varVec, 
Reals]]]
kVec = FactorNeeds /. temp 

The output I get is {λ[1.] -> 0.333166, λ[2.] -> 0.32428, λ[3.] -> 0.342554, Subscript[δ, 1] -> 1.30963, Subscript[δ, 2] -> 0.371542}, which is the solution to the second Solve and {2 λ[1]^2 + 3 λ[2]^2 + λ[3]^2, 3 λ[1]^2 + 2 λ[2]^2 + 4 λ[3]^2}, which is just the definition of FactorNeeds. I was asuming that my last line of code would replace the values λ[j] in FactorNeeds by those obtained in the Solve. Can the fact that those are listed as λ[j.] rather than λ[j] have anything to do with it?

$\endgroup$
3
  • $\begingroup$ Thanks, @Kuba. How do I use greek letters within the code? I knew my post looked horrible, but I don't know how to do what you did. $\endgroup$
    – Patricio
    Commented Oct 30, 2018 at 8:59
  • $\begingroup$ You can use the site steampiano.net/msc $\endgroup$
    – Coolwater
    Commented Oct 30, 2018 at 9:00
  • $\begingroup$ Thanks, @Coolwater $\endgroup$
    – Patricio
    Commented Oct 30, 2018 at 9:02

1 Answer 1

1
$\begingroup$

Before N you need to add the NHoldAll attribute to λ:

SetAttributes[λ, NHoldAll]
temp = N[...];
kVec = FactorNeeds /. temp
{0.65481527, 1.0126864}

Otherwise your variables are modified by N, e.g. λ[2] becomes λ[2.] which prevents ReplaceAll from matching the variables in FactorNeeds.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.