3
$\begingroup$

I looked at different questions asked concerning the error message that I get but it did not help. I try to plot a function. Here is the code that I use ;

u[c_] := (c^(1 - σ) - 1)/(1 - σ)
h[s_] := (2 hbar)/(1 + Exp[η (1 - s/sbar)])
d[s_] := (b s^2)/2
ψ[k_] := ω + (1 - ω) Exp[-γ k]
co[a_] := x  (a^2)/2

The calibration I use ;

paramFinal1 = {σ -> 2.1, ρ -> 0.01, sbar -> 128, η -> 5.1, hbar -> 0.08, b -> 0.0001, γ -> 0.6, χ -> 0.025, ω -> 0.185, δ -> 0.015, x -> 0.14, β -> 0.8};

I wrote the following code ;

Here is the function that I try to plot on a $(k,s)$ plane

l[k_, s_] :=   u'[χ/β s] - β (h'[s]/(ρ + h[s]) ((u[χ/β s] - ψ[k] d[s] - co[δ k]  )/(χ + (ρ + h[s]))) + ( ψ[k] d'[s])/(χ + (ρ + h[s])))

I write

sol3[i_] := Solve[l[k, i] == 0 /. paramFinal1, k]
tab = Table[sol3[i], {i, 1, 300}];
pollutiondot0 = Last /@ Flatten[tab] // ListPlot

But the code generates the following error messages ;

Solve::inex: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> General::stop: Further output of Solve::inex will be suppressed during this calculation. >>

How can I fix it ? What could be the problem ? Thanks in advance.

$\endgroup$
  • $\begingroup$ Try using exact numbers e.g. instead of p -> 0.01 use p -> 1/100. You can use Rationalize[] to convert your parameters automatically. $\endgroup$ – Sascha Jan 2 '16 at 16:02
  • $\begingroup$ You also might try to use NSolve instead of Solve to find a numerical solution $\endgroup$ – Sascha Jan 2 '16 at 16:11
  • 3
    $\begingroup$ This is a question about Solve and NSolve, not a question about plotting. $\endgroup$ – march Jan 2 '16 at 16:14
4
$\begingroup$
u[c_] := (c^(1 - σ) - 1)/(1 - σ);
h[s_] := (2 hbar)/(1 + Exp[η (1 - s/sbar)]);
d[s_] := (b s^2)/2;
ψ[k_] := ω + (1 - ω) Exp[-γ k];
co[a_] := x (a^2)/2;

paramFinal1 = {σ -> 2.1, ρ -> 0.01, sbar -> 128, η -> 5.1, 
   hbar -> 0.08, b -> 0.0001, γ -> 0.6, χ -> 0.025, 
   ω -> 0.185, δ -> 0.015, x -> 0.14, β -> 0.8};

l[k_, s_] := 
  u'[χ/β s] - β (h'[
         s]/(ρ + 
          h[s]) ((u[χ/β s] - ψ[k] d[s] - 
           co[δ k])/(χ + (ρ + h[s]))) + 
           (ψ[k] d'[s])/(χ + (ρ + h[s])));

Use NSolve and since you are using a numeric technique, restrict sol3 to numeric arguments

sol3[i_?NumericQ] :=
 {i, k} /. NSolve[l[k, i] == 0 /. paramFinal1, k, Reals]

For many values of i there is a single solution, e.g.,

sol3[1]

(*  {{1, -22.6603}}  *)

However, for some values of i there are two solutions, e.g.,

sol3[100]

(*  {{100, -2.37307}, {100, 133.086}}  *)

tab = Table[sol3[i], {i, 1, 300}];

ListLinePlot[
 Flatten[#, {1}] & /@ {Last /@ tab, First /@ Select[tab, Length[#] == 2 &]},
 PlotLegends -> Automatic]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for this insightful code. $\endgroup$ – optimal control Jan 3 '16 at 21:51

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.