I am a relative beginner when it comes to python, and I currently am trying to figure out some python for a problem I have.
I am attempting to calculate the lyapunov exponent of a bifurcation diagram I am supposed to be creating.
The equation is X_(n+1) = asin(pi x_(n)),
where a = 0.9 (for when I calculate the exponent)
This is currently the code that i have set up to create an array of values becoming large.
import numpy as np
np.set_printoptions(threshold=np.nan)
import matplotlib.pyplot as plt
a = np.linspace(0,1)
xn = np.array([.001], dtype = float)
for i in range(0,10000):
y = a*np.sin(np.pi*xn[i])
xn = np.append(xn,y)
plt.plot(a,xn[-1])
However, very obviously, when i plot xn, i just get a mad mess of dots instead of a bifurcation diagram. I was hoping I could get some guidance as to moving towards the correct diagram which i can hopefully use to get closer to my end goal.
Thanks for any help, I appreciate it!
I'm not exactly sure what you are trying to accomplish, and I don't know enough about bifurcations to really figure it out on my own, but I was able to get something that seems to work. The main caveat seems to be that if alpha starts at less than 0.158, it won't produce the right output.
import numpy as np
import matplotlib.pyplot as plt
x = [0.001]
a = np.linspace(0.2,1,100000)
for i in range(1,a.shape[0]):
x.append(a[i]*np.sin(np.pi*x[i-1]))
fig = plt.figure(figsize=(8,4))
plt.scatter(a,x,s=0.1)
which produces the figure:
Related
I am trying to randomise the magnitude component of the spectrum of the following image. I was thinking of using numpy.random.rand() for the generation of random values however I am not sure how to go about it. Is someone able to explain how I can proceed?
The following is the code to obtain the magnitude spectrum of the image attached.
import numpy as np
import cv2
import matplotlib.pyplot as plt
img_tp = cv2.imread('testpattern.png', 0)
f_tp = np.fft.fft2(img_tp)
fshift_tp = np.fft.fftshift(f_tp)
mag_spectrum_tp = 20*np.log(np.abs(fshift_tp))
plt.imshow(mag_spectrum_tp)
I don't see how that would be useful in any way. The phase information doesn't have any real meaning without the magnitude. Theoretically, you can combine unrelated magnitude and phase values by doing something like this:
f_mag = np.abs( fft_result1 )
f_phase = np.angle( fft_result2 )
combined = np.multiply(f_mag, np.exp(1j*f_phase))
img = np.fft.iff2(combined)
but the results are pretty much going to be garbage.
I tried to generate a 1000 points in 2D uniformly distributed on a rectangle of dimensions [-1,1]x[0,0.5], then plot the points, but I couldn't. I get this error.
typeError: 'float' object cannot be interpreted as an integer.
Here is the code I came up with:
import matplotlib.pyplot as plt
vect = np.random.uniform(1000)
plt.plot(range(-1,1), range(0,0.5), vect)
plt.show()
I think I don't really understand how to do it. Should I use np.random.randn or np.random.rand? I would have to have some explanations about what I did wrong. (if possible to each line so that i can better understand)
Thank you
first: xlist = np.random.uniform(1000,low=-1, high = 1)
second ylist = np.random.uniform(1000,low=0, high = 0.5)
last plt.scatter(xlist, ylist)
This is my code. I want to get a typical sine graph but somehow am not getting it.
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,2*(np.pi),(np.pi)/2)
y=np.sin(x)
plt.plot(x,y,color='b')
plt.show()
I am getting this graph.
1
Also, what would I need to modify to the axes so that it would look like this ?
2
Look at the step size in your range:
x=np.arange(0,2*(np.pi),(np.pi)/2)
You're evaluating sin every pi/2 ... in other words, only at -1, 0 and 1.
You need a much smaller step size ... say, np.pi / 100
For future problems, see this lovely reference for debugging help. Simply printing x would have shown your problem.
I have been using Matlab for a few years which is quite easy (in my opinion) and powerful when it comes to 3D-plots such as surf, contour or contourf.
It seems at least more unintuitive to me to do the same in Python.
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0,100,0.1) # time domain
sp = np.arange(0,50,0.2) # spatial domain
c = 0.5
u0 = np.exp(-(sp-5)**2)
u = np.empty((len(t),len(sp))
for i in range(0,len(t)):
u[i][:] = u0*(sp-c*t)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(t,sp,u)
plt.show()
So, in Matlab it would be that easy I think.
What do I have to do in order to get a 3D-Plot (surface or whatever) with two arrays for the x and y dimensions with different sizes and a z-matrix giving a value to each grid point?
As this is a basic question, feel free to explain a bit more or just give me a link with an answer. Unfortunately, I do not really understand what is happening in the codes I read regarding this problem so far.
I don't think what you have written would work in matlab either (I may be wrong, I haven't used it in a while).
To do a plot_surface(X, Y, Z), X, Y, Z must be 2D arrays of equal size. So, just like you would do in matlab:
T, SP = numpy.meshgrid(t, sp)
plot_surface(T, SP, u)
I'm making a demonstration of a different types of regression in numpy with ipython, and so far, I've been able to plot a simple linear regression without difficulty. Now, when I go on to make a quadratic fit to my data and go to plot it, I don't get a quadratic curve but instead get many lines. Here's the code I'm running that generates the problem:
import numpy
from numpy import random
from matplotlib import pyplot as plt
import math
# Generate random data
X = random.random((100,1))
epsilon=random.randn(100,1)
f = 3+5*X+epsilon
# least squares system
A =numpy.array([numpy.ones((100,1)),X,X**2])
A = numpy.squeeze(A)
A = A.T
quadfit = numpy.linalg.solve(numpy.dot(A.transpose(),A),numpy.dot(A.transpose(),f))
# plot the data and the fitted parabola
qdbeta0,qdbeta1,qdbeta2 = quadfit[0][0],quadfit[1][0],quadfit[2][0]
plt.scatter(X,f)
plt.plot(X,qdbeta0+qdbeta1*X+qdbeta2*X**2)
plt.show()
What I get is this picture (zoomed in to show the problem):
You can see that rather than having a single parabola that fits the data, I have a huge number of individual lines doing something that I'm not sure of. Any help would be greatly appreciated.
Your X is ordered randomly, so it's not a good set of x values to use to draw one continuous line, because it has to double back on itself. You could sort it, I guess, but TBH I'd just make a new array of x coordinates and use those:
plt.scatter(X,f)
x = np.linspace(0, 1, 1000)
plt.plot(x,qdbeta0+qdbeta1*x+qdbeta2*x**2)
gives me