Clear Filters
Clear Filters

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

2 views (last 30 days)
I have tried to code Covid19 mathematical model but got "Error using vertcat" everytime.
This is a SEIRD model.
function [time,state_values]= mathematical
%%Initial values
t0 = 0;
tend = 100;
tspan=[t0,tend];
y0 = [100,10,3,0,0];
%%Constant(rates) values
A=0;
e=0.3;
a=0.4;
g=0.23;
k=0.3;
p=0.25;
m=0.12;
b=0.45;
s=y0;
%% [sdot] = g(t,s)
sdot = [A-(e*s(1)*s(2))-(a*s(1)*s(3))+(g*s(4));
-((k+p)*s(2)) +(e*s(1)*s(2))+(a*s(1)*s(3));
-((b+m)*s(3)) +(k*s(2));
-(g*s(4))+ (p*s(2))+(b*s(3));
(m*s(3))];
%%Calling ODE45 solver
[time, state_values] = ode45(@(t,s) sdot,tspan,y0);
%%Extracting individual values
S= state_values(:,1);
E= state_values(:,2);
I= state_values(:,3);
R= state_values(:,4);
D= state_values(:,5);
%%Plot S,E,I,R,D
figure(1)
clf
plot(time,S); title('Susceptible Population vs Time')
figure(2)
clf
plot(time,E); title('Exposed Population vs Time')
figure(3)
clf
plot(time,I); title('Infected Population vs Time')
figure(4)
clf
plot(time,R); title('Recovered Population vs Time')
figure(5)
clf
plot(time,D); title('Death Population vs Time')
end

Answers (1)

KSSV
KSSV on 6 May 2022
Becareful while leaving space between terms in the array.
function [time,state_values]= mathematical
%%Initial values
t0 = 0;
tend = 100;
tspan=[t0,tend];
y0 = [100,10,3,0,0];
%%Constant(rates) values
A=0;
e=0.3;
a=0.4;
g=0.23;
k=0.3;
p=0.25;
m=0.12;
b=0.45;
s=y0;
%% [sdot] = g(t,s)
sdot = [A-(e*s(1)*s(2))-(a*s(1)*s(3))+(g*s(4));
-((k+p)*s(2))+(e*s(1)*s(2))+(a*s(1)*s(3));......
-((b+m)*s(3))+(k*s(2));
-(g*s(4))+(p*s(2))+(b*s(3));
(m*s(3))];
%%Calling ODE45 solver
[time, state_values] = ode45(@(t,s) sdot,tspan,y0);
%%Extracting individual values
S= state_values(:,1);
E= state_values(:,2);
I= state_values(:,3);
R= state_values(:,4);
D= state_values(:,5);
%%Plot S,E,I,R,D
figure(1)
clf
plot(time,S); title('Susceptible Population vs Time')
figure(2)
clf
plot(time,E); title('Exposed Population vs Time')
figure(3)
clf
plot(time,I); title('Infected Population vs Time')
figure(4)
clf
plot(time,R); title('Recovered Population vs Time')
figure(5)
clf
plot(time,D); title('Death Population vs Time')
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!