MATLAB: Problem with function @(t) [ (1/2)*a*t.^2]

room

function eulers_for_mv
a = .5 ;
t = linspace(0,2) ;
dt = t(2) - t(1) ;
x = zeros(1,length(t));
v = zeros(1,length(t));
for i=1:length(t)-1
v(i+1) = v(i) + dt*a
x(i+1) = x(i) + dt*v(i) ;
end
original_eq = @(t) [x(1) + v(1)*t +(1/2)*a*t.^2] ;
original_eq(t) —> returns to me a 1×200 matrix because I left some room between t and 1/2 it shouldnt matter no?? if I take this away writing …t+(1/2) … it`ll give me the correct 1×100 matrix. why??

Best Answer

MATLAB interprets spaces in matrices as separators, so it does matter in that situation. In matrices, put parentheses around expressions you want MATLAB to consider as single expressions.
If you want ‘v(1)*t +(1/2)*a*t.^2’ to be a single expression, write it as:
original_eq = @(t) [x(1) + (v(1)*t +(1/2)*a*t.^2)] ;