Where's the error in my code
Show older comments
hi, im trying to figure out an error in my code, but no one seems to know where it is. Its a simple index error as i get matrix dimensions must agree. Here it is.
function mosquito = chikungunya(t,x,parameters);
uh= 500;
r= parameters(1); %rate at which humans are bitten
Tmh= parameters(2); %transmission probability of mosquito passing it on to human when biting
z= parameters(3); %rate at which humans recover
Thm= parameters(4); %transmission probability of humans passing it on to mosquitos when mosquitos bite.
Sh= x(1); %susceptible humans
Sm= x(2); %susceptible mosquitos
ih= x(3); %infected humans
Im= x(4); %infected mosquitos
Rh= x(5); %recovered humans
mosquito= zeros(5,1);
mosquito(1)= uh -(r*Tmh*Im*Sh);
mosquito(2)= -(z*Rh) -(r*Thm*ih*Sm)+(r*Tmh*Im*Sh);
mosquito(3)= (z*Rh);
mosquito(4)= -(r*Thm*ih*Sm);
mosquito(5)= (r*Thm*ih*Sm);
% RSS for chikungunya model
function WRSS = chikungunyaRSS(tspan,x0,parameters,data)
%Simulate ODE
[t,x] = ode45(@mosquito,tspan,x0,[],parameters);
%initial conditions:
x0= zeros(5,1);
x0(1)= 2900;
x0(2)= 500;
x0(3)= 100;
x0(4)= 100;
x0(5)= 0;
%output/measurement equation
y = x(:,3)/parameters(4);
%WRSS (sum of squared residuals (least squares))
WRSS = ((data- y)./(0.1*data))'*((data- y)'./(0.1*data));
%%Chikungunya Data Set
%Central America Chikungunya Data units are in days, numbers of people.
%Data from PAHO.
tspan = [0 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259];
data = [2320 2377 2407 2407 2407 2428 2507 2534 2540 2550 2555 4790 4797 4797 5014 5014 5053 2773 2736 2843 2907 2908 2913 2916 2919 2924 2924 2924 2930 2932 3932 3553 3555 3555 3555 3555 4370 4370]';
% Model Arranging:
%initial conditions:
x0= zeros(5,1);
x0(1)= 2900;
x0(2)= 500;
x0(3)= 100;
x0(4)= 100;
x0(5)= 0;
% Start and Stop time%
% t0 = 0;
% t1 = 1.1
%parameters
parameters= zeros(4,1);
parameters(1)= 100;
parameters(2)= 100;
parameters(3)= 100;
parameters(4)= 100;
% r, Tmh, z, Thm
%Estimates
Pestimates = fminsearch(@(parameters) chikungunyaRSS(tspan,x0,parameters,data),parameters, optimset('Display','iter'));
%Simulate ODE
% Start and end time
t0 =0;
tf = 259;
% MODEL!
[t_est,x_est] = ode45(@mosquito,tspan,x0,[],Pestimates);
%y
y_est =x_est(:,2)/parameters(4);
% x(3) is I, Parameters(4) = rate at which humans recover.
% Plot
figure(1)
set(gca,'LineWidth',1,'FontSize',16,'FontName','Arial');
hold on
plot(tspan,data,'yo','LineWidth',2);
plot(t_est,y_est,'k','LineWidth',2);
xlabel ('Time(days)');
ylabel ('Number of People');
legend('chikungunyaData','estimate');
hold off
1 Comment
Walter Roberson
on 8 Dec 2015
Your code references @mosquito but there is no routine by that name shown.
The first function in your file is chikungunya but that routine does not invoke the other routines.
You need to make clear which code belongs in which file, and then you need to tell us how to invoke it all.
Answers (0)
Categories
Find more on Ordinary Differential Equations 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!