Genetic algorithm fot multi differential equations
4 views (last 30 days)
Show older comments
abdelghani msaad
on 14 Mar 2021
Commented: abdelghani msaad
on 18 Jul 2022
good mornig i am working at a project in which i want to optimize the parameters of the 3 differential equations defined in the fun function, the parameters are 2, a = x(1) and b = x(2), i used the command ga to optimize them according to the available data that io already have, but i don't understand what i wrong because i get a lot of errors, also i don't know how to plot the final graph and compare it with the real one, here is the code:
clear all
N = 1e7;
dt = 1;
%% first data curve
Tmax = 99;
t = 1:dt:Tmax;
data = xlsread('MyData.xlsx', 'Foglio1', 'B1:B100'); %infected people per day
%% Loading data into arrays
y = data(:,1); %copy data into array
I = y'; % vector of infected people per day
I0 = I(1);
%% setting conditions for the ga
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0 0];
ub = [5 5];
variables = 2;
%% defining the fitness function and the distance fitness function which will be inside the ga
fun = @(x) fitness_fun(x,Tmax,N,dt);
objFun = @(x) norm(fun(x)-I);
%% solution given by the ga
coeff = ga(objFun,variables,A,b,Aeq,beq,lb,ub);
%% graph of the solution compared withj the real one
axes();
plot(t, I, 'b+');
hold on
plot(t, fun(coeff), 'r-');
legend({'Data points', 'Fitted Curve'})
the following is the fitness function which contains the ode45 command for the fun function with the differential equations:
function y = fitness_fun(x,Tmax,N,dt)
tspan = 0:dt:Tmax;
y0 = [N 1 0];
[a,y] = ode45(@(x) fun, tspan, y0);
end
%% series of function for the ODE solver
function dydt = fun(x,y)
N = 1e4;
a = x(1);
b = x(2);
dydt(1) = -a*y(1)*y(2)/N; %-a*S*I
dydt(2) = a*y(1)*y(2)/N - b*y(2); % a*S*I
dydt(3) = b*y(2); % b*I
end
0 Comments
Accepted Answer
Star Strider
on 14 Mar 2021
The ‘fitness_fun’ function should probably be something like this:
function y = fitness_fun(x,t,N)
tspan = t;
y0 = [N 1 0];
[t,y] = ode45(@(x) fun, tspan, y0);
%% series of function for the ODE solver
function dydt = fun(t,y)
dydt = zeros(3,1);
N = 1e4;
a = x(1);
b = x(2);
dydt(1) = -a*y(1)*y(2)/N; %-a*S*I
dydt(2) = a*y(1)*y(2)/N - b*y(2); % a*S*I
dydt(3) = b*y(2); % b*I
end
end
Also, the time (or independent variable) vector for the data must be the ‘tspan’ argument, or it will not be possible to compare the fitted result with the data. I added the zeros call so that ‘fun’ will return a column vector, and added ‘t’ as its first argument. It will inherit the parameter vector ‘x’ from the outer function workspace.
I cannot test this, however my changes should get it closer to working. (I generally pass the initial conditions vector as the last elements of the parameter vector so that the optimisation routine can fit them as well.)
0 Comments
More Answers (4)
abdelghani msaad
on 19 Mar 2021
3 Comments
abdelghani takha
on 7 Nov 2021
hi sir
how to use Genetic algorithm for this differential equations
dx1dt = a0*x(1) - w0*x(2);
dx2dt = a0*x(2) + w0*x(1);
dx3dt = - sum(ai.*dti.*exp(-0.5*(dti./bi).^2))- 1.0*(x(3) - zbase);
abdelghani msaad
on 7 Nov 2021
6 Comments
Omima Musa Mohmed Abusil
on 16 Jul 2022
I do have this code but the ga has failed at initialize the value and I do not know why? so will you help me to locate the issue and thanks
here is my code
function [J,Jv]=paramfun(theta,t,bt0)
% Monod Model for PPB growth
% dx/dt = Mumax*inhibition factor*x - kd*x
% ds/dt = -(1/y)*Mumax*inhibition factor*x
% with
% variable b(1) = x, b(2) = s
% parameter theta(1) = Mumax, theta(2) = inhibition factor, theta(3) = decay, theta(4)= yield
[T,Jv] = ode45(@fun,t,bt0);
function dC = fun(t,b);
dcdt = zeros(2,1);
dcdt(1)= theta(1)*theta(2)*b(1)-theta(3)*b(1);
dcdt(2) = -(1/theta(4))*theta(1)*theta(2)*b(1);
dC=dcdt;
end
J=Jv(:,2);
end
clear all
clc
t = [0 2.1 4 5 22.5 24.5 26.5 28.5 29.5 46.5 48.5 50.5 52.5 53.5 70.5 73.25 75.5]';
x = [19.5 23.57 24.33 24.33 80.6 100.76 142.2 174.14 188.21 321.3 331.18 324.33 322.432 322.432 327.755 332.052 319.77]';
s = [999.957 996.4 982.012 968.86 495.17 459.42 429.2 403.65 392.43 292.94 287.5 282.74 278.55 276.64 256.49 254.6 253.22]';
b = [x s];
% optimization
lb = [0,0,0,0];
ub = [0.04,0.5,0.03,1];
%%
A = [];
b = [];
Aeq = [];
beq = [];
%%
nvar = 4%;%number of variable in the objective function
%% to call the objective function
fun =@(b)paramfun(theta,t,bt0);
objFun = @(b)norm(fun(b)-b);
%fitnessF = @(theta,t,bt0)Mysim(theta,t,bt0);
%% defining the iteration option
options = optimoptions('ga','Display','iter','MaxGenerations',1000*nvar,'PopulationSize',55,'FunctionTolerance',1e-6,'PlotFcn',@gaplotbestindiv);
%% optimization
[b,fval]=ga(objFun,nvar,A,b,Aeq,beq,lb,ub,[],[],options);
%% drawing model data and real one
axes()
plot(t,b,'o'); hold on;
plot(t,fun([b,fval]),'-bo'); hold off;
waiting for your feedback urgently
abdelghani takha
on 7 Nov 2021
hi sir
In the case of variables :
ai = [-60 -15 0 15 90];
bi = [1.2 -5 30 -7.5 0.75];
ci = [0.25 0.1 0.1 0.1 0.4];
How do I write Ib and Ub
0 Comments
See Also
Categories
Find more on Surrogate Optimization 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!