im looking for the error in the following code, please help
Ältere Kommentare anzeigen
% Fractional SIR System using Caputo Derivative
clear all; clc;
% Parameters
N = 1000; % Population size
beta = 0.5; % Contact rate
gamma = 0.1; % Recovery rate
alpha = 0.7; % Fractional order
% Initial conditions
I0 = 1; % Initial number of infected individuals
S0 = N - I0; % Initial number of susceptible individuals
R0 = 0; % Initial number of recovered individuals
% Time steps
t_start = 0;
t_end = 50;
dt = 0.1;
t = t_start:dt:t_end;
% Caputo derivative
CaputoDerivative = @(f,alpha,dt) 1/gamma(alpha)*(f(1)-f(2))/dt^(alpha);
% Initialization
S = zeros(1,length(t));
I = zeros(1,length(t));
R = zeros(1,length(t));
S(1) = S0;
I(1) = I0;
R(1) = R0;
% Iterative solution
for n = 1:length(t)-1
DalphaS = CaputoDerivative(S(1:n),alpha,dt);
DalphaI = CaputoDerivative(I(1:n),alpha,dt);
DalphaR = CaputoDerivative(R(1:n),alpha,dt);
S(n+1) = S(n) - beta*S(n)*DalphaI*dt;
I(n+1) = I(n) + beta*S(n)*DalphaI*dt - gamma*I(n)*DalphaI*dt;
R(n+1) = R(n) + gamma*I(n)*DalphaR*dt;
end
3 Kommentare
noureddine karim
am 16 Apr. 2023
I change the code to this following, but same probleme
But you still didn't change the
gamma(alpha)
in the definition of the fractional differentiation operator. It throws an error if you previously defined a scalar gamma as
gamma = 0.05; % Recovery rate
because the gamma function will not be used then.
Further
diff([0,t.^(alpha-1)])
equals
t.^(alpha-1) - 0 = t.^(alpha-1)
If this is what you want, there is no need to use "diff" here.
noureddine karim
am 17 Apr. 2023
Antworten (3)
Image Analyst
am 16 Apr. 2023
0 Stimmen
See the FAQ for a thorough discussion:
https://matlab.fandom.com/wiki/FAQ#%22Subscript_indices_must_either_be_real_positive_integers_or_logicals.%22
DalphaS = CaputoDerivative(S(1:n),alpha,dt);
DalphaI = CaputoDerivative(I(1:n),alpha,dt);
DalphaR = CaputoDerivative(R(1:n),alpha,dt);
For n = 1, you call CaputoDerivative with S(1:n), I(1:n) and R(1:n) each having only a single element.
But CaputoDerivative expects that it has at least two elements: f(1) and f(2).
And - as Walter noticed - gamma(alpha) is not defined in your case because you set gamma to a value instead of using the gamma function in your formula.
Walter Roberson
am 16 Apr. 2023
gamma = 0.1; % Recovery rate
gamma is a scalar not a function
CaputoDerivative = @(f,alpha,dt) 1/gamma(alpha)*(f(1)-f(2))/dt^(alpha);
That tries to use the gamma function, but gamma is a scalar
1 Kommentar
Walter Roberson
am 16 Apr. 2023
If the intention is
then you would need 1./(gamma .* alpha) . (Note that could be calculated in advance instead of recalculating it each time.)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!