Question
using matlab
First Derivative Central: Write a user written function that expects as its input argument a vector of x values and a vector of corresponding y-values. The function must return a matrix with 2 columns. The first column contains 'mid-point' x-values. The second column must contain the centered difference approximations for the first derivative, dy/dx, at the step midpoints. In other words, the x values [(x+ x1)/2, (xst x)/2,.. (x+ x)/2] and corresponding first derivatives estimates should be returned. 2.
Explanation / Answer
%%% Matlab function %%%
function z = user_diff ( x,y)
l=length(x);
for n=1:l-1
z(n,1)=( x(n+1)+x(n))/2; %%% mid point value
z(n,2)=(y(n+1)-y(n))/(2*(x(n+1)-x(n))); %%% first derivatives using cetral difference
end