0
$\begingroup$

I am new and am not sure how to ask about this without posting the full code.

Essentially what I do is:

1-Solve numerically a system of equations. 2- Use manipulate to understand how solutions change in some parameter space. There are 4 plots on the top grid (the solutions), and the second order conditions for max on the bottom part of the grid.

The problem is Mathematica crashes on the first attempt and only when left for long enough ends up solving it. The sliders are not useful to understand how the solution changes because everything is very slow and crashes frequently.

Any tips on how to improve the performance of my code?

    (* a system of equations on 4 eqns and unknowns*)
mysol3[h_?NumericQ, \[Lambda]_?NumericQ, w_?NumericQ, c_?NumericQ, 
  R_?NumericQ , \[Alpha]_?NumericQ, A_?NumericQ, \[Sigma]_?NumericQ] :=
  FindRoot[
  {(E^((z0 + z1 + z2) \[Lambda]) ((
      A E^(-z3 \[Lambda]) (-1 + E^((z0 + z1 + z2 + z3) \[Lambda])))/
      h)^(-1/\[Sigma]) (A (R \[Alpha])^(
        1/\[Sigma]) \[Lambda] (-1 + \[Sigma]) - ((
         A E^(-z3 \[Lambda]) (-1 + 
            E^((z0 + z1 + z2 + z3) \[Lambda])))/h)^(1/\[Sigma])
         w (c + \[Lambda] + c z0 \[Lambda]) \[Sigma]))/(h \[Sigma]) ==
     0,
   (E^((z1 + z2) \[Lambda]) ((
      A E^(-z3 \[Lambda]) (-1 + E^((z0 + z1 + z2 + z3) \[Lambda])))/
      h)^(-1/\[Sigma]) (A E^(z0 \[Lambda]) (R \[Alpha])^(
        1/\[Sigma]) \[Lambda] (-1 + \[Sigma]) - ((
         A E^(-z3 \[Lambda]) (-1 + 
            E^((z0 + z1 + z2 + z3) \[Lambda])))/h)^(1/\[Sigma])
         w ((E^(z0 \[Lambda]) + h) \[Lambda] + 
          c (h + E^(z0 \[Lambda]) z0 \[Lambda] + 
             h z1 \[Lambda])) \[Sigma]))/(h \[Sigma]) == 0, 
   -c E^(z2 \[Lambda]) w - (
     E^((z0 + z1 + z2) \[Lambda]) w (1 + c z0) \[Lambda])/h - 
     E^((z1 + z2) \[Lambda]) w (1 + c z1) \[Lambda] - 
     E^(z2 \[Lambda]) w (1 + c z2) \[Lambda] + (
     A E^((z0 + z1 + z2) \[Lambda]) ((
       A E^(-z3 \[Lambda]) (-1 + E^((z0 + z1 + z2 + z3) \[Lambda])))/
       h)^(-1/\[Sigma]) (R \[Alpha])^(
      1/\[Sigma]) \[Lambda] (-1 + \[Sigma]))/(h \[Sigma]) == 0,
   -c w + (
     A E^(-z3 \[Lambda]) ((
       A E^(-z3 \[Lambda]) (-1 + E^((z0 + z1 + z2 + z3) \[Lambda])))/
       h)^(-1/\[Sigma]) (R \[Alpha])^(
      1/\[Sigma]) \[Lambda] (-1 + \[Sigma]))/(h \[Sigma]) == 0}, {{z0,
     1}, {z1, 1}, {z2, 2} , {z3, 3}}]

(Manipulate to understand the solution)

Manipulate[

 Grid[{

   {Plot3D[
     z0 /. mysol3[h, \[Lambda], w, c, R , \[Alpha], A, \[Sigma]] , {h,
       0.26, 0.9}, {\[Lambda], 1, 1.5}, PlotLabel -> "z0", 
     ImageSize -> Large, PlotRange -> Full, AxesLabel -> Automatic, 
     ImageSize -> 200],
    Plot3D[ 
     z1 /. mysol3[h, \[Lambda], w, c, R , \[Alpha], A, \[Sigma]] , {h,
       0.26, 0.9}, {\[Lambda], 1, 1.5}, PlotLabel -> "z1", 
     ImageSize -> Large, PlotRange -> Full, AxesLabel -> Automatic, 
     ImageSize -> 200],
    Plot3D[ 
     z2 /. mysol3[h, \[Lambda], w, c, R , \[Alpha], A, \[Sigma]] , {h,
       0.26, 0.9}, {\[Lambda], 1, 1.5}, PlotLabel -> "z2", 
     ImageSize -> Large, PlotRange -> Full, AxesLabel -> Automatic, 
     ImageSize -> 200],
    Plot3D[ 
     z3 /. mysol3[h, \[Lambda], w, c, R , \[Alpha], A, \[Sigma]] , {h,
       0.26, 0.9}, {\[Lambda], 1, 1.5}, PlotLabel -> "z3", 
     ImageSize -> Large, PlotRange -> Full, AxesLabel -> Automatic, 
     ImageSize -> 200]},



   }],
 {w, 1, 1}, {c, 0.225, 0.225}, {R, 1, 1} , {\[Alpha], 1000, 5000}, {A,
   0.26, 2}, {\[Sigma], 2, 4}, SaveDefinitions -> True, 
 TrackedSymbols :> True]
$\endgroup$
  • 1
    $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form**. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Why not choosing a meaningful name? $\endgroup$ – rhermans Jul 13 '17 at 16:05
  • $\begingroup$ Thanks @rhermans. I have read your links and tried to simplify the problem a bit. Possible things that I think may help speed: 1-Maybe try to simplify the system? It may be possible (?) but I already used Simplify. 2-Take some of the arguments are numbers from the beginning. 3- Make equations be functions which come from First Order Conditions instead of long set of symbols. Many thanks in advance. $\endgroup$ – aMar Jul 14 '17 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.