I've written a code for my school project. The u and v matrices are generated already. I've made a 2D contour, but I wish to plot u and v in 3D.
import numpy as np
#import matplotlib.pyplot as plt
#inputs
m = 40
n = 20
Uinf = 2
delX = 0.05
delY = 0.05
nu = 0.8e-2
u= np.zeros((m+1,n))
v= np.zeros((m+1,n))
#intializing boundry conditions
u[:,0] = v[:,0] = v[:,n-1] = v[0,:] = 0
u[:,n-1] = u[0,:] = Uinf
#main program
for i in range(0,m):
for j in range(1,n-1):
a = (nu*delX) / (u[i,j]*delY**2)
b = (v[i,j]*delX) / (2*u[i,j]*delY)
u[i+1,j] = (a-b)*u[i,j+1]+(1-2*a)*u[i,j]+(a+b)*u[i,j-1]
v[i+1,j] = v[i+1,j-1]-(delY/(2*delX))*(u[i+1,j]-u[i,j]+u[i+1,j-1]-u[i,j-1])
I have to mention that I don't wanna generate an arbitrary function; u[i,j] and v[i,j] are present.
Thanks!
Related
I did a logistic regression on my data and now I find the best Theta array to find the class of a new data.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def h_theta(x,theta):
return np.dot(x,np.transpose(theta))
def g_z(x,theta):
return 1/(1+pow(np.e,-h_theta(x,theta)))
def cost_function(x,y,theta):
cost = 0
for i in range(len(y)):
l = np.log(g_z(x[i],theta))
cost += -y[i]*l -(1-y[i])*np.log((1-(g_z(x[i],theta))))
return cost/(2*len(y))
def updata_theta(x,y,theta,alpha):
for i in range(6):
u = 0
for j in range(len(y)):
u += (h_theta(x[j],theta)-y[j])*x[j,i]
theta[0,i] -= alpha*u/(len(y))
data = pd.read_csv("D:\REZA\programming\machine learning-andrew ng\coding\machine-learning-ex2\ex2\ex2data2.csv")
y = np.array(data["1"])
s = np.array(data.drop("1",axis=1))
x1T2 = np.zeros((117,1))
x2T2 = np.zeros((117,1))
x1x2 = np.zeros((117,1))
one = np.ones((117,1))
m = len(y)
for i in range(m):
x1T2[i] = s[i,0]*s[i,0]
x2T2[i] = s[i,1]*s[i,1]
x1x2[i] = s[i,0]*s[i,1]
x = np.append(one,s,axis=1)
f = np.append(x1T2,x2T2,axis=1)
f = np.append(f,x1x2,axis=1)
x = np.append(x,f,axis=1)
x = np.array(x,dtype=np.float)
theta = np.zeros((1,6),dtype=float)
n=0
alpha = 0.003
while(n<100 and cost_function(x,y,theta)>0.01):
updata_theta(x,y,theta,alpha)
n+=1
I can plot my data with plt.scatter
plt.scatter(x[:,1],x[:,2],c=y)
plt.show()
scatter plot output
Now I want to plot decision boundary using this theta array, but I don't know how to do it.
i need to create a higher resolution of my plot with the linspace function but i can't figure out how to implement it into my code. Maybe someone has a better understanding of this and can help me.
import numpy as np
import matplotlib.pyplot as plt
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16])
ct = c/1000
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = np.zeros(n)
k = np.zeros(n)
for j in range(0,n):
A[j] = (ct[j] - ct0)/(cmax - ct0)
for j in range(0, n):
if j < 3:
k[j] = -(np.log(1 - A[j])) / N[j]
else:
k[j] = 1
MWk = np.mean(k)
Amod = np.zeros(n)
for j in range(0,n):
Amod[j] = 1 - np.exp((-N[j]) * MWk)
print(ct)
print(A)
print(k)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.plot(N, A, "g", label = "Aufschlussgrad")
plt.plot(N, Amod, "k", label = "Modelfunktion")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
There is no need to interpolate Amod, since it is a function you defined. On the other hand, it is necessary to perform an interpolation (either on A or on the original data c) in order to add more points to the graph. With only 4 or 5 points, the interpolation will not be very meaningful. In this case I choose to interpolate A.
The code was not taking profit of numpy's arrays, so I pythonized it a little (it looked like C)
import numpy as np
import matplotlib.pyplot as plt
def Amod(x, MWk):
return 1 - np.exp((-x) * MWk)
def k(x, A):
rv = -np.log(1 - A) / x
rv[np.nonzero(A==1)] = 1
return rv
# No real changes here: only a little 'pythonization'
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16])
ct = c/1000
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = (ct - ct0) / (cmax-ct0)
MWk = np.mean(k(N, A))
print(ct)
print(A)
print(k)
# we now interpolate A
# numpy's interpolation is linear... it is not useful for this case
from scipy.interpolate import interp1d
# interp1d returns a function that interpolates the data we provide
# in this case, i choose quadratic interpolation
A_interp_fun = interp1d(N, A, 'quadratic')
# Let's increase the number of points
new_x = np.linspace(N[0], N[-1])
A_xtra = A_interp_fun(new_x)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.scatter(N, A, label = "Aufschlussgrad - data")
plt.plot(new_x, A_xtra, "g", label="Aufschlussgrad - interpolation")
plt.scatter(N, Amod(N, MWk), label="Modelfunktion - data")
plt.plot(new_x, Amod(new_x, MWk), "k", label="Modelfunktion - function")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
I have a vector and the Eigenvectors of the matrix H, id like to find the dot product on V1 with every vector in the array.
I want to multiply the vector PhiZero with every eigenvector that's calculated for H
import numpy as np
import scipy.linalg as la
import math
import networkx as nx
Alpha = []
n=3
p=0.5
G = nx.gnp_random_graph(n,p)
A = nx.to_numpy_matrix(G)
w = np.zeros(shape=(n,n))
w[1,2] = 1
gamma = 1/(n*p)
H = (-w) - (gamma * A)
ive chosen a random position in w,
evals, evecs = la.eig(H)
PhiZero = np.reciprocal(math.sqrt(n)) * np.ones((n,1), dtype=int)
i've tried to calculate it two ways, the first way, I get a 3x3 matrix
Alpha = np.dot(PhiZero.transpose(), evecs)
the other way, I tried it with a for loop:
for y in evecs:
alphaJ = np.dot(PhiZero.transpose(), evecs)
Alpha.append(alphaJ)
i've taken the transpose PhiZero to make the dimensions align with evecs (1x3 & 3x1)
In your second approach, should you not have:
alphaJ = np.dot(PhiZero.transpose(), y)
rather than
alphaJ = np.dot(PhiZero.transpose(), evecs)
?
If I try your first example, it works:
import numpy as np
import scipy.linalg as la
import math
import networkx as nx
Alpha = []
n=3
p=0.5
G = nx.gnp_random_graph(n,p)
A = nx.to_numpy_matrix(G)
w = np.zeros(shape=(n,n))
w[1,2] = 1
gamma = 1/(n*p)
H = (-w) - (gamma * A)
evals, evecs = la.eig(H)
PhiZero = np.reciprocal(math.sqrt(n)) * np.ones((n,1), dtype=int)
Alpha = np.dot(PhiZero.transpose(), evecs)
print(Alpha)
gives
[[ 0.95293215 -0.32163376 0.03179978]]
Are you sure you get a 3x3 if you run this?
I'm trying to generate random sample points on a cartesian plane using polar coordinates. I have a cartesian map with polar sectors, I'd like to put a random sample point within each of the sectors.
Problem Visual Description
I've added a sample point in the first sector. The problem is I don't know how to set the min and max limits for each sector as it's a cartesian plane (using cartesian min and max of the sector corners will give you boxes instead of the entire polar sector).
Code is commented for clarity. Final output posted below.
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 10]
import math
import pylab as pl
from matplotlib import collections as mc
import pprint
from IPython.utils import io
from random import randrange, uniform
#convertes cartesian x,y coordinates to polar r, theta coordinates
def cart2pol(x, y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
return np.array([rho, phi])
#convertes polar r,theta coordinates to cartesian x,y coordinates
def pol2cart(r, theta): #r is distance
x = r * np.cos(theta)
y = r * np.sin(theta)
return np.array([x, y])
#cooks delicious pie
pi = np.pi
#no idea what this does
theta = np.linspace(0,2*pi,100)
#x theta
def x_size(r):
return r*np.cos(theta)
#y theta
def y_size(r):
return r*np.sin(theta)
#calculates distribution of sectors on a circle in radians
#eg. sub_liner(3) = array([0. , 2.0943951, 4.1887902])
def sub_liner(k):
sub_lines = []
for c,b in enumerate(range(0,k)):
sub_lines = np.append(sub_lines,((12*pi/6)/k)*c)
return sub_lines
#calculates all distribution sectors for every ring and puts them in a list
def mlp(i):
master_lines = []
k = 3
for a in range(0,i):
master_lines.append(sub_liner(k))
k += 3
return master_lines
#calculates all four corners of each sector for a ring
#(ring,ring points,number of rings)
def cg(r,rp,n):
return [[[pol2cart(r-1,mlp(n)[r-1][i])[0],pol2cart(r-1,mlp(n)[r-1][i])[1]]\
,[pol2cart(r,mlp(n)[r-1][i])[0],pol2cart(r,mlp(n)[r-1][i])[1]]] for i in range(0,rp)]
#generates all corners for the ring sectors
def rg(n):
cgl = []
k = 3
for r in range(1,11):
cgl.append(cg(r,k,n))
k += 3
output = cgl[0]
for q in range(1,10):
output = np.concatenate((output,cgl[q]))
return output
#print(cg(1,3,10)[0][0][0])
#print(cg(1,3,10))
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
#define ring sectors
ring_sectors = rg(10)
#define node points
nx = 0.5
ny = 0.5
#define ring distance
ymin = [0]
ymax = [1]
#generate rings
ring_r = np.sqrt(1.0)
master_array = np.array([[x_size(i),y_size(i)] for i in range(0,11)])
#plot rings
fig, ax = plt.subplots(1)
[ax.plot(master_array[i][0],master_array[i][1]) for i in range(0,11)]
ax.set_aspect(1)
size = 10
plt.xlim(-size,size)
plt.ylim(-size,size)
#generate nodes
ax.plot(nx, ny, 'o', color='black');
#ring lines
lc = mc.LineCollection(ring_sectors, color='black', linewidths=2)
ax.add_collection(lc)
plt.grid(linestyle='--')
plt.title('System Generator', fontsize=8)
plt.show()
Sample output can be viewed at.
Edit:
What I've tried:
Based on feedback, I implemented a system which gets random uniform values between the polar coordinates, and it works, but the points aren't neatly distributed within their sectors as they should be, and I'm not sure why. Maybe my math is off or I made a mistake in the generator functions. If anyone has any insight, I'm all ears.
Output with points
def ngx(n):
rmin = 0
rmax = 1
nxl = []
s1 = 0
s2 = 1
k = 0
for i in range(0,n):
for a in range(0,rmax*3):
nxl.append(pol2cart(np.random.uniform(rmin,rmax),\
np.random.uniform(sub_liner(rmax*3)[(s1+k)%(rmax*3)],sub_liner(rmax*3)[(s2+k)%(rmax*3)]))[0])
k += 1
rmin += 1
rmax += 1
return nxl
def ngy(n):
rmin = 0
rmax = 1
nyl = []
s1 = 0
s2 = 1
k = 0
for i in range(0,n):
for a in range(0,rmax*3):
nyl.append(pol2cart(np.random.uniform(rmin,rmax),\
np.random.uniform(sub_liner(rmax*3)[(s1+k)%(rmax*3)],sub_liner(rmax*3)[(s2+k)%(rmax*3)]))[1])
k += 1
rmin += 1
rmax += 1
return nyl
#define node points
nx = ngx(10)
ny = ngy(10)
So I've been starting to use python recently, and i am working on a project of calculating wind exposure. I've managed my code in matlab and it runs very fast(can be done in 3 minutes), but after I translated my code into python, i am getting the same result but it takes 3hours to finish it's job. I really need a hand on checking what's causing such a huge difference...
So here's my python code. I can give out my matlab code if anyone need it.
from netCDF4 import Dataset, num2date
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
#import pylab as py
#input data
dem = Dataset('comparearea_fill.nc','r')
lon = np.array(dem.variables['lon'])
lat = np.array(dem.variables['lat'])
DEM = np.array(dem.variables['elevation'])
carea = Dataset('carea.nc','r')
u = np.array(carea.variables['u10'])
v = np.array(carea.variables['v10'])
mu = np.mean(u, axis=0)
mv = np.mean(v, axis=0)
x = np.linspace(1,21,21)
y = np.linspace(1,11,11)
newu = interpolate.interp2d(x, y, mu, kind='cubic')
newv = interpolate.interp2d(x, y, mv, kind='cubic')
spu = newu(lon,lat)
spv = newv(lon,lat)
A = np.zeros((4951,9451))
B = np.zeros((4951,9451))
for i in range(100,4850):
for j in range(100,9350):
for n in range(20):
A[i,j] = (DEM[i,j]-np.max(DEM[np.floor(n*spv[i,j]).astype(int),j-np.floor(n*spu[i,j]).astype(int)]))/DEM[i,j]
if A[i,j] < 0:
A[i,j] = 0
B[i,j] = (DEM[i,j]-np.max(DEM[i-np.ceil(n*spv[i,j]).astype(int),j-np.ceil(n*spu[i,j]).astype(int)]))/DEM[i,j]
if B[i,j] < 0:
B[i,j] = 0
C = A+B
plt.contourf(lon,lat,C); plt.colorbar()
here the mu and mv are the monthly average of the u and v wind, while the spu and spv are the spline interpulated u and v wind to fit the resolution of my dem data set.