Algorithm equalivence from Matlab to Python -
i've plotted 3-d mesh in matlab
below little m-file
:
[x,n] = meshgrid(0:0.1:20, 1:1:100); mu = 0; sigma = sqrt(2)./n; f = normcdf(x,mu,sigma); mesh(x,n,f);
i going acquire same result utilization of python
, corresponding modules, below code snippet:
import numpy np scipy.integrate import quad import matplotlib.pyplot plt sigma = 1 def integrand(x, n): return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2)) tt = np.linspace(0, 20, 2000) nn = np.linspace(1, 100, 100) t = np.zeros([len(tt), len(nn)]) i,t in enumerate(tt): j,n in enumerate(nn): t[i, j], _ = quad(integrand, -np.inf, t, args=(n,)) x, y = np.mgrid[0:20:0.01, 1:101:1] plt.pcolormesh(x, y, t) plt.show()
but output of python
is considerably different matlab
one, , matter of fact unacceptable. afraid of wrong utilization of functions linespace
, enumerate
or mgrid
...
does have idea about?!...
ps. unfortunately, couldn't insert output plots within thread...!
best
..............................
edit: changed linespace
, mgrid
intervals , replaced plot_surface
method... output 3d suitable accuracy , smoothness...
from see equivalent solution be:
import numpy np scipy.stats import norm import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d x, n = np.mgrid[0:20:0.01, 1:100:1] mu = 0 sigma = np.sqrt(2)/n f = norm.cdf(x, mu, sigma) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(x, n, f, rstride=x.shape[0]//20, cstride=x.shape[1]//20, alpha=0.3) plt.show()
unfortunately 3d plotting matplotlib not straight forward matlab.
here plot code:
Comments
Post a Comment