matlab code for python script?
Show older comments
i am trying to write a matlab code for python script as:
class structure:
def __init__(self, shape, eps, mu):
self.shape = shape
self.eps = eps
self.mu = mu
self.epsr = np.ones(shape, dtype='float64')
self.mur = np.ones(shape, dtype='float64')
class Sphere(structure):
def __init__(self, shape, center, R, eps, mu, smoothing = False):
super(Sphere, self).__init__(shape, eps, mu)
self.center = center
self.R = R
self.smoothing = smoothing
self.get_epsr()
def get_epsr(self):
x = np.arange(self.shape[0])
y = np.arange(self.shape[1])
z = np.arange(self.shape[2])
X,Y,Z = np.meshgrid(x,y,z)
self.r = np.sqrt((self.center[1]-X)**2 + (self.center[0]-Y)**2 + (self.center[2]-Z)**2)
self.region = np.where(self.r >= self.R, 0, 1)
print("self.region value: ",self.region)
print("self.region shape: ",self.region.shape)
self.epsr += self.region * (self.eps - 1)
if self.smoothing == True:
smoothing_region = 1 - (np.where((0 < (self.r - self.R)) & ((self.r - self.R) < 1), 1/3*(self.r+np.sqrt(self.r**2-1)-2*self.R)**2, 1))
self.epsr += smoothing_region * (self.eps - 1)
del self.r
the call for these structures in python is as:
um = 1e-6
dx = 4.8 * um
lamb = 74.9*um
eps = np.zeros((250,250))
str1 = Sphere(shape = (250,250,250), center = (125,125,125), R = lamb*3/dx, eps=10, mu=1)
THE MATLAB SCRIPT WHICH I TRIED IS AS:
shape = [250,250,250]
N1=shape(1);
N2=shape(2);
N3=shape(3);
um = 1e-6
c = 3e8 % m/s
dx = 4.8 * um
dy = 4.8 * um
dz = 4.8 * um
dt = 1/4 * dx / c
lamb = 74.9*um
R = lamb*3/dx;
eps=10;
mu=1;
x = linspace(0,N1-1,N1);
y = linspace(0,N2-1,N2);
z = linspace(0,N3-1,N3);
[X,Y,Z] = meshgrid(x,y,z);
X=(X(:))';
Y=(Y(:))';
Z=(Z(:))';
center = [125, 125,125];
r = sqrt((center(2)-X).^2 + (center(1)-Y).^2 + (center(3)-Z).^2);
epsr = ones(shape);
mur = ones(shape);
for k=1:1:length(r)
for j = 1:1:length(r)
for k=1:1:length(r)
if r(:,:,k) >= R
epsr(:,:,k) =
Q: i am struck with implementation of region in matlab, can someone help me with this
Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!