To differentiate in Matlab, you can use the built-in diff
function. This function takes in a vector (or matrix) and returns a vector (or matrix) of the same size with the differences between adjacent elements.
If you want to compute the derivative of a function at a specific point, you can use the diff
function together with indexing to get the adjacent values of the function, and then divide the difference by the step size:
main.m426 chars10 lines
The variable dfdx_at_pi2
now contains the value of the derivative of the sin
function at pi/2
. Note that as the step size used to take the differences approaches 0, the result will converge to the actual value of the derivative at the point.
gistlibby LogSnag