scipy.odeint strange behavior - python

Here is my code to solve differential equation dy / dt = 2 / sqrt(pi) * exp(-x * x) to plot erf(x).
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
import math
def euler(df, f0, x):
h = x[1] - x[0]
y = [f0]
for i in xrange(len(x) - 1):
y.append(y[i] + h * df(y[i], x[i]))
return y
def i(df, f0, x):
h = x[1] - x[0]
y = [f0]
y.append(y[0] + h * df(y[0], x[0]))
for i in xrange(1, len(x) - 1):
fn = df(y[i], x[i])
fn1 = df(y[i - 1], x[i - 1])
y.append(y[i] + (3 * fn - fn1) * h / 2)
return y
if __name__ == "__main__":
df = lambda y, x: 2.0 / math.sqrt(math.pi) * math.exp(-x * x)
f0 = 0.0
x = np.linspace(-10.0, 10.0, 10000)
y1 = euler(df, f0, x)
y2 = i(df, f0, x)
y3 = odeint(df, f0, x)
plt.plot(x, y1, x, y2, x, y3)
plt.legend(["euler", "modified", "odeint"], loc='best')
plt.grid(True)
plt.show()
And here is a plot:
Am I using odeint in a wrong way or it's a bug?

Notice that if you change x to x = np.linspace(-5.0, 5.0, 10000), then your code works. Therefore, I suspect the problem has something to do with exp(-x*x) being too small when x is very small or very large. [Total speculation: Perhaps the odeint (lsoda) algorithm adapts its stepsize based on values sampled around x = -10 and increases the stepsize in such a way that values around x = 0 are missed?]
The code can be fixed by using the tcrit parameter, which tells odeint to pay special attention around certain critical points.
So, by setting
y3 = integrate.odeint(df, f0, x, tcrit = [0])
we tell odeint to sample more carefully around 0.
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import numpy as np
import math
def euler(df, f0, x):
h = x[1] - x[0]
y = [f0]
for i in xrange(len(x) - 1):
y.append(y[i] + h * df(y[i], x[i]))
return y
def i(df, f0, x):
h = x[1] - x[0]
y = [f0]
y.append(y[0] + h * df(y[0], x[0]))
for i in xrange(1, len(x) - 1):
fn = df(y[i], x[i])
fn1 = df(y[i - 1], x[i - 1])
y.append(y[i] + (3 * fn - fn1) * h / 2)
return y
def df(y, x):
return 2.0 / np.sqrt(np.pi) * np.exp(-x * x)
if __name__ == "__main__":
f0 = 0.0
x = np.linspace(-10.0, 10.0, 10000)
y1 = euler(df, f0, x)
y2 = i(df, f0, x)
y3 = integrate.odeint(df, f0, x, tcrit = [0])
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.legend(["euler", "modified", "odeint"], loc='best')
plt.grid(True)
plt.show()

Related

Plotting a vector field using quiver

I'm trying to reproduce a 2D vector map with components
v = 100/a * exp(-1/a^2 * ((x+0.55)^2+y^2))(-y,x) - 100/a * exp(-1/a^2 * ((x-0.55)^2+y^2))(-y,x)
and here are my codes. It did not give the map I want (see attached vector map). Could someone please help me with it?
import numpy as np
import matplotlib.pyplot as plt
import math
grid_resolution = 25
grid_size = 2*grid_resolution+1
a = 0.2
x = np.linspace(-1,1,grid_size)
y = np.linspace(-1,1,grid_size)
X,Y = np.meshgrid(x, y)
vx = np.zeros((grid_size,grid_size))
vy = np.zeros((grid_size,grid_size))
for i in range(0,grid_size):
for j in range(0,grid_size):
x0 = x[j]
y0 = y[i]
xx = (x0 + 0.55) ** 2 + y0 ** 2
yy = (x0 - 0.55) ** 2 + y0 ** 2
expf1 = math.exp(-xx / (a ** 2))
expf2 = math.exp(-yy / (a ** 2))
vx[i,j] = 100 / a * (-expf1 + expf2) * y0
vy[i,j] = 100 / a * (expf1 - expf2) * x0
fig, ax = plt.subplots()
ax.quiver(X, Y, vx, vy)
ax.set_aspect('equal')
plt.show()
In the last passage, when you compute vx[i,j] and vy[i,j], you are computing vector field components in (x0, y0), while you should compute it in the current point, so (x0 ± 0.55, y0). Moreover, you should change the sign of vx and vy in order to draw a vector field like the one you linked.
import numpy as np
import matplotlib.pyplot as plt
import math
grid_resolution = 25
grid_size = 2*grid_resolution + 1
a = 0.2
x = np.linspace(-1, 1, grid_size)
y = np.linspace(-1, 1, grid_size)
X, Y = np.meshgrid(x, y)
vx = np.zeros((grid_size, grid_size))
vy = np.zeros((grid_size, grid_size))
for i in range(0, grid_size):
for j in range(0, grid_size):
x0 = x[j]
y0 = y[i]
xx = (x0 + 0.55)**2 + y0**2
yy = (x0 - 0.55)**2 + y0**2
expf1 = math.exp(-xx/(a**2))
expf2 = math.exp(-yy/(a**2))
vx[i, j] = -100/a*(-expf1 + expf2)*y0
if x0 > 0:
vy[i, j] = -100/a*(expf1 - expf2)*(x0 - 0.55)
else:
vy[i, j] = -100/a*(expf1 - expf2)*(x0 + 0.55)
fig, ax = plt.subplots()
ax.quiver(X,Y,vx,vy)
ax.set_aspect('equal')
plt.show()

Scipy odeint. Gravitational motion

I solve the motion in gravitational field around the sun with scipy and mathplotlib and have a problem. My solution is not correct. It is not like in example. Formulas that I used.
from scipy.integrate import odeint
import scipy.constants as constants
import numpy as np
import matplotlib.pyplot as plt
M = 1.989 * (10 ** 30)
G = constants.G
alpha = 30
alpha0 = (alpha / 180) * np.pi
v00 = 0.7
v0 = v00 * 1000
def dqdt(q, t):
x = q[0]
y = q[2]
ax = - G * M * (x / ((x ** 2 + y ** 2) ** 1.5))
ay = - G * M * (y / ((x ** 2 + y ** 2) ** 1.5))
return [q[1], ax, q[3], ay]
vx0 = v0 * np.cos(alpha0)
vy0 = v0 * np.sin(alpha0)
x0 = -150 * (10 ** 11)
y0 = 0 * (10 ** 11)
q0 = [x0, vx0, y0, vy0]
N = 1000000
t = np.linspace(0.0, 100000000000.0, N)
pos = odeint(dqdt, q0, t)
x1 = pos[:, 0]
y1 = pos[:, 2]
plt.plot(x1, y1, 0, 0, 'ro')
plt.ylabel('y')
plt.xlabel('x')
plt.grid(True)
plt.show()
How can I fix this?
Maybe you can tell me solution with another method for example with Euler's formula or with using other library.
I will be very greatful if you help me.

Fitting curve with conditions

I'm trying to simulate an exoplanet transit and to determine its orbital characteristics with curve fitting. However, the intersection area between two circles needs to distinguish two cases: if the center of the smallest circle is in the biggest or not. This is a problem for scipy with the function curve_fit, calling an array in my function cacl_aire. The function transit simulates the smallest disc's evolution with time.
Here's my code:
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import xlrd
dt = 0.1
Vx = 0.08
Vy = 0
X0 = -5
Y0 = 0
R = 2
r = 0.7
X = X0
Y = Y0
doc = xlrd.open_workbook("transit data.xlsx")
feuille_1 = doc.sheet_by_index(0)
mag = [feuille_1.cell_value(rowx=k, colx=4) for k in range(115)]
T = [feuille_1.cell_value(rowx=k, colx=3) for k in range(115)]
def calc_aire(r, x, y):
D2 = x * x + y * y
if D2 >= (r + R)**2:
return 0
d = (r**2 - R**2 + D2) / (2 * (D2**0.5))
d2 = D2**0.5 - d
if abs(d) >= r:
return min([r * r * np.pi, R * R * np.pi])
H = (r * r - d * d)**0.5
As = np.arccos(d / r) * r * r - d * H
As2 = R * R * np.arccos(d2 / R) - d2 * H
return As + As2
def transit(t, r, X0, Y0, Vx, Vy):
return -calc_aire(r, X0 + Vx * t, Y0 + Vy * t)
best_vals = curve_fit(transit, T, mag)[0]
print('best_vals: {}'.format(best_vals))
plt.figure()
plt.plot(T, mag)
plt.draw()
I have the following error :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() with the line 28 :
if D2 >= (r + R)**2:
Here is my database:
https://drive.google.com/file/d/1SP12rrHGjjpHfKBQ0l3nVMJDIRCPlkuf/view?usp=sharing
I don't see any trick to solve my problem.

"scipy.optimize.minimize" ignores constraint, derivation is positive everywhere

I have a function z(T,x,p). With my given data points i want to fit the function and get the coefficients of the function. My constraint is that the derivation of z after x should be positive everywhere dz/dx > 0. But in my following code the constraint is not working and I do not know why.
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
T = np.array([262,257,253,261,260,243,300,283,282], dtype=float)
p = np.array([25,22,19,24,24,14,62,45,44], dtype=float)
x = np.array([0.1,0.1,0.2,0.2,0.3,0.3,1,0.3,0.2], dtype=float)
z = np.array([10,9,13,16,20,12,62,37,28], dtype=float)
def func(pars, T, x, p): #my actual function
a,b,c,d,e,f = pars
return x * p + x * (1 - x) * (a + b * T + c * T ** 2 + d * x + e * x * T + f * x * T ** 2) * p
def resid(pars): #residual function
return ((func(pars, T, x, p) - z) ** 2).sum()
def der(pars): # constraint function: Derivation of func() after x positive everywhere
a,b,c,d,e,f = pars
return p+p*(2*x*a+2*x*b*T+2*x*c*T**2+3*x**2*d+3*x**2*e*T+3*x**2*f*T**2)+p*(a+b*T+c*T**2+2*x*d+2*e*x*T+2*f*x*T**2)
con1 = (dict(type='ineq', fun=der))
pars0 = np.array([0,0,0,0,0,0])
res = minimize(resid, pars0, method='cobyla',options={'maxiter': 5000000}, constraints=con1)
print("a = %f , b = %f, c = %f, d = %f, e = %f, f = %f" % (res.x[0], res.x[1], res.x[2], res.x[3], res.x[4], res.x[5]))
Trying to plot an example:
x0 = np.linspace(0, 1, 100) # plot two example graphs z(x) for a certain T and p
fig, ax = plt.subplots()
fig.dpi = 80
ax.plot(x,z,'ro', label='data')
ax.plot(x0, func(res.x, 300, x0, 62), '-', label='fit T=300, p=62')
ax.plot(x0, func(res.x, 283, x0, 45), '-', label='fit T=283, p=45')
plt.xlabel('x')
plt.ylabel('z')
plt.legend()
plt.show()
As you can see the derivation (gradient) is not positive everywhere. I do not know why the constraint gets ignored. Maybe someone can help me.

Plot graph with 4 equations consist of f(x,y, xy)

I'd like to plot a graph which contains 4 functions as shown below:
xy1: - 12.8 x - 0.108 y + xy >= -1.3824
xy2: - 40 x - 5 y + xy >= -200
xy3: - 40 x - 0.108 y + xy <= -4.32
xy4: - 12.8 x - 5 y + xy <= -64
I started off by generating data for x and y
import numpy as np
x = np.linspace(0, 5, 100)
y = np.linspace(0, 40, 100)
But then when I tried to plot the graph, I started to confuse about how should I reformulate the equations so that it is appropriate value for x, y, xy?
import matplotlib.pyplot as plt
plt.plot((-1.3824 + (12.8 * x) + (0.108 * y)), y) <--- this doesn't seems to be right
plt.show()
You can use contour for implicit function plotting. By the way I don't think you can plot multiple functions with inequality on the same graph because you have to show the ranges in the 2D plane, and they overlap each other.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 100)
y = np.linspace(0, 40, 100)
X, Y = np.meshgrid(x, y)
XY1 = (-12.8*X - 0.108*Y + X*Y) >= -1.3824
XY2 = - 40*X - 5*Y + X*Y >= -200
XY3 = - 40*X - 0.108*y + X*Y <= -4.32
XY4 = - 12.8*X - 5*Y + X*Y <= -64
plt.contour(X,Y, XY1)
plt.contour(X,Y, XY2)
plt.contour(X,Y, XY3)
plt.contour(X,Y, XY4)
plt.show()
================================================================
UPDATE:
Seems like sympy's plot_implicit works so much better. You may need to install it.
from sympy import plot_implicit, symbols, And
x, y = symbols('x y')
p1 = plot_implicit(And(-12.8*x - 0.108*y + x*y >= -1.3824), (x,0,5), (y,0,40))
p2 = plot_implicit(And(-40*x - 5*y + x*y >= -200), (x,0,5), (y,0,40))
p3 = plot_implicit(And(-40*x - 0.108*y + x*y <= -4.32), (x,0,5), (y,0,40))
p4 = plot_implicit(And(-12.8*x - 5*y + x*y <= -64), (x,0,5), (y,0,40))
Results:
you can use contourf:
import pylab as pl
import numpy as np
x, y = np.mgrid[-10:10:100j, -10:50:100j]
z1 = - 12.8 * x - 0.108 * y + x * y + 1.3824
z2 = - 40 * x - 5 * y + x * y + 200
z3 = - 40 * x - 0.108 * y + x * y + 4.32
z4 = - 12.8 * x - 5 * y + x * y + 64
fig, axes = pl.subplots(2, 2, figsize=(12, 8))
axes = axes.ravel()
axes[0].contourf(x, y, z1, levels=[0, 1e10], alpha=0.2, colors=["blue"])
axes[1].contourf(x, y, z2, levels=[0, 1e10], alpha=0.2, colors=["green"])
axes[2].contourf(x, y, z3, levels=[-1e10, 0], alpha=0.2, colors=["red"])
axes[3].contourf(x, y, z4, levels=[-1e10, 0], alpha=0.2, colors=["yellow"])
Here is the output:

Categories