Error using vertcat Dimensions of arrays being concatenated are not consistent.

4 次查看(过去 30 天)
Im trying to create an SEIR Model for a covid simulation and keep getting the following error messages. The error seems to come in when I make beta dependent on R (that is it everything functions properly with no errors when I make beta a constant), which turns it into a 1 by 365 matrix. I thought that when this happens, I need to change the code in my ode equation to include a " .* " after the matrix beta to do it sort of piece wise, but I keep getting these errors. Any help would be appreciated.
Here is the code:
%To find Ri value from data, which is assumed to fit an exponential model
day35=1:1:35;
Italy35=[3 3 3 17 79 132 229 322 400 650 888 1128 1689 2036 2502 3089 3858 4636 5883 7375 9172 10149 12462 15113 17660 21157 23980 27980 31506 35713 41035 47021 53578 59138 63927];
%Using cftool and creating an exponential fit gives a growth rate in the data of
r=0.1387;
%Parameters
t=linspace(0,365,365); %set to give days after initial infection
sigma=1/3; %exposure rate
gamma=1/10; %recovery rate
alpha=1/20; %death rate
Ri=[[(r^2)+r*(sigma+gamma)]/(sigma*gamma)]+1 ; %Initial basic reproduction number, as determined above
Rf=2.3; %Estimated final basic reproduction number
L=75; %Day of extreme quarantine measures
w=0.5; %Determines quarantine rate
R=((Ri-Rf)./[1+exp(-w*(-t+L))])+Rf; %Changing basic reproduction number
%birth and death rates, respectively:
mu=0.005;
nu=0.002;
%Total population-S, E, I, R sum to this:
N=50000;
beta=R*gamma; %Infectious rate
%Initial Conditions
Eo=5;
Io=1;
Ro=0;
Do=0;
%ODE Equation Solver
f = @(t,x) [mu*N-nu*x(1)-beta.*(x(1)*x(3)/N);beta.*(x(1)*x(3)/N)-(nu+sigma)*x(2);sigma*x(2)-(gamma+alpha)*x(3);gamma*x(3);alpha*x(3)];
%The @(t,x) term is just the function handle. Here I have created a function with four pieces, each representing the right sides of the SEIR equations.
%In place, I have x(1)=S, x(2)=E, x(3)=I, x(4)=R, and x(5)=D
[t,xa]=ode45(f,[0 365], [N Eo Io Ro Do]);
%ode45 takes each term in the function f and integrates the function from 0 to 365. [N Eo Io Ro Do] represents the inital conditions for SEIRD respectively
a1=plot(t,xa(:,1),'b'); M1='S';
hold on
%Hold allows for the addition of more plots without deleting the first
a2=plot(t,xa(:,2),'k'); M2='E';
a3=plot(t,xa(:,3),'r'); M3='I';
a4=plot(t,xa(:,4),'g'); M4='R';
a5=plot(t,xa(:,5),'y'); M5='D';
hold off
legend([a1, a2 a3 a4 a5], M1, M2, M3, M4, M5)
And here is the error message:
  1 个评论
Jarred Diaz
Jarred Diaz 2020-5-6
Just tested it out a little bit, and the error only comes with the following portion of the code. Everything beforehand shows no error
[t,xa]=ode45(f, 0:365, [N Eo Io Ro Do]);
%ode45 takes each term in the function f and integrates the function from 0 to 365. [N Eo Io Ro Do] represents the inital conditions for SEIRD respectively
a1=plot(t,xa(:,1),'b'); M1='S';
hold on
%Hold allows for the addition of more plots without deleting the first
a2=plot(t,xa(:,2),'k'); M2='E';
a3=plot(t,xa(:,3),'r'); M3='I';
a4=plot(t,xa(:,4),'g'); M4='R';
a5=plot(t,xa(:,5),'y'); M5='D';
hold off
legend([a1, a2 a3 a4 a5], M1, M2, M3, M4, M5)

请先登录,再进行评论。

采纳的回答

Mehmed Saad
Mehmed Saad 2020-5-6
编辑:Mehmed Saad 2020-5-6
Because R in line 14 is an array due to which beta is an array of size 1x365.
The size of Five Rows you are trying to concatenate in function f are
[1x365;1x365;1x1;1x1;1x1]
You can not concatenate them this way. if you take beta transpose they will concatenate but ode45 initial condition length is 5 and output of f will be 733 so there will be an error again.

  8 个评论
Jarred Diaz
Jarred Diaz 2020-5-6
Thank you so much for breaking it down! I just started coding 2 weeks ago and was really struggling with this. I appreciate your patience!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by