MATLAB: Why it display Undefined variable for x when i hit run?(for the last line)

definding x

f=@(x) sin(x); x1=0;x2=pi/3;x3=2*pi/3;x4=pi;n=4; a1=f(x1);a2=f(x2);a3=f(x3);a4=f(x4);
for i=1:n h(i)=x(i+1)-x(i); end

Best Answer

f=@sin;
x1=0;
x2=pi/3;
x3=2*pi/3;
x4=pi;
n=4;
a1=f(x1);
a2=f(x2);
a3=f(x3);
a4=f(x4);
a=[a1,a2,a3,a4]
h = zeros(1,n-1); %preallocation for speed and efficiency
for i=1:n-1
h(i)=a(i+1)-a(i);
end