function dy = SIR(t,y)
n=100;
dy=zeros(n+2,1);
for j=2:n
dy1=5*(n+1)*(y(1)+sum(y(j)))-n*y(1);
dy(j)=n*y(j-1)-n*y(j);
dy3 =-5*(n+1)*(y(1)+sum(y(j)));
dy4 =n*y(n);
dy=[dy1;dy(j);dy3;dy4];
end
Why is this giving me a vector of length 4? I want a vector of lenth n+2.
It is difficult to understand exactly what you want the code to do. My best understanding is that
dy1is to be in position $1$,dy3in position $n+1$,dy4in position $n+2$ and positions $2-n$ are calculated using the loop. This can be corrected as follows.It makes no sense to use
sum(y(j))as this is summing a single element. I have changed this to besum(y)which adds the elements ofy, as I think you meant to do.Also, if
nis the length ofy, they you should usen=length(y);instead of hard coding the value.