Plotting a sinus function [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I try to plot a sinus function. But it throws an error" x and y must have same first dimension, but have shapes (1,) and (51,)" I can't understand they have same dimension. Why do I get this error? Thanks in advance.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from numpy import *
# spatial domain
xmin = 0
xmax = 1
n = 50 # num of grid points
# x grid of n points
x = np.linspace(xmin, xmax, n+1);
k=2
def f1(x):
return np.sin(2*pi*k*x)
plt.plot(f1,x)

Change the bottom of your program to be semantically correct:
plt.plot(f1(x),x)
plt.show()
f1 is a function object; f1(x) is a vector of return values.

Related

Numpy - how interp function works? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to understand how numpy.interp function works. This function has a couple of parameters which are vague to me, like: xp, fp, left and right.
I saw an example of this function as below:
X is a numpy ndarray and y is one-dimensional array:
X = np.interp(X, (X.min(), X.max()), (0, 10))
y = np.interp(y, (y.min(), y.max()), (30000, 100000))
Thanks for helping me out!
x are the points which are not in xp but you want their y values i.e. points where you want to perform interpolation
xp and yp are the main inputs based on which 1D interpolation works (these are the discrete data points through which you want to interpolate)
left and right handle the edge case scenarios when x is out of the closed interval range of [xp_minimum, xp_maximum] i.e. the output values to generate incase of extrapolation

Bifurcation and Lyapunov exponent in Python

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:

Python Matplotlib - How to plot a graph from a table with 2 columns [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
If the table represented in a matrix (a table with 2 columns - y,x columns).
I want to represent the dots on the graph and then to draw a line connecting all the dots.
I didn't find an example for this simple need.
How to achieve it?
As been pointed out in the comment, this information is already available online, with some googling. However, the provided link does not answer how to extract the data from a matrix. Further, there is no example showing the simplest way to plot a line with the data points marked out specifically. Therefore I provide this minimal example showing these two concepts:
import numpy as np
from matplotlib import pyplot as pp
M = np.array([[1., 1],
[4, 2],
[9, 3],
[16, 4],
[25, 5]])
x = M[:,1] # Extracting second column (x values)
y = M[:,0] # Extracting first column (y values)
# The third argument to plot describes the curve.
# o means that there should be dots for the data values
# - (single dash) means that there should be a connecting line
# As can be seen, these options can be combined.
pp.plot(x, y, 'o-')
pp.show()

Interpolate Z values in a 3D surface, starting from an irregular set of points [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a surface that looks like Figure A, imagine that is top view. The surface has calculated Z value.
Now I need to find all Z values in new points like figure B. How to do this? I tried scipy.interpolate.interp2d but it gives some weird results like this:
I just want to find custom z for custom x and y inside the "figure".
Mimimal code example
func_int = scipy.interpolate.interp2d([point[0] for point in pointsbottom],[point[1] for point in pointsbottom],[point[2] for point in pointsbottom], kind = 'linear')
pointscaption = map(lambda point:(point[0],point[1],func_int(point[0],point[1])),pointscaption)
Where pointsbottom is list of (x,y,z) and pointscaption is list of (x,y,z) but I need to find new z.
Try using griddata instead:
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
The difference is griddata expects regular data as input (hmm..., I think). It's not that you should have a different result but more that you'll be able to catch the problem faster. You can mask the "regular grid" data easily.
My first guess would be that those input coordinates are not what you are expecting them to be (perhaps with a different scale from the function you are calculating) but it's difficult to say without testing.
In any case it seems you want a surface, which by definition is a type of grid data, so it should be fairly easy to spot the problem using this different framework.
EDIT (further considerations about the doubts of the poster):
Let's say you want some object that you want to input some data inside. After doing this you want to be able to estimate any position using that data. For that purpose you can build a class like this:
import numpy as np
class Estimation():
def __init__(self,datax,datay,dataz):
self.x = datax
self.y = datay
self.v = dataz
def estimate(self,x,y,using='ISD'):
"""
Estimate point at coordinate x,y based on the input data for this
class.
"""
if using == 'ISD':
return self._isd(x,y)
def _isd(self,x,y):
d = np.sqrt((x-self.x)**2+(y-self.y)**2)
if d.min() > 0:
v = np.sum(self.v*(1/d**2)/np.sum(1/d**2))
return v
else:
return self.v[d.argmin()]
This example is using Inverse Squared Distance method which is very stable for estimation (if you avoid divisions by zero). It won't be fast but I hope it's understandable. From this point on you can estimate any point in 2D space by doing:
e = Estimation(datax,datay,dataz)
newZ = e.estimate(30,55) # the 30 and 55 are just example coordinates
If you were to do this to an entire grid:
datax,datay = np.random.randint(0,100,10),np.random.randint(0,100,10)
dataz = datax/datay
e = Estimation(datax,datay,dataz)
surf = np.zeros((100,100))
for i in range(100):
for j in range(100):
surf[i,j] = e.estimate(i,j)
You would obtain an image that you can see using, for instance, matplotlib (in which the color represents the height in your surface):
import matplotlib.pyplot as plt
plt.imshow(surf.T,origin='lower',interpolation='nearest')
plt.scatter(datax,datay,c=dataz,s=90)
plt.show()
The result of this experiment is this:
If you don't want to use ISD (Inverse Squared Distance) just implement a new method on Estimation class. Is this what you are looking for?

How to use values inside range() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a loop
for i in range(0,1000,100):
and inside it I compute a list which holds 10 values but the loop goes from 0 to 1000.I want to relate these 10 values with the 1000 values; namely, create a list (or array) which will hold these values (the 10 to 1000 values).
UPDATED------------------------------------------
I want to make a plot which will have in the horizontal axis values from 0 to 1000 and in the vertical axis the 10 values of the list that i computed.
Your question is very unclear.
From your comment it seems like you're asking about matplotlib? Do you want something like this?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1000, 100)
# As a placeholder for your calculation...
y = np.cos(x / 100.0)
plt.plot(x, y, marker='o', mfc='red')
plt.show()
Do you want to access the list elements while retaining the range from 0 to 1000 in steps of 100? If so, this should be a way.
mylst=[12,5,6,34,6,11,78,1,1,88]
for i in range(0,1000,100):
print mylst[i/100]
Not sure... a more detailed question could help.

Categories

Resources