vendredi 29 mai 2015

Algorithm equalivence from Matlab to Python

I've plotted a 3-d mesh in Matlab by 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 am going to acquire the same result by utilization of Python and its corresponding modules, by below code snippet:

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as 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)])

for i,t in enumerate(tt):
    for 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 the output of the Python is is considerably different with the Matlab one, and as a matter of fact is unacceptable. I am afraid of wrong utilization of the functions just like linespace, enumerate or mgrid...

Does somebody have any idea about?!...

PS. Unfortunately, I couldn't insert the output plots within this thread...!

Best

..............................

Edit: I changed the linespace and mgrid intervals and replaced plot_surface method... The output is 3d now with the suitable accuracy and smoothness...

Aucun commentaire:

Enregistrer un commentaire