In this line
y2(i) = (f(i+2) - 2*f(i+1) + f(i))/(deltaX.^2);
index of f is i+2, therefore, i should not go above n. Change the range of the for loop like this
for i = 1:n-2
y(i) = (f(i+1)- f(i))/deltaX;
y2(i) = (f(i+2) - 2*f(i+1) + f(i))/(deltaX.^2);
end
Similarly in the line
y4(i) = (f(i) -- 2*f(i-1) + f(i-2))/(deltaX.^2);
index of f is i-2, so i should not go below 3
for i = 3:n
y3(i) = (f(i)- f(i-1))/deltaX;
y4(i) = (f(i) -- 2*f(i-1) + f(i-2))/(deltaX.^2);
end
Best Answer