MATLAB: In an assignment A(:)=B, the number of elements in A and B must be the same error.

a(:)=berrorMATLABnumber of elements in a and b must be the same

Here is the code:
tFinal=2;
N = 200;
h=tFinal/N;
t=linspace(0,tFinal,N+1);
y=zeros(1,N+1);
yExact=(9)./((3.*t)-1+(10.*exp(-3.*t)));
y(1) = 1;
for n=1:N
y(n+1) = y(n) + h.*(y.*(3-t.*y));
end
plot(t,y,t,yExact,'–')
xlabel('t'); ylabel('y');
error200= abs(y(N+1)-yExact(N+1));
fprintf('The error went down by a factor of %f.\n',error100/error200);
MATLAB doesn't tell me which line the error is.
The error: In an assignment A(:) = B, the number of elements in A and B must be the same.

Best Answer

tFinal=2;
N = 200;
h=tFinal/N;
t=linspace(0,tFinal,N+1);
y=zeros(1,N+1);
yExact=(9)./((3.*t)-1+(10.*exp(-3.*t)));
y(1) = 1;
for n=1:N
y(n+1) = y(n) + h.*(y(n).*(3-t(n).*y(n)));
end
plot(t,y,t,yExact,'--')
xlabel('t'); ylabel('y');
error200= abs(y(N+1)-yExact(N+1));
fprintf('The error went down by a factor of %f.\n',error200);