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

assignmentb and i must be the samenumber of elements

t=0 : dt : tmax;
mpts=length(t);
for i=1:mpts
if (t>=0) & (t<=t1)
rd(i)=a1*t.^3+b1*t.^2;
r(i)=((1/4)*a1*t.^4)+((1/3)*b1*t.^3);
elseif (t>t1) & (t<=t2)
rd(i)=m;
r(i)=m*(t-t1)+k1;
else
rd(i)=a3*t.^3+b3*t.^2+c3*t+d3;
r(i)=a3/4*(t.^4-t2.^4)+b3/3*(t.^3-t2.^3)+c3/2*(t.^2-t2.^2)+d3*(t-t2)+k2;
end
end
The error occurs at the final 'rd(i)' section, I'm at a loss as to why! t1, t2 and t3 are all defined above this section of code. As is m, k1 and k2.
Any help is much appreciated!

Best Answer

t is a vector, so the expressions involving t that you have written will be vectors. You can't stuff a vector into a single element like rd(i) or r(i). I think you just need to index into t on your rhs equations. E.g.,
if (t(i)>=0) & (t(i)<=t1)
rd(i)=a1*t(i)^3+b1*t(i)^2;
r(i)=((1/4)*a1*t(i)^4)+((1/3)*b1*t(i)^3);
:
etc.