I need to convert this Matlab code to Python code.
You can see my attempt below . but when i run the python code get error message ShapeError: Matrix size mismatch: (3, 6) * (1296, 1296).
Here is the Matlab code.
clc; clear all;
A11 = 83.5165
B11 = 0.0000
D11 = 0.2784
H =24.3590
I0 = 792.0000
I1 = 0.0000
I2 = 2.6400
fac=1e-3 % Initial value
kb=0.;err=+1;ico=0;isign=1;
while err>0
ico=ico+1; kb=kb+fac; od=kb^2;
c1=vpa((D11*I0 - B11*I1)*od^2/(B11^2 - A11*D11),10);
c2=vpa((B11*H + (D11*I1 - B11*I2)*od^2)/(B11^2 - A11*D11),10);
c3=vpa(B11*H/(B11^2 - A11*D11),10);
c4=vpa(-(B11*I0 - A11*I1)*od^2/(B11^2 - A11*D11),10) ;
c5=vpa(-(A11*H + (B11*I1 - A11*I2)*od^2)/(B11^2 - A11*D11),10);
c6=vpa(-A11*H/(B11^2 - A11*D11),10);
c7=vpa(-H/(H-N0),10);
c8=vpa(-I0*od^2/(H-N0),10);
T = [0 1 0 0 0 0 ;...
eval(c1) 0 eval(c2) 0 0 eval(c3) ;...
0 0 0 1 0 0;...
eval(c4) 0 eval(c5) 0 0 eval(c6) ;...
0 0 0 0 0 1 ;...
0 0 0 eval(c7) eval(c8) 0];
[E,lambda]=eig(T); lambda=diag(lambda);
gdet1= E;
gdet2= E;
tt1 =eval(A11)*gdet1(2,:)+eval(B11)*gdet1(4,:);
tt2 =gdet1(5,:);
tt3 =eval(B11)*gdet1(2,:)+eval(D11)*gdet1(4,:);
tt4 =eval(A11)*gdet2(2,:)+eval(B11)*gdet2(4,:);
tt5 =gdet2(5,:);
tt6 =eval(B11)*gdet2(2,:)+eval(D11)*gdet2(4,:);
tem1=[tt1;tt2;tt3];
tem2=[tt4;tt5;tt6];
lam1= lambda*1/2;
lam2= lambda*(-1/2);
e_lam1 = diag(exp(lam1),0); %e_lam1(1,2) = a/2;
e_lam2 = diag(exp(lam2),0); %e_lam2(1,2) = -a/2;
% print(lam1,lam2);
bc1 =tem1*e_lam1;
bc2 =tem2*e_lam2;
bc =[bc1;bc2];
for ii=1:6
if real(lam1(ii))>100 % large positive
bc(:,ii)=exp(imag(lam1(ii))*1i)*[tt1(ii); tt2(ii); tt3(ii);0;0;0]';
elseif real(lam1(ii))<-100 % large negative
bc(:,ii)=exp(imag(lam1(ii))*1i)*[0;0;0;tt4(ii); tt5(ii); tt6(ii)]';
end
end
err=det(bc)/det(E);
%% To avoid looping stops at once with just 1 loop
if ico==1
if err<0
isign=-1;
else
isign=1;
end
end
err=err*isign;
%% To decide the tolerance - if the error is quite large when err<0, then err =-err to loop again
if err<0
kb=kb-fac;
fac=fac/10;
if fac>1.0e-5
err=-err;
end
end
fprintf('er= %e | fac= %e| od: %4.4f n',err, fac, od);
Here is my attempt, the Python code
import sympy as sp
A11 = 83.5165
B11 = 0.0000
D11 = 0.2784
H =24.3590
I0 = 792.0000
I1 = 0.0000
I2 = 2.6400
fac = 1e-3 # Initial value
kb = 0
err = 1
ico = 0
isign = 1
while err > 0:
ico += 1
kb += fac
od = kb**2
c1 = sp.N((D11 * I0 - B11 * I1) * od**2 / (B11**2 - A11 * D11), 10)
c2 = sp.N((B11 * H + (D11 * I1 - B11 * I2) * od**2) / (B11**2 - A11 * D11), 10)
c3 = sp.N(B11 * H / (B11**2 - A11 * D11), 10)
c4 = sp.N(-(B11 * I0 - A11 * I1) * od**2 / (B11**2 - A11 * D11), 10)
c5 = sp.N(-(A11 * H + (B11 * I1 - A11 * I2) * od**2) / (B11**2 - A11 * D11), 10)
c6 = sp.N(-A11 * H / (B11**2 - A11 * D11), 10)
c7 = sp.N(-H / (H), 10)
c8 = sp.N(-I0 * od**2 / (H), 10)
T = sp.Matrix([
[0, 1, 0, 0, 0, 0],
[c1, 0, c2, 0, 0, c3],
[0, 0, 0, 1, 0, 0],
[c4, 0, c5, 0, 0, c6],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, c7, c8, 0]
])
E, lambda_ = T.diagonalize()
lambda_ = sp.diag(*lambda_)
gdet1 = E
gdet2 = E
tt1 = A11 * gdet1[1, :] + B11 * gdet1[3, :]
tt2 = gdet1[4, :]
tt3 = B11 * gdet1[1, :] + D11 * gdet1[3, :]
tt4 = A11 * gdet2[1, :] + B11 * gdet2[3, :]
tt5 = gdet2[4, :]
tt6 = B11 * gdet2[1, :] + D11 * gdet2[3, :]
tem1 = sp.Matrix([tt1, tt2, tt3])
tem2 = sp.Matrix([tt4, tt5, tt6])
lam1 = lambda_ / 2
lam2 = -lambda_ / 2
e_lam1 = sp.diag(*[sp.exp(l) for l in lam1])
e_lam2 = sp.diag(*[sp.exp(l) for l in lam2])
bc1 = tem1 * e_lam1
bc2 = tem2 * e_lam2
bc = sp.Matrix.vstack(bc1, bc2)
for ii in range(6):
if sp.re(lam1[ii]) > 100:
bc[:, ii] = sp.exp(sp.im(lam1[ii]) * sp.I) * sp.Matrix([tt1[ii], tt2[ii], tt3[ii], 0, 0, 0])
elif sp.re(lam1[ii]) < -100: # in matlab elseif real(lam1(ii))<-100
bc[:, ii] = sp.exp(sp.im(lam1[ii]) * sp.I) * sp.Matrix([0, 0, 0, tt4[ii], tt5[ii], tt6[ii]])
err = sp.det(bc) / sp.det(E)
if ico == 1:
if err < 0:
isign = -1
else:
isign = 1
err *= isign
if err < 0:
kb -= fac
fac /= 10
if fac > 1.0e-5:
err = -err
print(f'er= {err:e} | fac= {fac:e} | od: {od:.4f} ')
The Python code isn’t functioning properly. Can someone help me correct it?
Thanks in advance.
New contributor
Youcef Tlidji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.