i wrote a code to generate a set of random points on the surface of concentric spheres,where on each sphere I would generate a certain number of points depending on how many concentric sphere surfaces I want as seen in the code written below,you can try to run it and maybe get a better understanding of the issue at hand:
class point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
R_list=(0.001 , 0.002, 0.003 , 0.004 ,0.005)
num_points = 1000/len(R_list)
LD1 = 0.05
LD23 = 0.025
obj_points = []
condition = True
for R in R_list:
while len(rpoints) < num_points:
theta = np.random.uniform(-math.pi, math.pi)
phi = np.random.uniform(-math.pi, math.pi)
pi = R * math.cos(theta) * math.sin(phi) + (LD1 / 2), R * math.sin(theta) * math.sin(phi) + (
LD23 / 2), R * math.cos(phi) + (LD23 / 2)
obj_points.append(point(pi[0], pi[1], pi[2]))
# plott array
lt = len(obj_points)
Vis_arr = np.zeros((lt, 3))
n = 0
for n in range(0, lt-1 ):
Vis_arr[n][0] = obj_points[n].x
Vis_arr[n][1] = obj_points[n].y
Vis_arr[n][2] = obj_points[n].z
# scattering of points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# scattering of points
ax.scatter3D(Vis_arr[:, 0], Vis_arr[:, 1], Vis_arr[:, 2], marker="o")
# graph output fully no evolution
plt.show()
but when I run the code a random point appears out of nowhere:
I suspect that this is an error in creating the arrays of points but I cant detect the exact reason of the problem.
You have two errors in your code:
while len(rpoints) < num_points:
should be:
while len(obj_points) < num_points:
and
for n in range(0, lt-1 ):
should be:
for n in range(0, lt ):
Related
Programming in Python (Blender):
I want to create a square and print all vertices (A;B;C;D) into my console on top of a given Vector. The square should be orthogonal to this vector, like this:
def create_verts_around_point(radius, vert):
# given Vector
vec = np.array([vert[0], vert[1], vert[2]])
# side_length of square
side_length = radius
# Vctor x-direction (1,0,0)
x_vec = np.array([1,0,0])
# Vekctor y-direction (0,1,0)
y_vec = np.array([0,1,0])
# Vector z-direction (0,0,1)
z_vec = np.array([0,0,1])
p1 = vec + (side_length/2) * x_vec + (side_length/2) * y_vec + (side_length/2) * z_vec
p2 = vec - (side_length/2) * x_vec + (side_length/2) * y_vec + (side_length/2) * z_vec
p3 = vec - (side_length/2) * x_vec - (side_length/2) * y_vec + (side_length/2) * z_vec
p4 = vec + (side_length/2) * x_vec - (side_length/2) * y_vec + (side_length/2) * z_vec
But my output looks like this in the end (Square is always parallel to my x-axis and y-axis):
I don't think you're really thinking about this problem in 3D, but see if this is close.
I create a square, perpendicular to the X axis. I then rotate that square based on the angles in x, y, and z. I then position the square at the end of the vector and plot it. I add plot points for the origin and the end of the vector, and I duplicate the last point in the square do it draws all the lines.
import math
import numpy as np
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
def create_verts_around_point(sides, vert):
x0, y0, z0 = vert
# Here is the unrotated square.
half = sides/2
square = [
[0, -half,-half],
[0, -half, half],
[0, half, half],
[0, half,-half],
]
# Now find the rotation in each direction.
thetax = math.atan2( z0, y0 )
thetay = math.atan2( z0, x0 )
thetaz = math.atan2( y0, x0 )
# Now rotate the cube, first in x.
cubes = []
txcos = math.cos(thetax)
txsin = math.sin(thetax)
tycos = math.cos(thetay)
tysin = math.sin(thetay)
tzcos = math.cos(thetaz)
tzsin = math.sin(thetaz)
for x,y,z in square:
x,y,z = (x, y * txcos - z * txsin, y * txsin + z * txcos)
x,y,z = (x * txcos - z * txsin, y, x * txsin + z * txcos)
x,y,z = (x * txcos - y * txsin, x * txsin + y * txcos, z)
cubes.append( (x0+x, y0+y, z0+z) )
return cubes
point = (10,10,10)
square = create_verts_around_point(5, point)
points = [(0,0,0),point] + square + [square[0]]
x = [p[0] for p in points]
y = [p[1] for p in points]
z = [p[2] for p in points]
ax = plt.figure().add_subplot(111, projection='3d')
ax.plot( x, y, z )
plt.show()
Output:
I am trying to rewrite this article:We draw, programming. Machine-generated generation of artistic patterns in vector fields (Russian language) from pseudo-code in Python. I am new to ML, hence the following question arises: How to build a grid of angles and output it through PyCharm? I am at this stage:
import numpy as np
import math
import matplotlib.pyplot as plt
width = 100
height = 100
left_x = int(width * -0.5)
right_x = int(width * 1.5)
top_y = int(height * -0.5)
bottom_y = int(height * 1.5)
resolution = int(width * 0.01)
num_columns = int((right_x - left_x) / resolution)
num_rows = int((bottom_y - top_y) / resolution)
grid=np.ndarray((num_columns, num_rows))
grid[:,:]=math.pi * 0.25
In this code, I create a grid array in which 200 rows and 200 columns, into which the angle 'default_angle' is inserted. Please tell me whether I’m moving in the right direction and how to "draw" a grid, as in an attached link. So far I think I need to use matplotlib.
I believe you need to take a look at meshgrid from numpy
from the meshgrid documentation examples:
x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xx, yy = np.meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)
Edit. After seeing you r link a better resource is the matplotlib quiver demo
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(-10, 10, 1)
Y = np.arange(-10, 10, 1)
U, V = np.meshgrid(X, Y)
fig, ax = plt.subplots()
q = ax.quiver(X, Y, U, V)
ax.quiverkey(q, X=0.3, Y=1.1, U=10,
label='Quiver key, length = 10', labelpos='E')
plt.show()
You need to make several steps to recreate this:
create vector field based on some function or equation
normalize arrows for proper display
draw line
3.1. set starting parameters
3.2. set while out condition
3.3. calculate new position based on angle from starting point
3.4. get new position index --> net new angle
3.5. update starting positions
draw vector field and line
import numpy as np
import matplotlib.pyplot as plt
size = 50
X = np.arange(1, size, 1)
Y = np.arange(1, size, 1)
U, V = np.meshgrid(X, Y)
# Normalize the arrows:
U = U / np.sqrt(U**2 + V**2)
V = V / np.sqrt(U**2 + V**2)
# create angles field
data = []
for i in np.linspace(0, 180, Y.shape[0]):
data.append([i]*X.shape[0])
angle = np.array(data)
# set starting parameters
x_start_position = 2
y_start_position = 2
step_length = 1.0
point_angle = angle[x_start_position, y_start_position]
line_coordinates = [[x_start_position, y_start_position]]
# collect line points for each step
while x_start_position >= 2:
# calculate tep based on angle
x_step = step_length * np.cos(point_angle*np.pi/180)
y_step = step_length * np.sin(point_angle*np.pi/180)
# calculate new position
x_new_position = x_start_position + x_step
y_new_position = y_start_position + y_step
# get array index of new position
x_new_index = int(x_new_position)
y_new_index = int(y_new_position)
# get new angle
point_angle = angle[y_new_index, x_new_index]
# update start position
x_start_position = x_new_position
y_start_position = y_new_position
# collect results
line_coordinates.append([x_new_position, y_new_position])
# set line coordinates
line_data = np.array(line_coordinates)
x_line = line_data[:,0]
y_line = line_data[:,1]
# plot field
plt.quiver(X, Y, U, V, color='black', angles=angle, width=0.005)
# plot line
plt.plot(x_line, y_line, '-', color='red')
plt.show()
Output:
I am attempting to plot the nullcline (steady state) curves of the Oregonator model to assert the existence of a limit cycle by applying the Poincare-Bendixson Theorem. I am close, but for some reason the plot that is produced shows two straight lines. I think it has something to do with the plotting stage. Any ideas?
Also any hints for how to construct a quadrilateral to apply the theorem with would be most appreciated.
Code:
import numpy as np
import matplotlib.pyplot as plt
# Dimensionless parameters
eps = 0.04
q = 0.0008
f = 1
# Oregonator model as numpy array
def Sys(Y, t = 0):
return np.array((Y[0] * (1 - Y[0] - ((Y[0] - q) * f * Y[1]) / (Y[0] + q)) / eps, Y[0] - Y[1] ))
# Oregonator model steady states
def g(x,z):
return (x * (1 - x) + ((q - x) * f * z) / (q + x)) / eps
def h(x,z):
return x - z
# Initial lists containing values
x = []
z = []
def sys(iv1, iv2, dt, time):
# initial values:
x.append(iv1)
z.append(iv2)
# Compute and fill lists
for i in range(time):
x.append(x[i] + (g(x[i],z[i])) * dt)
z.append(z[i] + (h(x[i],z[i])) * dt)
return x, z
sys(1, 0.5, 0.01, 30)
# Locate and find equilibrium points
eqp = []
def find_fixed_points(r):
for x in range(r):
for z in range(r):
if ((g(x, z) == 0) and (h(x, z) == 0)):
eqp.append((x,z))
return eqp
# Plot nullclines
plt.plot([0,2],[2,0], 'r-', lw=2, label='x-nullcline')
plt.plot([1,1],[0,2], 'b-', lw=2, label='z-nullcline')
# Plot equilibrium points
for point in eqp:
plt.plot(point[0],point[1],"red", marker = "o", markersize = 10.0)
plt.legend(loc='best')
x = np.linspace(0, 2, 20)
z = np.linspace(0, 2, 20)
X1 , Z1 = np.meshgrid(x, z) # Create a grid
DX1, DZ1 = Sys([X1, Z1]) # Compute reaction rate on the grid
M = (np.hypot(DX1, DZ1)) # Norm reaction rate
M[ M == 0] = 1. # Avoid zero division errors
DX1 /= M # Normalise each arrows
DZ1 /= M
plt.quiver(X1, Z1, DX1, DZ1, M, pivot='mid')
plt.xlabel("x(\u03C4)")
plt.ylabel("z(\u03C4)")
plt.legend()
plt.grid()
plt.show()
I need to assign value to points in a 3D array that are inside an ellipsoid.
The ellipsoid equation should be something like this:
r=b.sin(u)
x=r.cos(v)
y=r.sin(v)
z=a.cos(u).
But I think that this is only visual. I already tried something with a mask over a cubic array:
a, b = (size-1)/2, (size-1)/2
n = size
r = (size-1)/2
y,x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r # circle mask
array = np.zeros((n, n, n))
array[mask] = 10
But this creates a circle only in x and y which gives me: /.
It's not a sphere. (and I need an ellipsoid).
Any ideas?
mask = x*x + y*y <= r*r gives you a circle, because that's the equation for a circle.
By the same rationale,
mask = x*x + y*y + z*z <= r*r should give you a sphere, and
mask = x*x/(a*a) + y*y/(b*b) + z*z/(c*c) <= r*r should give you an ellipsoid with principal axes of half-length a, b, and c.
Of course, you'll have to create a z array in a way akin to that in which you create your x and y arrays.
For me the easiest way would be to use the coordinate equations for a sphere and work from there.
x = a * cos(u) * cos(v)
y = b * cos(u) * sin(v)
z = c * sin(u)
You can construct these coordinates with np.meshgrid and then plot.
a, b, c = 4, 8, 6
space = np.linspace(0, 2 * np.pi, 50)
u, v = np.meshgrid(space)
x = a * np.cos(u) * np.cos(v)
y = b * np.cos(u) * np.sin(v)
z = c * np.sin(u)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
fig.show()
Update
To get the interior coordinates of the sphere you would use the mask akin to your example, but with the ellipsoid implicit equation. x^2/a^2 + y^2/b^2 + z^2/c^2 = 1
a, b, c = 4, 8, 6
xs, ys, zs = np.mgrid[-a + 1:a + 1:15j, -b + 1:b + 1:15j, -c + 1:c + 1:15j]
mask = xs**2/(a**2) + ys**2/(b**2) + zs**2/(c**2) <= 1
xs[~mask] = 0
ys[~mask] = 0
zs[~mask] = 0
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs)
fig.show()
Your first equations hints axis aligned ellipsoid centered at (0,0,0) for such is the easiest way I know of to use scaling to/from sphere. so let:
[x ,y ,z ] - ellipsoid (rx,ry,rz)
[x',y',z'] - sphere (r)
So the transforms are:
// sphere -> ellipsoid
x = x' * rx/r
y = y' * ry/r
z = z' * rz/r
// sphere <- ellipsoid
x' = x * r/rx
y' = y * r/ry
z' = z * r/rz
The (rx,ry,rz) are the radiuses of ellipsoid (in your case rx=ry) and r is any nonzero radius of the sphere (for example r=1.0)
So the test for inside ellipsoid boils down to this:
// scale constants
sx = 1/rx
sy = 1/ry
sz = 1/rz
// condition for inside ellipsoid
x*x*sx*sx + y*y*sy*sy + z*z*sz*sz <= 1.0
My goal is to make a density heat map plot of sphere in 2D. The plotting code below the line works when I use rectangular domains. However, I am trying to use the code for a circular domain. The radius of sphere is 1. The code I have so far is:
from pylab import *
import numpy as np
from matplotlib.colors import LightSource
from numpy.polynomial.legendre import leggauss, legval
xi = 0.0
xf = 1.0
numx = 500
yi = 0.0
yf = 1.0
numy = 500
def f(x):
if 0 <= x <= 1:
return 100
if -1 <= x <= 0:
return 0
deg = 1000
xx, w = leggauss(deg)
L = np.polynomial.legendre.legval(xx, np.identity(deg))
integral = (L * (f(x) * w)[None,:]).sum(axis = 1)
c = (np.arange(1, 500) + 0.5) * integral[1:500]
def r(x, y):
return np.sqrt(x ** 2 + y ** 2)
theta = np.arctan2(y, x)
x, y = np.linspace(0, 1, 500000)
def T(x, y):
return (sum(r(x, y) ** l * c[:,None] *
np.polynomial.legendre.legval(xx, identity(deg)) for l in range(1, 500)))
T(x, y) should equal the sum of c the coefficients times the radius as a function of x and y to the l power times the legendre polynomial where the argument is of the legendre polynomial is cos(theta).
In python: integrating a piecewise function, I learned how to use the Legendre polynomials in a summation but that method is slightly different, and for the plotting, I need a function T(x, y).
This is the plotting code.
densityinterpolation = 'bilinear'
densitycolormap = cm.jet
densityshadedflag = False
densitybarflag = True
gridflag = True
plotfilename = 'laplacesphere.eps'
x = arange(xi, xf, (xf - xi) / (numx - 1))
y = arange(yi, yf, (yf - yi) / (numy - 1))
X, Y = meshgrid(x, y)
z = T(X, Y)
if densityshadedflag:
ls = LightSource(azdeg = 120, altdeg = 65)
rgb = ls.shade(z, densitycolormap)
im = imshow(rgb, extent = [xi, xf, yi, yf], cmap = densitycolormap)
else:
im = imshow(z, extent = [xi, xf, yi, yf], cmap = densitycolormap)
im.set_interpolation(densityinterpolation)
if densitybarflag:
colorbar(im)
grid(gridflag)
show()
I made the plot in Mathematica for reference of what my end goal is
If you set the values outside of the disk domain (or whichever domain you want) to float('nan'), those points will be ignored when plotting (leaving them in white color).