Built-in function for Differentiation:
The MATLAB has a built-in function, diff that returns the differences between consecutive elements in a vector. For illustration,
>> diff([4 7 15 32])
ans =
3 8 17
For a function y = f(x) here x is a vector, and the values of f '(x) can be approximated as diff(y) divided by diff(x). For illustration, the earlier equation can be written as an anonymous function.
>> f = @ (x) x .^ 3 + 2 .* x .^ 2 - 4 .* x + 3;
>> x = 1:3;
>> y = f(x)
y =
2 11 36
>> diff(y)
9 25
>> diff(x)
1 1
>> diff(y) ./ diff(x)