Bifurcation diagram in matplotlib - python

I'm trying to acquire the bifurcation diagram for the equation below:
(x is a function of t)
as:
And here is my snippet:
import numpy as np
import matplotlib.pyplot as plt
def pitch(r, x):
return r * x + np.power(x,3)- np.power(x,5)
n = 10000
r = np.linspace(-200, 200, n)
iterations = 1000
last = 100
x = 0
for i in range(iterations):
x = pitch(r,x)
if i >= (iterations - last):
plt.plot(r,x, ',k', alpha=0.02)
plt.title("Bifurcation diagram")
plt.show()
But the generated plot is not what it is supposed to be:
Edit:
Here is my recent attempt:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def pitch(s,x,r):
x = s[0]
dxdt = r * x + np.power(x,3)- np.power(x,5)
return [dxdt]
t = np.linspace(0,100)
s0=[-50]
r = np.linspace(-200, 200)
for i in r:
s = odeint(pitch,s0,t, args=(i,))
plt.plot(s,i,',k', alpha=0.02)
plt.title("Bifurcation diagram")
plt.show()
With this error:
raise ValueError("x and y must have same first dimension") ValueError:
x and y must have same first dimension
Could you give me some advice to fix this problem?!

I found a link to this post and decided to post a few remarks that might be helpful to someone stumbling upon it in the future.
I did not analyze the equation in detail but it is clear from the first sight that something interesting would happen when r is close to 0.
So we could study the behavior of the system for r in [-10,10]
You are right to use odeint instead of solving the Cauchy problem using Euler method coded by yourself.
This equation has an attractor in that it soon "forgets" the initial condition and slides towards the attractor, yet the choice of the attractor depends on where in relation to 0 do we start. Large positive initial conditions would slide to the negative attractor and vice versa as - x^5 is the term that defines the behavior at large x.
What we need to do is for each r in the range put a mark at the attractor that the solution slides to for each initial condition.
We first create a canvas to put marks into:
diagram = np.zeros((200,200))
And then for each combination of (r,s0) we put a point on the canvas at (r,s[-1]).
Here is the complete code
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def pitch(s,x,r):
x = s[0]
dxdt = r * x + np.power(x,3)- np.power(x,5)
return [dxdt]
t = np.arange(0,100,2)
s0=[-50]
N = 200 # Number of points along each side of the diagram
diagram = np.zeros((N,N))
rmin,rmax = -10,10
rrange = np.arange(rmin, rmax,(rmax-rmin)/N)
smin,smax = -5.0,5.0
srange = np.arange(smin,smax,2*(smax-smin)/N)
for i in rrange:
for s0 in srange:
s = odeint(pitch,[s0],t, args=(i,))
imgx = int((i-rmin)*N/(rmax-rmin))
imgy = int((s[-1]-smin)/(smax-smin)*N)
imgx = min(N-1,max(0,imgx)) # make sure we stay
imgy = min(N-1,max(0,imgy)) # within the diagram bounds
diagram[imgy,imgx] = 1
plt.title("Bifurcation diagram")
plt.imshow(np.flipud(diagram),cmap=cm.Greys,
extent=[rmin,rmax,smin,smax],aspect=(rmax-rmin)/(smax-smin))
plt.xlabel("r")
plt.ylabel("x")
plt.show()
And the resulting plot
When you zoom in into the region around 0 by setting (rmin,rmax) to (-0.5,0.5) you could see that the branches of the diagram do not start at 0
Instead as in the diagram drawn in the original post the branches start at roughly r=-0.25

Related

Generating points within a Menger Sponge (fractal shape)

I am trying to generate a lattice of points in the shape of a Menger sponge or Sierpinski sponge.
https://en.wikipedia.org/wiki/Menger_sponge This link details how the shape is mathematically constructed.
I wanted to find a way where I could make this shape using recursion to remove the necessary cubes.
I looked online but I could only find code which generated 3d renderings of the shape and not a lattice of points.
It is worth mentioning that I am not familiar with OO programming which seemed to be the general method the examples I found used.
I then tried to make a 2D version to see if I could implement it, but the only version I got to work was by manually subtracting the areas needed.
This is what I've gotten to work, only removing the first square from the centre:
`
import numpy as np
import matplotlib.pyplot as plt
size = 12
x = []
y = []
for index_x in np.arange(size):
for index_y in np.arange(size):
x = np.append(x, index_x)
y = np.append(y, index_y)
# step 1: remove central box
x_box = []
y_box = []
for index_1 in np.arange(144):
if (x[index_1] < size/3 or x[index_1] >= 2/3 * size or
y[index_1] < size/3 or y[index_1] >= 2/3 * size):
x_box = np.append(x_box, x[index_1])
y_box = np.append(y_box, y[index_1])
# step 2: remove central square in each surrounding square
# Do the same steps as above but for the other smaller squares
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x_box, y_box)
ax.set_title('Menger Sponge')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
`
This is what my code produces.
Is there an easier / better way of implementing this?
You need to add a recursive element to your code. I would also suggest thinking in terms of 2D (and eventually 3D) matricies instead of 1D arrays and explore numpy's abilities in depth:
import numpy as np
def menger(matrix, size):
quotient, remainder = divmod(size, 3)
if remainder == 0:
for x in np.arange(0, size, quotient):
for y in np.arange(0, size, quotient):
view = matrix[x:x + quotient, y:y + quotient]
if (x // quotient) % 3 == 1 and (y // quotient) % 3 == 1:
view *= 0
menger(view, quotient)
if __name__ == "__main__":
import matplotlib.pyplot as plt
SIZE = 27
matrix = np.ones((SIZE, SIZE))
menger(matrix, SIZE)
plt.matshow(matrix)
plt.colorbar()
plt.show()

I can't animate a surface with Mayavi

I'm trying to do a simple animation of a surface with Mayavi, but due to the lack of online examples (the few are extremely unclear or not useful) and official documentation, I'm struggling a lot. I wrote a very simple code to show where I've gone so far
import numpy as np
from mayavi import mlab
import time
#Meshgrid + u + Surface
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
u = np.ones((10,10))
surf = mlab.mesh(X,Y,u)
#Surface animation
#mlab.animate(delay=1000)
def anim():
for n in range(1,10):
global u
print(n)
u = u+1
surf.mlab_source.scalars = u
yield
anim()
mlab.show()
So very simple, every iteration I pass from a 10x10 matrix of ones to a 10x10 matrix of twos and so on.
The problems I've encountered are 3
I really can't understand what mlab_source.scalars does: fundamentally I don't know how to tell it that the z axis is changing and in doing so it follows the variation of u
Mayavi opens up, but the grid is all plane and black, like there is some issues with u
I don't understand what yield does: in the actual code I'm writing it stops the computation in a lot of advance (like in 300 steps it stops it at the step 29), but I can't remove it
I really can't get my head around, it's weeks I'm trying, hope somebody helps. Thanks in advance.
Edit:
I tried to extend #E.Klahn code in a case resembling more mine
import numpy as np
from mayavi import mlab
s = 0.01
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
Z = np.ones((10,10))
m = mlab.mesh(X, Y, Z)
#mlab.animate(delay=20)
def anim():
for i in range(1,100):
m.mlab_source.z = np.ones((10,10))*s*i
yield
anim()
mlab.show()
But I obtain only a flat surface going up, not a tridimensional object evolving, such as in his code.
Edit2:
Here the working code just copying the example of #E.Klahn, which I thank very very much
import numpy as np
from mayavi import mlab
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
X,Y=X.T,Y.T #seems an important command
u = np.ones((10,10))
surf = mlab.surf(X,Y,u)
#mlab.animate(delay=500)
def anim():
for n in range(1,10):
print(n)
surf.mlab_source.scalars = 1+np.sin(X)*np.sin(Y)*np.sin(n)
yield
anim()
mlab.show()
I just print it to give continuity to code above and to spot the differences.
Here I've shown the code to plot a cube and animate it so that it grows out from a height of 0.01 to a height of 1 using mlab.mesh. What yield does is that it returns control to the decorator so that the scene can be updated.
import numpy as np
from mayavi import mlab
s = 0.01
X = np.array([[0,0,1,1],[0,0,1,1],[0,0,0,0],[1,1,1,1]])
Y = np.array([[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]])
Z = np.array([[0,0,0,0],[s,s,s,s],[0,0,s,s],[0,0,s,s]])
m = mlab.mesh(X, Y, Z)
#mlab.animate(delay=20)
def anim():
for i in range(1,101):
print(i, end='\r')
m.mlab_source.z = np.array([[0,0,0,0],[s*i,s*i,s*i,s*i],[0,0,s*i,s*i],[0,0,s*i,s*i]])
yield
anim()
mlab.show()
scalars is one way to access the underlying data structure if that data structure has the scalars attribute. However, for mesh that attribute is not used, rather you want to access z and set those values. I would not know what your specific issue with u if unless there is a traceback to work from. The same with yield - I see no reason why that keyword would stop the animation at any particular step.
The code below shows, instead of a box evolving over time, a surface evolving with time.
import numpy as np
from mayavi import mlab
def evolving_function(X, Y, t, Lx=1, Ly=1):
return np.sin(X+Lx*t)*np.sin(Y+Ly*t)
Lx, Ly = 1,2
x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)
X,Y = np.meshgrid(x,y)
X, Y = X.T, Y.T
s = evolving_function(X, Y, 0, Lx=Lx, Ly=Ly)
m = mlab.surf(X, Y, s)
dt = 50
t = 10
steps = int(10*1000/dt)
#mlab.animate(delay=dt)
def anim():
for i in range(1,steps):
m.mlab_source.scalars = evolving_function(X, Y, dt*i, Lx=Lx, Ly=Ly)
yield
anim()
mlab.show()

How to put initial condition of ODE at a specific time point using odeint in Python?

How to put initial condition of ODE at a specific time point using odeint in Python?
So I have y(0) = 5 as initial condition,
following code works::
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
# function that returns dy/dt
def model(y,t):
k = 0.3
dydt = -k * y
return dydt
# initial condition
y0 = 5
# time points
t = np.linspace(0,20)
# solve ODE
y = odeint(model,y0,t)
# plot results
plt.plot(t,y)
plt.xlabel('time')
plt.ylabel('y(t)')
plt.show()
I wanna see the graph in both negative and positive time line.
So I change t = np.linspace(0,20) to t = np.linspace(-5,20), but then the initial condition is taken as y(-5) = 5.
How to solve this?
I do not think you can, according to the docs
But you can solve for positive and negative t's separately and then stich them together. Replace the relevant lines with
tp = np.linspace(0,20)
tm = np.linspace(0,-5)
# solve ODE
yp = odeint(model,y0,tp)
ym = odeint(model,y0,tm)
# stich together; note we flip the time direction with [::-1] construct
t = np.concatenate([tm[::-1],tp])
y = np.concatenate([ym[::-1],yp])
this produces

Scaling x-axis after IFFT-FFT

See the edit below for details.
I have a dataset, on which I need to perform and IFFT, cut the valueable part of it (by multiplying with a gaussian curve), then FFT back.
First is in angular frequency domain, so an IFFT leads to time domain. Then FFT-ing back should lead to angular frequency again, but I can't seem to find a solution how to get back the original domain. Of course it's easy on the y-values:
yf = np.fft.ifft(y)
#cut the valueable part there..
np.fft.fft(yf)
For the x-value transforms I'm using np.fft.fftfreq the following way:
# x is in ang. frequency domain, that's the reason for the 2*np.pi division
t = np.fft.fftfreq(len(x), d=(x[1]-x[0])/(2*np.pi))
However doing
x = np.fft.fftfreq(len(t), d=2*np.pi*(t[1]-t[0]))
completely not giving me back the original x values. Is that something I'm misunderstanding?
The question can be asked generalized, for example:
import numpy as np
x = np.arange(100)
xx = np.fft.fftfreq(len(x), d = x[1]-x[0])
# how to get back the original x from xx? Is it even possible?
I've tried to use a temporal variable where I store the original x values, but it's not too elegant. I'm looking for some kind of inverse of fftfreq, and in general the possible best solution for that problem.
Thank you.
EDIT:
I will provide the code at the end.
I have a dataset which has angular frequency on x axis and intensity on the y. I want to perfrom IFFT to change to time domain. Unfortunately the x values are not
evenly spaced, so a (linear) interpolation is needed first before IFFT. Then in time domain the transform looks like this:
The next step is to cut one of the symmetrical spikes with a gaussian curve, then FFT back to angular frequency domain (the same where we started). My problem is when I transfrom the x-axis for the IFFT (which I think is correct), I can't get back into the original angular frequency domain. Here is the code, which includes the generator for the dataset too.
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy.interpolate import interp1d
C_LIGHT = 299.792
# for easier case, this is zero, so it can be ignored.
def _disp(x, GD=0, GDD=0, TOD=0, FOD=0, QOD=0):
return x*GD+(GDD/2)*x**2+(TOD/6)*x**3+(FOD/24)*x**4+(QOD/120)*x**5
# the generator to make sample datasets
def generator(start, stop, center, delay, GD=0, GDD=0, TOD=0, FOD=0, QOD=0, resolution=0.1, pulse_duration=15, chirp=0):
window = (np.sqrt(1+chirp**2)*8*np.log(2))/(pulse_duration**2)
lamend = (2*np.pi*C_LIGHT)/start
lamstart = (2*np.pi*C_LIGHT)/stop
lam = np.arange(lamstart, lamend+resolution, resolution)
omega = (2*np.pi*C_LIGHT)/lam
relom = omega-center
i_r = np.exp(-(relom)**2/(window))
i_s = np.exp(-(relom)**2/(window))
i = i_r + i_s + 2*np.sqrt(i_r*i_s)*np.cos(_disp(relom, GD=GD, GDD=GDD, TOD=TOD, FOD=FOD, QOD=QOD)+delay*omega)
#since the _disp polynomial is set to be zero, it's just cos(delay*omega)
return omega, i
def interpol(x,y):
''' Simple linear interpolation '''
xs = np.linspace(x[0], x[-1], len(x))
intp = interp1d(x, y, kind='linear', fill_value = 'extrapolate')
ys = intp(xs)
return xs, ys
def ifft_method(initSpectrumX, initSpectrumY, interpolate=True):
if len(initSpectrumY) > 0 and len(initSpectrumX) > 0:
Ydata = initSpectrumY
Xdata = initSpectrumX
else:
raise ValueError
N = len(Xdata)
if interpolate:
Xdata, Ydata = interpol(Xdata, Ydata)
# the (2*np.pi) division is because we have angular frequency, not frequency
xf = np.fft.fftfreq(N, d=(Xdata[1]-Xdata[0])/(2*np.pi)) * N * Xdata[-1]/(N-1)
yf = np.fft.ifft(Ydata)
else:
pass # some irrelevant code there
return xf, yf
def fft_method(initSpectrumX ,initSpectrumY):
if len(initSpectrumY) > 0 and len(initSpectrumX) > 0:
Ydata = initSpectrumY
Xdata = initSpectrumX
else:
raise ValueError
yf = np.fft.fft(Ydata)
xf = np.fft.fftfreq(len(Xdata), d=(Xdata[1]-Xdata[0])*2*np.pi)
# the problem is there, where I transform the x values.
xf = np.fft.ifftshift(xf)
return xf, yf
# the generated data
x, y = generator(1, 3, 2, delay = 1500, resolution = 0.1)
# plt.plot(x,y)
xx, yy = ifft_method(x,y)
#if the x values are correctly scaled, the two symmetrical spikes should appear exactly at delay value
# plt.plot(xx, np.abs(yy))
#do the cutting there, which is also irrelevant now
# the problem is there, in fft_method. The x values are not the same as before transforms.
xxx, yyy = fft_method(xx, yy)
plt.plot(xxx, np.abs(yyy))
#and it should look like this:
#xs = np.linspace(x[0], x[-1], len(x))
#plt.plot(xs, np.abs(yyy))
plt.grid()
plt.show()

How can I implement bivariate normal Gaussian noise?

I want to implement complex standard Gaussian noise in python or C. This figure shows what I want to implement.
And first I implement it in python, like this.
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pylab as pl
size = 100000
BIN = 70
x = np.random.normal(0.0,1.0,size)
y = np.random.normal(0.0,1.0,size)
xhist = pl.hist(x,bins = BIN,range=(-3.5,3.5),normed = True)
yhist = pl.hist(y,bins = BIN,range=(-3.5,3.5),normed = True)
xmesh = np.arange(-3.5,3.5,0.1)
ymesh = np.arange(-3.5,3.5,0.1)
Z = np.zeros((BIN,BIN))
for i in range(BIN):
for j in range(BIN):
Z[i][j] = xhist[0][i] + yhist[0][j]
X,Y = np.meshgrid(xmesh,ymesh)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_wireframe(X,Y,Z)
plt.show()
However, it is not standard complex Gaussian noise.
The output figure become:
I think Gaussian noises are additive, however, why it become so different?
I already tried to change the parts of code
x = np.random.normal(0.0,1.0,size)
y = np.random.normal(0.0,1.0,size)
to
r = np.random.normal(0.0,1.0,size)
theta = np.random.uniform(0.0,2*np.pi,size)
x = r * np.cos(theta)
y = r * np.sin(theta)
however, the result was same.
Please tell me the correct implementation or equation of bivariate standard Gaussian noise.
So sorry.It's my mistake.
Joint probability is defined by the product, not summation. I was a perfect fool!
So
Z[i][j] = xhist[0][i] + yhist[0][j]
term must become
Z[i][j] = xhist[0][i] * yhist[0][j]
And I checked
for i in range(BIN):
for j in range(BIN):
integral = integral + Z[i][j] * 0.01
will be 1.0.
So if we need complex standard Gaussian noise, we should do adding the real standard Gaussian noise to real part and imaginary part.
This is the graph for comparing.

Categories