How to iterate my algorithm?

2 visualizaciones (últimos 30 días)
Thomas Veith
Thomas Veith el 6 de Jun. de 2019
Comentada: Thomas Veith el 6 de Jun. de 2019
So I have the code below, and I think I need to put another for-loop around it, but I'm not exactly sure how to get where I want to go.
In the example below, I solve my ODE for ten 'individuals', sort by MSE, delete the bottom half of results (IE, the ones with greatest MSE), genereate parameters for five 'new individuals', then solve again and sort my results by MSE. I'd like to do this a total of ten times before I finally end with a 10x6 numeric array sorted by MSE.
Any advice?
Thanks in advance.
load('patientdata1.mat')
%%assign number of individuals and setup vector for parameters
n = 10 ;
parameters = zeros(n,6);
%%initialize parameters
for k=1:n ;
alpha0=1;psa0=29;gamma=rand;psi=rand;beta=rand;mse=0;
parameters(k,:) = [alpha0,psa0,gamma,psi,beta,mse];
end;
%%solve ODE
for k = 1:size(parameters,1)
tspan=linspace(0,7,50);
ODE = @(t,x,gamma,psi,beta) [-gamma*x(1) + psi*x(1); beta*x(2) - x(1)*x(2)];
[t,x(:,:,k)] = ode45(@(t,x) ODE(t,x,parameters(k,3),parameters(k,4),parameters(k,5)), tspan, [parameters(k,1) parameters(k,2)]);
end
%%access all x2 columns from x
x2 = x(:,[2],:);
%%grab last entry from each individual in x2
lp=x2(end,1,:);
lastposition=lp(:);
%%calulate mse
msematrix=[lastposition-patientdata1(9,2)].^2;
%%update parameters matrix with mse values and sort by mse
parameters(:,6)=msematrix(:);
parameters=sortrows(parameters,6);
%%delete bottom 5 rows
parameters([6:10],:)=[];
%%initialize 5 new parameter sets
>> parameters([6:10],:)=[alpha0,psa0,rand,rand,rand,mse;alpha0,psa0,rand,rand,rand,mse;alpha0,psa0,rand,rand,rand,mse;alpha0,psa0,rand,rand,rand,mse;alpha0,psa0,rand,rand,rand,mse];
%%solve ODE again with new paremeters matrix
for k = 1:size(parameters,1)
tspan=linspace(0,7,50);
ODE = @(t,x,gamma,psi,beta) [-gamma*x(1) + psi*x(1); beta*x(2) - x(1)*x(2)];
[t,x(:,:,k)] = ode45(@(t,x) ODE(t,x,parameters(k,3),parameters(k,4),parameters(k,5)), tspan, [parameters(k,1) parameters(k,2)]);
end
%%access all x2 columns from x
x2 = x(:,[2],:);
%%grab last entry from each individual in x2
lp=x2(end,1,:);
lastposition=lp(:);
%%calulate mse
msematrix=[lastposition-patientdata1(9,2)].^2;
%%update parameters matrix with mse values and sort by mse
parameters(:,6)=msematrix(:);
parameters=sortrows(parameters,6);
  2 comentarios
Geoff Hayes
Geoff Hayes el 6 de Jun. de 2019
Thomas - try putting the loop after where you populate/initialize the parameters
load('patientdata1.mat')
%%assign number of individuals and setup vector for parameters
n = 10 ;
parameters = zeros(n,6);
%%initialize parameters
for k=1:n ;
alpha0=1;psa0=29;gamma=rand;psi=rand;beta=rand;mse=0;
parameters(k,:) = [alpha0,psa0,gamma,psi,beta,mse];
end;
% iterate n times
for v = 1:n
%%solve ODE
for k = 1:size(parameters,1)
tspan=linspace(0,7,50);
ODE = @(t,x,gamma,psi,beta) [-gamma*x(1) + psi*x(1); beta*x(2) - x(1)*x(2)];
[t,x(:,:,k)] = ode45(@(t,x) ODE(t,x,parameters(k,3),parameters(k,4),parameters(k,5)), tspan, [parameters(k,1) parameters(k,2)]);
end
% etc.
end
Thomas Veith
Thomas Veith el 6 de Jun. de 2019
I think that did the trick, thank you so much!!!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by