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

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 Comment

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)

Sign in to comment.

 Accepted Answer

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 Comments

What are the four columns youre referencing?
5 Rows sorry
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)];
  1. Row 1 size is 1x365 (due to beta as its size is 1x365)
  2. Row 2 size is 1x365 (due to beta as its size is 1x365)
  3. Row 3 size is 1x1
  4. Row 4 size is 1x1
  5. Row 5 size is 1x1
You need to correct beta. its size must be 1x1
for example
beta = R(1)*gamma;
Then your code will run fine and the plots looks like
When I post the code with everything up until the ode45, the code works fine with no errors. As in, I can post f and there are no error codes
f is an anonymous function. Suppose i write a function
h = @(x,t) [x;[x t]]
it will execute fine. No error. But when i call it
h(1,2)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in @(x,t)[x;[x,t]]
Similar is the case with your code. ode45 calls your function f(x,t) and it has error in it so error in ode45 occured because it called it
I need to be able to get the beta to change, which is why its 1X365. Do you have any suggestions on how to make this work without Beta being a constant?
as you are feeding all the time to beta at once
t = 0:365;
while you need it to feed time one by one according to f(t,x) so define beta as anonymous function which depends on t
beta = @(t) (((Ri-Rf)./(1+exp(-w*(-t+L))))+Rf)*gamma;
Now replace beta with beta(t) in f i.e.
f = @(t,x) [mu*N-nu*x(1)-beta(t).*(x(1)*x(3)/N);beta(t).*(x(1)*x(3)/N)-(nu+sigma)*x(2);sigma*x(2)-(gamma+alpha)*x(3);gamma*x(3);alpha*x(3)];
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!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!