Borrar filtros
Borrar filtros

Parse error at observed data

3 visualizaciones (últimos 30 días)
Abdullahi
Abdullahi el 10 de Ag. de 2024
Respondida: Torsten el 10 de Ag. de 2024
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [
0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200
];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Define the objective function for optimization
function error = objectiveFunction(params)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
% Solve the ODE with the current parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);
% Interpolate the model's output to match the time points of observed data
model_values = interp1(t, y(:, 2:5), tspan);
% Calculate the sum of squared errors
error = sum((model_values - observed_data(:, 2:5)).^2, 'all');
end
% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
% Perform optimization using fminsearch
options = optimset('MaxFunEvals', 1000, 'MaxIter', 1000);
optimized_params = fminsearch(@objectiveFunction, initial_params, options);
Unrecognized function or variable 'tspan'.

Error in solution>objectiveFunction (line 50)
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);

Error in fminsearch (line 209)
fv(:,1) = funfcn(x,varargin{:});
% Display optimized parameters
disp('Optimized parameters:');
disp(optimized_params);
% Solve the system with optimized parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;

Respuestas (2)

Star Strider
Star Strider el 10 de Ag. de 2024
It took a few minutes to determine what the problems are with this (there were several).
I got it to work. Most of the problems were with respect to passing all the necessary arguments to the various funcitons. I will let you explore the changes I made, probably the most important of which was using ‘@SITRModel’ to pass the function as an argument.
Try this —
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Define the objective function for optimization
function error = objectiveFunction(params,tspan,SITRModel,y0,N,observed_data)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
% Solve the ODE with the current parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);
% Interpolate the model's output to match the time points of observed data
model_values = interp1(t, y(:, 2:5), tspan);
% Calculate the sum of squared errors
error = sum((model_values - observed_data(:, 2:5)).^2, 'all');
end
% % Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
initial_params = initial_params + rand(1,6)/100
initial_params = 1x6
0.3046 0.1054 0.0536 0.0203 0.1001 0.1038
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% % Perform optimization using fminsearch
options = optimset('MaxFunEvals', 1000, 'MaxIter', 1000);
optimized_params = fminsearch(@(params)objectiveFunction(params,tspan,@SITRModel,y0,N,observed_data), initial_params, options);
% Display optimized parameters
disp('Optimized parameters:');
Optimized parameters:
disp(optimized_params);
0.3375 0.1071 0.0483 0.0476 0.0501 0.0536
% optimized_params = initial_params;
% Solve the system with optimized parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;
.

Torsten
Torsten el 10 de Ag. de 2024
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [
0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200
];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
objectiveFunction(initial_params,y0,N,observed_data)
ans = 2.8078e+04
% Perform optimization using fminsearch
options = optimset('MaxFunEvals', 10000, 'MaxIter', 10000);
%optimized_params = fminsearch(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, options);
optimized_params = fmincon(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, [],[],[],[],zeros(6,1),[],[],options);
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
% Display optimized parameters
disp('Optimized parameters:');
Optimized parameters:
disp(optimized_params);
0.3375 0.1334 0.0354 0.0342 0.0000 0.0000
objectiveFunction(optimized_params,y0,N,observed_data)
ans = 2.3540e+04
% Solve the system with optimized parameters
[t, y] = ode15s(@(t, y) SITRModel(t, y, optimized_params,N), linspace(0,160,100), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;
% Define the objective function for optimization
function error = objectiveFunction(params,y0,N,observed_data)
% Solve the ODE with the current parameters
[t, y] = ode15s(@(t, y) SITRModel(t, y, params,N), observed_data(:,1), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));
% Calculate the sum of squared errors
error = sum((y(:,2:5) - observed_data(:,2:5)).^2, 'all');
end
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, params,N)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end

Categorías

Más información sobre Monopole Antennas en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by