How to iterate my algorithm?

3 次查看(过去 30 天)
Thomas Veith
Thomas Veith 2019-6-6
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 个评论
Geoff Hayes
Geoff Hayes 2019-6-6
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 2019-6-6
I think that did the trick, thank you so much!!!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by