How to iterate my algorithm?

조회 수: 2 (최근 30일)
Thomas Veith
Thomas Veith 2019년 6월 6일
댓글: 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