- it's not easy to be certain where subfunctions begin and end
- the anonymous function objective is defined (and is an input to a subfunction), but is never used
- the variable Best_pos3 appears as a function output, but I don't see that it is calculated anywhere
Convert MATLAB code to PYTHON
69 views (last 30 days)
Show older comments
Hello guys, i have a matlab code, i am a beginner in python so I found difficulties to convert it to a python code, could you please help me? Thank you :)
PS: The complete code is in the 1st comment
Code:
clc;close;
x=[1 10 2];
y=[2 5 5];
r=[1 2 0.3]
lb = [0,0,0]; % declaration de la borne inférieure
%
ub = [2*pi,2*pi,2*pi]; % declaration de la borne supérieure
dim=3; % declaration de la dimmension du vecteur x
N=100; % declaration de nombre population
Max_iteration=500; % decalaration de nombre d'iteration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Cercle function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Objective function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1=@(p)r(1).*cos(p(1))+x(1);
y1=@(p)r(1).*sin(p(1))+y(1);
x2=@(p)r(2).*cos(p(2))+x(2);
y2=@(p)r(2).*sin(p(2))+y(2);
x3=@(p)r(3).*cos(p(3))+x(3);
y3=@(p)r(3).*sin(p(3))+y(3);
objective = @(p)sqrt(1/3*((x1(p)-x2(p)).^2+(x1(p)-x3(p)).^2+(x2(p)-x3(p)).^2))+sqrt(1/3*((y1(p)-y2(p)).^2+(y1(p)-y3(p)).^2+(y2(p)-y3(p)).^2));%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%MFO algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[Best_flame_score,Best_pos3,Convergence_curve]=WSO(N,Max_iteration,lb,ub,dim,objective);
theta1=Best_pos3(1);
theta2=Best_pos3(2);
theta3=Best_pos3(3);
x1=r(1).*cos(theta1)+x(1);
y1=r(1).*sin(theta1)+y(1);
x2=r(2).*cos(theta2)+x(2);
y2=r(2).*sin(theta2)+y(2);
x3=r(3).*cos(theta3)+x(3);
y3=r(3).*sin(theta3)+y(3);
X=[x1 x2 x3]
Y=[y1 y2 y3]
Best_flame_score
figure;
plot(x,y,'*',X,Y,'*');
h=circle(x(1),y(1),r(1));
h1=circle(x(2),y(2),r(2));
h2=circle(x(3),y(3),r(3));
2 Comments
the cyclist
on 31 Jan 2023
This MATLAB code will be difficult to convert to python, because this code is itself confusing and ill-formed. Specifically,
So, you might want to think about those things first.
That being said, I have found ChatGPT to be a surprisingly good tool for converting code from one language to another. (I have converted some moderately tricky statistical code from MATLAB to python.) You might want to create an OpenAI account, and give that a try (if you don't get a useful solution here).
Answers (1)
Priyank Pandey
on 7 Mar 2023
Hi Emna,
To convert MATLAB code to Python follow the following steps:
Step 1: Develop your own MATLAB Code as MATLAB Function
Step 2: Enter the command on command window
command:
deploytool
Step 3:
Select python package
Step 4:
Export your package
Follow the bellow link to have a better understanding on how to convert MATLAB Code to Python:
https://in.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html
0 Comments
See Also
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!