Error in Model Fitting

조회 수: 2 (최근 30일)
Abdullahi
Abdullahi 2024년 8월 7일
편집: Torsten 2024년 8월 8일
Hello! i have been to run this model fitting code but keeps on getting these error messages as:
  1. FUN must have two input arguments.
  2. userFcn_ME = initEvalErrorHandler(userFcn_ME,funfcn_x_xdata{3}, ...
  3. [params_fit, ~] = lsqcurvefit(obj_fun, params0, time, observed_I, [], [], options);
Find attached, the code,, Please help
function dydt = sir_model(t, y, beta, gamma)
S = y(1);
I = y(2);
R = y(3);
N = S + I + R; % Total population
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I;
dRdt = gamma * I;
dydt = [dSdt; dIdt; dRdt];
end
function error = model_error(params, time, observed_I, y0)
beta = params(1);
gamma = params(2);
% Solve the differential equations
[~, y] = ode45(@(t, y) sir_model(t, y, beta, gamma), time, y0);
% Extract the infectious data from the solution
model_I = y(:, 2);
% Compute the error as the difference between model and observed data
error = model_I - observed_I';
end
% Define the time vector and observed data
time = [0, 1, 2, 3, 4, 5]; % Example time points
observed_I = [10, 15, 20, 25, 30, 35]; % Example observed infectious data
% Initial guess for parameters
beta_guess = 0.3;
gamma_guess = 0.1;
params0 = [beta_guess, gamma_guess];
% Initial conditions
S0 = 1000; % Initial number of susceptible individuals
I0 = observed_I(1); % Initial number of infectious individuals
R0 = 0; % Initial number of recovered individuals
y0 = [S0; I0; R0];
% Define the objective function for fitting
obj_fun = @(params) model_error(params, time, observed_I, y0);
% Perform the curve fitting
options = optimoptions('lsqcurvefit', 'Display', 'off');
[params_fit, ~] = lsqcurvefit(obj_fun, params0, time, observed_I, [], [], options);
% Display the fitted parameters
beta_fit = params_fit(1);
gamma_fit = params_fit(2);
disp(['Fitted beta: ', num2str(beta_fit)]);
disp(['Fitted gamma: ', num2str(gamma_fit)]);

채택된 답변

Torsten
Torsten 2024년 8월 7일
편집: Torsten 2024년 8월 7일
% Define the time vector and observed data
time = [0, 1, 2, 3, 4, 5]; % Example time points
observed_I = [10, 15, 20, 25, 30, 35]; % Example observed infectious data
% Initial guess for parameters
beta_guess = 0.3;
gamma_guess = 0.1;
params0 = [beta_guess, gamma_guess];
% Initial conditions
S0 = 1000; % Initial number of susceptible individuals
I0 = observed_I(1); % Initial number of infectious individuals
R0 = 0; % Initial number of recovered individuals
y0 = [S0; I0; R0];
% Define the objective function for fitting
obj_fun = @(params,time) model_error(params, time, y0);
% Perform the curve fitting
options = optimoptions('lsqcurvefit', 'Display', 'off');
[params_fit, ~] = lsqcurvefit(obj_fun, params0, time, observed_I, [], [], options);
% Display the fitted parameters
beta_fit = params_fit(1);
gamma_fit = params_fit(2);
disp(['Fitted beta: ', num2str(beta_fit)]);
Fitted beta: 1.6778
disp(['Fitted gamma: ', num2str(gamma_fit)]);
Fitted gamma: 1.2954
hold on
plot(time,observed_I,'o')
plot(time,model_error(params_fit,time,y0))
hold off
grid on
function error = model_error(params, time, y0)
beta = params(1);
gamma = params(2);
% Solve the differential equations
[~, y] = ode45(@(t, y) sir_model(t, y, beta, gamma), time, y0);
% Extract the infectious data from the solution
model_I = y(:, 2);
% Compute the error as the difference between model and observed data
error = model_I.';
end
function dydt = sir_model(t, y, beta, gamma)
S = y(1);
I = y(2);
R = y(3);
N = S + I + R; % Total population
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I;
dRdt = gamma * I;
dydt = [dSdt; dIdt; dRdt];
end
  댓글 수: 6
Abdullahi
Abdullahi 2024년 8월 8일
I don't actually know the difference between the two..
what do you recommend?
Torsten
Torsten 2024년 8월 8일
편집: Torsten 2024년 8월 8일
Both solvers are equivalent - only the user interfaces are different.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Gamma Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by