Reputation: 13
I want to plot solutions of an inequality by matplotlib so I need solution values in sth like an array. the inequality is F1(γ)>=0 for γ>1.11
.
To do so first I create F1 function by sympy and then transform it by lambdify. my code is:
import sympy
from sympy import *
r, γ, W, c= symbols('r γ W c', positive=True, real=True)
D = 2*γ-c-sqrt((2*γ-c)**2-4*γ*(1+r)*(1-W))
EU_e = γ-D+D**2/(4*γ)
F = EU_e - (1+r)*W
F1 = F.subs([(W, 0.003), (c, 0.2), (r, 0.11)])
now F1 is a symbolic function of γ: click here
then I lambdify F1 and find the the nearest value of γ that applies to equality equation F1(γ)=0 for γ>1.11
.
from scipy.optimize import minimize
import numpy as np
ff=lambdify(γ, F1)
bounds = ((1.11, None),)
x0 = np.array([1.3])
res = minimize(lambda x: np.linalg.norm(ff(x)), x0=x0, bounds=bounds)
It works for many values of parametres W,c,r
but doesn`t work for some such as (W=0.003), (c= 0.2), (r= 0.11)
i.e. I encounter the following error:
click here
and res
is:
click here
Does anyone know how to find solution values of γ (or the nearest values) of the inequality equation?
I drawed solutions area by desmos.com. I`d like to plot sth like this by matplotlib: click here
Thank you
Upvotes: 1
Views: 189
Reputation: 13170
It works for many values of parametres
W,c,r
but doesnt work for some such as
(W=0.003), (c= 0.2), (r= 0.11)` i.e. I encounter the following error...
The problem is that your function is not defined for every combination of the parameters. In fact, the function F
:
is defined only when the radicand is >= 0. If all your parameters are positive, then you can see that when 0 <= W < 1
, the function might not defined.
As far as I know, minimization strategies require the function to be defined and continuous over a specified range. But your function clearly is not defined.
I drawed solutions area by desmos.com. I`d like to plot sth like this by matplotlib
SymPy exposes the plot_implicit
function, which is useful to plot inequalities:
from sympy.plotting import plot_implicit
# NOTE: adaptive=False because it appears to produce more "correct" results
plot_implicit(F.subs({c: 0.2, r: 0.11}) >= 0, (W, 0, 1), (γ, 1.1, 1.35), adaptive=False)
Upvotes: 1