0 svar
101 visningar
IndentationError behöver inte mer hjälp
IndentationError 7
Postad: 21 apr 2021 13:18

Konvertering av Matlab kod till Python kod

Hej, jag skulle vilja få lite hjälp med att konvertera Matlab-kod till Python-kod. Kan någon hjälpa mig?

Jag kommer att dela vad jag har gjort hittills.

 

############## Matlab kod ###################

D0 = 7.82;

a0 = 0.0518;
a1 = 2.0026;
a2 = -4.491;

noR = 15;
rMax = D0/2;
r = linspace( 0,rMax, noR )';

fact = 0.8;
radd = (1-fact) * r(end-1) + fact * r(end);
r = [r(1:end-1); radd; r(end)];

dr = r(2) - r(1);

Rs = [];
Phi = [];

for k=1:noR
    L = 2 * pi * r(k);
    noPhi = round( L / dr );
    phi = linspace( 0, 2 * pi * (noPhi - 1)/ noPhi, noPhi )';
    Rs = [Rs; r(k) * ones( noPhi, 1) ];
    Phi = [Phi; phi];
end;

[x, y] = pol2cart( Phi, Rs );

t = x.^2 + y.^2;
z = D0 * sqrt( 1 - 4 * t / D0^2 ) .* (a0 + (a1 * t) / D0^2 + (a2 * t.^2) / D0^4);

L = 2 * pi * rMax;
noPhi = round( L / dr );

phi = linspace( 0, 2 * pi * (noPhi - 1)/ noPhi, noPhi )';

[xm, ym] = pol2cart(phi, rMax * ones( noPhi, 1));
zm = zeros( noPhi, 1);


x = [x; xm];
y = [y; ym];
z = [z; zm];

############# Python kod #############

D0 = 7.82
a0 = 0.0518
a1 = 2.0026
a2 = -4.491

noR = 15

rMax = D0/2

r = list(np.linspace(0, rMax, num=noR))

fact = 0.8
radd = (1-fact) * r[-2] + fact * r[-1]
r = [r[-2], radd, r[-1]] # FEL?

dr = r[1] - r[0]

Rs = []
Phi = []

for k in np.arange(1, noR):
    L = 2 * math.pi # * r[k] list index out of range om man multiplicerar med r(k)
    noPhi = round(L/dr)
    phi = np.linspace(0, 2 * math.pi * (noPhi - 1)/noPhi, noPhi)
    #Rs = [Rs, r(k) * np.ones((noPhi, 1), dtype=int)]
    Rs = [Rs, np.ones((noPhi, 1), dtype=int)]
    Phi = [Phi, phi]

#[x, y] = pol2cart( Phi, Rs );

t = x**2 + y**2
z = D0 * math.sqrt( 1 - 4 * t / D0**2 ) * (a0 + (a1 * t) / D0**2 + (a2 * t**2) / D0**4)

L = 2 * math.pi * rMax
noPhi = round(L/dr)
phi = np.linspace(0, 2 * math.pi * (noPhi - 1)/noPhi, noPhi)

#[xm, ym] = pol2cart(phi, rMax * ones( noPhi, 1))
zm = np.zeros((noPhi, 1), dtype=int)

#x = (x, xm)
#y = (y, ym)
#z = (z, zm)

Svara
Close