Effacer les filtres
Effacer les filtres

matlab load python scripts have different result when directly run in python

1 vue (au cours des 30 derniers jours)
KOU DU
KOU DU le 14 Jan 2021
Commenté : Ishan le 28 Oct 2022
I load a python user-defined scripts in Matlab but I got different results with Python.
In python, I have a function to calculate the numerical intergral of an intergral function.
In matlab load this python scripts by:
system('python Userdefined.py');
A = py.Userdefined.Intergral(0.2);
But here 'A' is different with python result when I directly run 'Intergral(0.2)' in python.
I have confirmed that, Matlab result is wrong but I don't know why!
I just load it in Matlab.
  5 commentaires
KOU DU
KOU DU le 15 Jan 2021
I use system to load the python user-defined scripts.
Here I put an example of python script which is named 'Intergral.py':
#! /usr/bin/env python
import math
import sympy
from sympy import *
import numpy as np
from numpy import *
import scipy
from scipy import special
from scipy import *
from scipy.integrate import quad
from scipy import integrate
def da_theta_phi_np(p,theta,phi):
twop=2*p ; one_over_p=1./p ; one_over_twop=1./(2*p)
ct=np.cos(theta) ; st=np.sin(theta)
cp=np.cos(phi) ; sp=np.sin(phi)
r0=ct**twop+(st*cp)**twop+(st*sp)**twop
r=r0**(-one_over_twop)
tpm=twop-1.
drdtheta=(st*ct**tpm-ct*st**tpm*(cp**twop+sp**twop))\
*r0**(-one_over_twop-1)
drdphi=(st**twop*(cp*sp**tpm-sp*cp**tpm))\
*r0**(-one_over_twop-1)
st=np.sin(theta)
return r*np.sqrt((r*st)**2+(drdtheta*st)**2+drdphi**2)
def da_np(p,phi):
aux = quad(lambda theta: da_theta_phi_np(p,theta,phi), 0., acos(1./np.sqrt(2.+np.tan(phi)**2)))
res=aux[0]
return res
def surfacearea_np(p):
aux = quad(lambda phi: da_np(p,phi), 0., math.pi/4.)
res=aux[0]
return 48.*res
When I command in Matlab:
py.Intergral.surfacearea_np(0.2)
ans =
1.731011361308392
But I got 5.73153081591379 when I run in python. All results when I give input p<0.5 are different.
Ishan
Ishan le 28 Oct 2022
A call to the surfacearea_np with 0.2 as input gave an output of 1.7310113613083917 which is what MATLAB also gives. Essentially MATLAB just creates a call to the given function in the file you named 'Integral.py'. Do make sure you are calling the same function in your python script. Hope this helps!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by