Problem interfacing matlab with python
Show older comments
Hi everybody, I'm calling some matlab functions into my python scripts and I'm having some problems, also because I'm quite new in MATLAB.
I generate a python package with matlab and I want to use it, instead of the matlab engine (is it a good strategy or should I prefer the engine?) By the way my question is: during the execution of my python script this exception is thrown "Operator '-' is not supported for operands of type 'cell'.", but if I run the entire script this not happens and I think that it's due to some type conversion, but I'm not sure how to correctly pass the arguments to the function. According to the error message I think that the exception is thrown when it performs the "S0=N-I0-Q0;" operation in the script error_SIR.
My python code is
import NetworkModelNationalIdentification as NMNI
mat_code = NMNI.initialize()
parameters,y,If = mat_code.id_and_sim(tab_data.tolist(),t_init,data_sep,initial_guess,population,total_active.tolist(),nargout=3)
note that:
- tab_data is a numpy array
- t_init is an integer value
- data_set is an integer value
- population is an integer value
- total_active is a numpy array
- intial_guess is a struct that I've defined as a python dictionary in this way:
initial_guess = {"It0": 100.0, "v": 0.97, "g": 1/14, "tau": 0.066}
and my MATLAB functions are:
function [pars,y,If] = id_and_sim(tab_data,ti,te,initial_guess,N,total_active)
%Take data needed for identification
data=tab_data(:,ti:te);
g=initial_guess.g;
It0=initial_guess.It0;
v=initial_guess.v;
tau=initial_guess.tau;
%Define the constrints for the identification
lim_sup=[v*10, tau*1.5, It0*1.3];
lim_inf=[v*0, tau*0.9, It0*0.7];
times=ti:te;
%identify the model parameters
pars=Identify_Model(data,initial_guess,lim_inf,lim_sup,times,N,total_active);
[v,tau,g,I0]=deal(pars{:});
clc;
%Simulate the model
Q0=tab_data(1,ti);
S0=N-I0-Q0;
[t,y]=ode45(@(t,x) SIR(t,x,v,tau,g,total_active),times,[S0;I0;Q0]);
if length(times)==2
t=[t(1),t(end)];
y=[y(1,:);y(end,:)];
end
%Save the outputs in the form needed for the next steps
If=y(end-1,2);
y=reshape(y(:,3)',[size(y(:,3),1) 1]);
end
%%function called from the above one
function [pars] = Identify_Model(data,initial_guess,lim_inf,lim_sup,times,N,total_active)
scalefac=100;
%Initialize the initial guess for each parameter
v=initial_guess.v;
It0=initial_guess.It0;
tau=initial_guess.tau;
g=0.07;
lim_inf(3)=lim_inf(3)/scalefac;
lim_sup(3)=lim_sup(3)/scalefac;
%Identify the model parameters
options=optimset('MaxIter',100,'TolFun',1e-6,'TolX',1e-6);
parmin=fmincon(@(pars) error_SIR(data,[pars(1),pars(2),g,scalefac*pars(3)],times,N,total_active),[v,tau,It0],[],[],[],[],lim_inf,lim_sup,[],options);
pars=num2cell([parmin(1:2),scalefac*parmin(3)]);
pars=[pars(1),pars(2),g,pars(3)];
end
%function called from the above one
function [costo]=error_SIR(data,pars,tempi,N,totale_attivi)
%Assign the parameters
pars=num2cell(pars);
[v,tau,g,I0]=pars{:};
%Simulate the model
T=size(data,2);
Q0=data(1,1);
S0=N-I0-Q0;
[~,y]=ode45(@(t,x) SIR(t,x,v,tau,g,totale_attivi),tempi,[S0;I0;Q0]);
if length(tempi)==2
y=[y(1,:);y(end,:)];
end
%Identify on the normalized data (Data/mean data)
costo=sum(((diff(sum(data,1))./sum(data(:,2:end),1))-(diff(sum(y(:,3),2))./sum(y(2:end,3),2))').^2);%+0.05*(pars{1}+pars{2})^2;
end
Answers (0)
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!