2
$\begingroup$

I am editing this question, I hope to be able to respect the rules. My goal is to find the roots of a system of 6 nonlinear equations (they are actually more, this is a simplified version). The roots represent equilibrium wages and prices. I've written the following code:

Manipulate[
 Chop[FindRoot[
   {Nl == nl1[wl1, wh1] + nl2[wl2, wh2], Nh == nh1[wl1, wh1] + nh2[wl2, wh2], 
    (wl1/wl2) == (pH1/pH2)^α, (wh1/wh2) == (pH1/pH2)^α, 
    nl1[wl1, wh1]*wl1 + nh1[wl1, wh1]*wh1 == (pH1*H)/α, 
    nl2[wl2, wh2]*wl2 + nh2[wl2, wh2]*wh2 == (pH2*H)/α},
   {{wh1, 1}, {wh2, 1}, {wl1, 1}, {wl2, 1}, {pH1, 1.2}, {pH2, 1.2}}
  ]], 
  {α, 0.25}, {λ, 3}, {η, 0.5}, {ϕh1, 0.2}, {ϕh2, 0.2}, {ah, 5}, {am, 2},
  {A1g, 1}, {A2g, 1}, {Nh, 1}, {Nl, 1}, {H, 0.1}
]

where I have defined the functions involved in the equations as follows

nl1[wl1_, wh1_] := (wl1/(A1g*λ*η*(1 - ϕh1)^λ))^(1/(λ*\η - 1))*(1 
  + (wh1/wl1)^(η/(η - 1))*((1 - ϕh1)/(ah*ϕh1))^(1/(η - 1)))^((1 - λ)/(λ*η - 1));

nl2[wl2_, wh2_] := (wl2/(A2g*λ*η*(1 - ϕh2)^λ))^(1/(λ*\η - 1))*(1 + 
  (wh2/wl2)^(η/(η - 1))*((1 - ϕh2)/(ah*ϕh2))^(1/(η - 1)))^((1 - λ)/(λ*η - 1));    

nh1[wl1_, wh1_] := nl1[wl1, wh1]*(wh1/wl1)^(1/(η - 1))*((1 - ϕh1)/(ah*ϕh1))^(1/(η - 1)); 
nh2[wl2_, wh2_] := nl2[wl2, wh2]*(wh2/wl2)^(1/(η - 1))*((1 - ϕh2)/(ah*ϕh2))^(1/(η - 1));

I have two problems.

First, I am able to find the roots only if write the explicit functional forms in FindRoot. However, if I write the general functions defined above, I get:

The function value <<1>> is not a list of numbers with dimensions {6} at {wh1, wh2, wl1, wl2, pH1, pH2} {1.`,1.`,1.`,1.`,1.2`,1.2`}

I guess the problem can be solved using ?NumericQ. I have made many attempts but no success so far.

The second problem, more important, is that I want to evaluate the four functions I have defined above when the arguments take the value of the roots and see how their values change with the parameters, using Manipulate. I guess I have to define the solutions provided by FindRoot and then Manipulate the above defined function by forcing their arguments to take the value of the roots. Again, many attempts but no success.


UPDATE: I have managed to solve the second problem using the following code

sol[α_, λ_, η_, ϕh1_, ϕh2_, ϕm1_, ϕm2_, ah_, am_, A1g_, A2g_, Nh_, Nm_, Nl_, H_] := 
 Chop[FindRoot[{Nl == nl1[wl1, wh1] + nl2[wl2, wh2], 
 Nm == nm1[wm1] + nm2[wm2], 
 Nh == nh1[wl1, wh1] + nh2[wl2, wh2], (wh1/wh2) == (pH1/pH2)^α, (wl1/
 wl2) == (pH1/pH2)^α, (wm1/wm2) == (pH1/pH2)^α, 
 nl1[wl1, wh1]*wl1 + nh1[wl1, wh1]*wh1 == (pH1*H)/α, 
 nl2[wl2, wh2]*wl2 + nh2[wl2, wh2]*wh2 == (pH2*H)/α}, {{wh1, 0.5}, 
 {wh2, 0.5}, {wm1, 0.5}, {wm2, 0.5}, {wl1, 0.5}, {wl2, 0.5}, {pH1, 0.5}, {pH2, 
 0.5}}]];

 Manipulate[{nl1[wl1, wh1], nm1[wm1], nh1[wl1, wh1], nl2[wl2, wh2], 
 nm2[wm2], nh2[wl2, wh2]} /. sol[α, λ, η, ϕh1, \ 
 [Phi]h2, ϕm1, \ϕm2, ah, am, A1g, A2g, Nh, Nm, Nl, H], {α, 
 0.25}, {λ, 1.3}, {η, 0.6}, {ϕh1, 0.3}, {ϕh2, 
 0.3}, {ϕm1, 0.3}, {ϕm2, 0.3}, {ah, 2.5}, {am, 2.1}, {A1g, 
 1}, {A2g, 1}, {Nh, 1}, {Nm, 1}, {Nl, 1}, {H, 1, 2}, Initialization :> 
 (nl1[wl1_?NumericQ, wh1_?NumericQ] := (wl1/(A1g*λ*η*(1 - \ 
 [Phi]m1 - \ϕh1)^\ 
 [Lambda]))^(1/(λ*η - 1))*(1 + (wh1/wl1)^(η/(η - 1))* 
 ((1 - ϕm1 - \ϕh1)/(ah*ϕh1))^(1/(η - 
 1)))^((1 - λ)/(λ*η - 1));
 nl2[wl2_?NumericQ, wh2_?NumberQ] := (wl2/(A2g*λ*η*(1 - ϕm2 
 - \ϕh2)^\ 
 [Lambda]))^(1/(λ*η - 1))*(1 + (wh2/wl2)^(η/(η - 1))* 
 ((1 - ϕm2 - \ϕh2)/(ah*ϕh2))^(1/(η - 1)))^((1 - \ 
 [Lambda])/(λ*η - 1));
  nm1[wm1_?NumericQ] := (wm1/(A1g*η*ϕm1*am))^(1/(η - 1));
  nm2[wm2_?NumericQ] := (wm2/(A2g*η*ϕm2*am))^(1/(η - 1));
  nh1[wl1_?NumericQ, wh1_?NumericQ] := nl1[wl1, wh1]*(wh1/wl1)^(1/(η - 
  1))*((1 - ϕm1 - ϕh1)/(ah*ϕh1))^(1/(η - 1));
  nh2[wl2_?NumericQ, wh2_?NumericQ] := nl2[wl2, wh2]*(wh2/wl2)^(1/(η 
  -1))*((1 - ϕm2 - ϕh2)/(ah*ϕh2))^(1/(η - 1));)]

Thanks a lot for the suggestions!

$\endgroup$

marked as duplicate by Kuba Jun 5 '18 at 10:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ This is far too broad and vague. Give us an example. $\endgroup$ – MarcoB Jun 3 '18 at 21:37
  • $\begingroup$ Your question needs more from your side. Here it's considered helpful and polite to show your own efforts and share your data and code attempts in a well formatted form, so we can quickly see the problem you are facing. Please help us to help you and edit your question accordingly. If you write an excellent question it will inspire great answers. As it is, your question may be put on-hold because it lacks enough details. $\endgroup$ – rhermans Jun 4 '18 at 17:14
  • $\begingroup$ Fabio, thank you for adding all the details. I have voted to reopen and reverted my downvote. $\endgroup$ – MarcoB Jun 5 '18 at 0:12
1
$\begingroup$

Final solution using tips from linked topics, adding _?NumericQ and moving definitions to Initialization to match e.g. A1g with controller:

 Manipulate[
   Chop[FindRoot[{Nl==nl1[wl1,wh1]+nl2[wl2,wh2],Nh==nh1[wl1,wh1]+nh2[wl2,wh2],(wl1/wl2)==(pH1/pH2)^α,(wh1/wh2)==(pH1/pH2)^α,nl1[wl1,wh1]*wl1+nh1[wl1,wh1]*wh1==(pH1*H)/α,nl2[wl2,wh2]*wl2+nh2[wl2,wh2]*wh2==(pH2*H)/α},{{wh1,1},{wh2,1},{wl1,1},{wl2,1},{pH1,1.2},{pH2,1.2}}]],
  {α,0.25},{λ,3},{η,0.5},{ϕh1,0.2},{ϕh2,0.2},
  {ah,5},{am,2},{A1g,1},{A2g,1},{Nh,1},{Nl,1},{H,0.1},
  Initialization:>(
    nl1[wl1_?NumericQ,wh1_?NumericQ]:=(wl1/(A1g*λ*η*(1-ϕh1)^λ))^(1/(λ*η-1))*(1+(wh1/wl1)^(η/(η-1))*((1-ϕh1)/(ah*ϕh1))^(1/(η-1)))^((1-λ)/(λ*η-1));

    nl2[wl2_?NumericQ,wh2_?NumberQ]:=(wl2/(A2g*λ*η*(1-ϕh2)^λ))^(1/(λ*η-1))*(1+(wh2/wl2)^(η/(η-1))*((1-ϕh2)/(ah*ϕh2))^(1/(η-1)))^((1-λ)/(λ*η-1));

    nh1[wl1_?NumericQ,wh1_?NumericQ]:=nl1[wl1,wh1]*(wh1/wl1)^(1/(η-1))*((1-ϕh1)/(ah*ϕh1))^(1/(η-1));
    nh2[wl2_?NumericQ,wh2_?NumericQ]:=nl2[wl2,wh2]*(wh2/wl2)^(1/(η-1))*((1-ϕh2)/(ah*ϕh2))^(1/(η-1));
  )
]

enter image description here

$\endgroup$
  • $\begingroup$ That's great! This definitely solves my first problem. Thanks! Still I have to find a way to manipulate the defined functions moved to initialization, by forcing their arguments to take the value of the roots. I would like to try to do it in the same Manipulate operation but I have no clue. I guess I have to use .\ but don't know how. $\endgroup$ – Fabio Cerina Jun 5 '18 at 12:11
  • $\begingroup$ @FabioCerina f[x] /. {x -> 1} $\endgroup$ – Kuba Jun 5 '18 at 12:14
  • $\begingroup$ I think I have solved my second problem. I would like to share the code but the space here is limited. Can I answer my question somewhere? Thanks $\endgroup$ – Fabio Cerina Jun 5 '18 at 16:21

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