Estoy trabajando con un código de MATLAB y resulta que me hallé con una serie de métodos( y funciones) que he tratado de pasarlos a la sintaxis de Python. Cómo debo interpretar en Python esta sintaxis function [y1,...,yN] = myfun(x1,...,xM)
Función hecha en MATLAB:
function [stmat,utim,costmat,dhdu,Jiter,H]... =optimal_cntrl_calc_var_nsec_odesol(tvec,st0,b) % This function computes the optimal control and % the corresponding states and the co-states. % Method of Steepest Descent is used for this. % n=no. of sections nt=length(tvec) ; n=length(st0) ; n=n/2 ; utim=0.1*ones(nt,n) ; % assume control % forward integration for states stmat=fwdstinteg(tvec,utim,st0,b) ; rho=stmat(:,1:n); rhof=rho(end,:); % time increment for simulation dt=tvec(2)-tvec(1) ; % cost function for the simulation J=sum(sum(rho.^2+utim.^2,2)*dt)+sum(rhof.^2); % iter=no of control update iterations % Jiter=vector of iteration cost values iter=1 ; Jiter(iter)=J ; % convergence: % test=0 => not occured test=1 => occured. test=0 ; % tolerences tol1=10^-4 ; tol2=10^-6 ; % iterations for control and cost till % convergence occurs while test==0 % check for convergence iter=iter+1 ; stf=stmat(end,:)' ; rhof=stf(1:n) ; costf=[2*rhof;zeros(n,1)] ; % backward integration for co-states costmat=bkcostinteg(tvec,stmat,costf,b) ; % calculation dhdu at all time dhdu=2*utim+costmat(:,n+1:2*n) ; % "cntrl_update" updates the control and state % for the next iteration [stmat,utim,J,test]=... cntrl_update(tvec,stmat,utim,dhdu,b,tol1,tol2) ; % Jiter(iter)=value of the cost function at the % current iteration. Jiter(iter)=J ; end rho=stmat(:,1:n) ; vf=stmat(:,n+1:2*n) ; p=costmat(:,1:n) ; pvf=costmat(:,n+1:2*n) ; % Computation for hamiltonian H=zeros(nt,1) ; for i=1:1:n H=H+rho(:,i).^2+utim(:,i).^2+p(:,i)... .*(-rho(:,i).*(1-rho(:,i))*b(i).*vf(:,i)) ; H=H+pvf(:,i).*utim(:,i) ; if i>=2 H=H+p(:,i).*(rho(:,i-1).*(1-rho(:,i-1))... *b(i).*vf(:,i-1)) ; end end end
Lo que yo hice en Python:
#Creacion de la función optimal_cntrl_calc_var_nsec_odesol def optimal_cntrl_calc_var_nsec_odesol(tvec,st0,b): # Esta función calcula el control óptimo y # los estados correspondientes y los co-estados. # Método de descenso más pronunciado se utiliza para esto. # n = N°. de secciones nt=len(tvec) n=len(st0) n=n/2 utim=0.1*np.ones((nt,n))#% asume la integración de #control de avance para los estados stmat=fwdstinteg(tvec,utim,st0,b) rho=stmat[:,0:n+1] rhof=rho[end,:] #tiempo de incremento para la simulación dt=tvec[1]-tvec[0] #funcion de costo para la simulación #axis=1 => horizontal #axis=0 => vertical J=np.sum(np.sum(rho**2 + utim**2,axis=1)*dt)+np.sum(rhof**2) #itera= número de iteraciones de actualización de control #Jiter= vector de valores de costo de iteración itera=1 Jiter[itera]=J #convergencia #test=0 => no ocurre test=1 => ocurre test=0 #Tolerancias tol=10**(-4) tol2=10**(-6) #iteraciones para control y costo hasta #que ocurre la convergencia while (test==0):#para chequear la convergencia itera=itera+1 stf=(stmat[-1:,]).transpose() rhof=stf[0:n+1] v=[[[2*rhof]],np.zeros(n)] costf=np.array(v) #integración hacia atrás para co-estados costmat=bkcostinteg(tvec,stmat,costf,b) #calculo dhdu de todo el tiempo dhdu=2*utim+costmat[:,n+2:2*(n+1)] #"cntrl_update" actualiza el control y el estado #para la próxima iteración metod2=cntrl_update(tvec,stmat,utim,dhdu,b,tol,tol2) Jiter[iter]=J rho=stmat[:,0:n+1] vf=stmat[:,n+2:2*(n+1)] p=costmat[:,0:n+1] pvf=costmat[:,n+2:2*(n+1)] #cálculo del hamiltoniano H=np.zeros(int(nt)) for i in range(1,n+1): H=H+rho[:,i]**2 + utim[:,i]**2 +p[:,i]*(-rho[:,i]*(1-rho[:,i])*b[i]*vf[:,i]) H=H+pvf[:,i]*utim[:,i] if i>=2: H=H+p[:,i]*(rho[:,i-1]*(1-rho[:,i-1])*b[i]*vf[:,i-1])