Converting MATLAB code into python code
古いコメントを表示
Hello
I have written a MATLAB code, which is given below. I want to convert this code into pythod because I am not able to run this code on my laptop, which has limited Ram and very few Cores. So, i am going to ask high performance computation authority of my college to run this code. The problem is that they don't allow software which require GUI for running the codes. So, I want to convert this code into python to run it on clusters.
Please MATLAB community help me! MATLAB will always be my favourite :)
Thanks
tic
clc; close all; clear all;
syms p1(t) p2(t) p3(t) alpha_by_L beta_by_L gamma_by_L epsilon_by_L
Dp1 = diff(p1); D2p1 = diff(p1,2); Dp2 = diff(p2); D2p2 = diff(p2,2); Dp3 = diff(p3); D2p3 = diff(p3,2);
gamma_by_L_Matrix = [0.1:0.1:5];
beta_by_L_Matrix = [0.2:0.2:10];
alpha_by_L_Matrix = [0.1:0.1:5];
path = pwd ; % mention your path
myfolder = 'myfolder' ; % new folder name
folder = mkdir([path,filesep,myfolder]) ;
path = [path,filesep,myfolder] ;
parfor (iii = 1:1:length(gamma_by_L_Matrix))
gamma_by_L = gamma_by_L_Matrix(iii)
figure(1)
for jjj = 1:1:length(beta_by_L_Matrix)
beta_by_L = beta_by_L_Matrix(jjj)
for kkk = 1:1:length(alpha_by_L_Matrix)
alpha_by_L = alpha_by_L_Matrix(kkk)
% matrix terms
AA = 1/2 + (alpha_by_L)*(sin(pi*beta_by_L*t))^2;
BB = (alpha_by_L)*sin(pi*beta_by_L*t)*sin(2*pi*beta_by_L*t);
CC = (alpha_by_L)*sin(pi*beta_by_L*t)*sin(3*pi*beta_by_L*t);
DD = 1/2 + (alpha_by_L)*(sin(2*pi*beta_by_L*t))^2;
EE = (alpha_by_L)*sin(3*pi*beta_by_L*t)*sin(2*pi*beta_by_L*t);
FF = 1/2 + (alpha_by_L)*(sin(3*pi*beta_by_L*t))^2;
% matrix terms
GG = (pi^2)/2 + (gamma_by_L)*(sin(pi*beta_by_L*t))^2;
HH = (gamma_by_L)*sin(pi*beta_by_L*t)*sin(2*pi*beta_by_L*t);
II = (gamma_by_L)*sin(3*pi*beta_by_L*t)*sin(pi*beta_by_L*t);
JJ = (4*pi^2)/2 + (gamma_by_L)*(sin(2*pi*beta_by_L*t))^2;
KK = (gamma_by_L)*sin(2*pi*beta_by_L*t)*sin(3*pi*beta_by_L*t);
LL = (9*pi^2)/2 + (gamma_by_L)*(sin(3*pi*beta_by_L*t))^2;
% RHS
MM = (epsilon_by_L)*sin(pi*beta_by_L*t);
NN = (epsilon_by_L)*sin(2*pi*beta_by_L*t);
OO = (epsilon_by_L)*sin(3*pi*beta_by_L*t);
Eq1 = AA*diff(p1,t,2) + BB*diff(p2,t,2) + CC*diff(p3,t,2) + GG*p1 + HH*p2 + II*p3 == 0; % Equation 1
Eq2 = BB*diff(p1,t,2) + DD*diff(p2,t,2) + EE*diff(p3,t,2) + HH*p1 + JJ*p2 + KK*p3 == 0; % Equation 2
Eq3 = CC*diff(p1,t,2) + EE*diff(p2,t,2) + FF*diff(p3,t,2) + II*p1 + KK*p2 + LL*p3 == 0; % Equation 3
[V,S] = odeToVectorField(Eq1, Eq2, Eq3);
ftotal = matlabFunction(V, 'Vars',{'t','Y'}); % Using readymade MATLAB function to solve using ODE 45
interval = [0 2/beta_by_L]; % Time Interval to run the program
for i = 1:6
if i == 1
y0 = [1 ;0 ;0 ;0 ;0 ;0 ] ; % First IC
elseif i == 2
y0 = [0 ;1 ;0 ;0 ;0 ;0 ] ; % Second IC
elseif i == 3
y0 = [0 ;0 ;1 ;0 ;0 ;0 ] ;
elseif i == 4
y0 = [0 ;0 ;0 ;1 ;0 ;0 ] ;
elseif i == 5
y0 = [0 ;0 ;0 ;0 ;1 ;0 ] ;
elseif i == 6
y0 = [0 ;0 ;0 ;0 ;0 ;1 ] ;
end
xSol = ode45( @(t,Y) ftotal(t,Y),interval,y0); % Using ODE 45 to solve state space form of ODE
tValues = linspace(interval(1),interval(2),1000); % dividing time interval
x1Values = deval(xSol,tValues,1); % number 1 denotes first solution likewise you can mention 2 ,3 & 4 for the next three solutions
x2Values = deval(xSol,tValues,2); % number 2 denotes second solution likewise you can mention 2 ,3 & 4 for the next three solutions
x3Values = deval(xSol,tValues,3);
x4Values = deval(xSol,tValues,4);
x5Values = deval(xSol,tValues,5);
x6Values = deval(xSol,tValues,6);
if i == 1
col_11 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_12 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
elseif i == 2
col_21 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_22 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
elseif i == 3
col_31 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_32 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
elseif i == 4
col_41 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_42 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
elseif i == 5
col_51 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_52 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
elseif i == 6
col_61 = [x1Values(1);x2Values(1);x3Values(1);x4Values(1);x5Values(1);x6Values(1)];
col_62 = [x1Values(end);x2Values(end);x3Values(end);x4Values(end);x5Values(end);x6Values(end)];
end
end
A1 = [col_11 col_21 col_31 col_41 col_51 col_61];
A2 = [col_12 col_22 col_32 col_42 col_52 col_62] ;
A = (inv(A1))*A2 ;
eigval = eig(A);
abseigval = abs(eigval);
iteval = gamma_by_L
if (abseigval(1)<=1.00 && abseigval(2)<=1.00 && abseigval(3)<=1.00 && abseigval(4)<=1.00 && abseigval(5)<=1.00 && abseigval(6)<=1.00)
plot(beta_by_L,alpha_by_L,'k.', 'MarkerSize', 10)
xlabel('beta_by_L')
ylabel('alpha_by_L')
title("gamma_by_L " + gamma_by_L + " ")
hold on
else
plot(beta_by_L,alpha_by_L,'r.', 'MarkerSize', 10)
xlabel('beta_by_L')
ylabel('alpha_by_L')
title("gamma_by_L " + gamma_by_L + " ")
hold on
end
end
end
hold off
xlim([0 10])
ylim([0 5])
iii
end
toc
2 件のコメント
Mike Croucher
2021 年 11 月 19 日
You don't need a GUI to run MATLAB code. You can run it in batch mode just like you can Python. A huge part of my job is to work with researchers who want to run MATLAB effectively on High Performance Computing clusters.
Your first issue with this code is 'What results do you want to save?' I'm guessing the variables beta_by_L,alpha_by_L since you are plotting them?
aakash dewangan
2021 年 11 月 19 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Code Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

