1
$\begingroup$

I have the following function :

paramFinal = {σ -> 0.35, ρ -> 0.85, h -> 60, a -> 1, b -> 0.1, δ -> 0.001, γ -> 0.0001, ψ -> 60, θ -> 0.09586401037966653};

adap[β_, root22_] := (1 - root22) root22 - (γ a ((ψ a θ γ b  (\σ (1 - Exp[-(β - ρ) h]))/(β - ρ))/(root22 ((ρ + θ) - (1 - 2 root22)) ((a - δ) - (ρ + θ))))^(-1/\σ))/(a - δ) /. paramFinal

I try to make a list of variables root22 and $\beta$ and I write

solK[i_] := NSolve[adap[β, i] == 0 /. paramFinal, {β}, Reals];
Table[solK[i], {i, 0.1, 1, 0.1}]

There are some error messages as well but this operation gives result very very slowly. What can I do to make the procedure faster ?

$\endgroup$
  • $\begingroup$ I can run the code above in 2 sec and it gives some results with no errors. $\endgroup$ – happy fish May 23 '17 at 8:05
  • 1
    $\begingroup$ @happyfish I am using Mathematica 9.0 and have the results very slowly. Interesting... $\endgroup$ – optimal control May 23 '17 at 8:08
  • 1
    $\begingroup$ I am using v11.1. The output is {{{\[Beta] -> 1.13078}}, {{\[Beta] -> 0.92142}}, {{\[Beta] -> 0.876976}}, {{\[Beta] -> 0.855107}}, {{\[Beta] -> 0.840542}}, {{\[Beta] -> 0.82927}}, {{\[Beta] -> 0.819591}}, {{\[Beta] -> 0.810363}}, {{\[Beta] -> 0.799883}}, {}} $\endgroup$ – happy fish May 23 '17 at 8:35
  • $\begingroup$ Version 10.0.0 is slow and gives the error NSolve::ratnz:. $\endgroup$ – mattiav27 May 23 '17 at 9:22
  • $\begingroup$ If NSolve::ratnz: is the error that you get, you should replace 0.35 with 35/100 and so on. $\endgroup$ – mattiav27 May 23 '17 at 12:04
1
$\begingroup$

NSolve[]. It often helps in transcendental equations to bound the unknowns, if bounds can be determined. This might be done by plotting.

solK[i_] := NSolve[adap[β, i] == 0 && 0.1 < β < 1.5 /. paramFinal, {β}, Reals];
Table[solK[i], {i, 0.1, 1, 0.1}]

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>
...
General::stop: Further output of Solve::ratnz will be suppressed during this calculation. >>

(*
  {{{β -> 1.13078}},  {{β -> 0.92142}}, {{β -> 0.876976}}, {{β -> 0.855107}}, 
   {{β -> 0.840542}}, {{β -> 0.82927}}, {{β -> 0.819591}}, {{β -> 0.810363}},
   {{β -> 0.799883}}, {}}
*)

Reasonably fast:

Quiet@Table[solK[i], {i, 0.1, 1, 0.1}]; // AbsoluteTiming
(*  {0.151135, Null}  *)

FindRoot[]. Plots of the function suggest it is monotonic, in which case FindRoot is probably a better alternative; however, it fails on the last equation because there is no root.

solK[i_] := FindRoot[adap[β, i] == 0 /. paramFinal, {β, 1}];
Table[solK[i], {i, 0.1, 1, 0.1}]

FindRoot::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.

(*
  {{β -> 1.13078}, {β -> 0.92142}, {β -> 0.876976}, {β -> 0.855107}, {β -> 0.840542},
   {β -> 0.82927}, {β -> 0.819591}, {β -> 0.810363}, {β -> 0.799883},
   {β -> 0.236409}}  <-- not a solution
*)

Ten times faster, though:

Quiet@Table[solK[i], {i, 0.1, 1, 0.1}]; // AbsoluteTiming
(* {0.015183, Null}  *)
$\endgroup$

Your Answer

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

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