To numerically differentiate cos(x) at x = 1 using a first-order forward difference approximation, we can use the following formula:
f'(x) ≈ (f(x + h) - f(x)) / h
where f(x) is the function we want to differentiate, h is the step size, and f'(x) is the approximation of the derivative.
In MATLAB, we can calculate the numerical approximation and the error using the following code:
main.m513 chars15 lines
This code will produce the numerical approximation of the derivative at x = 1 and the corresponding error for each step size:
main.m452 chars21 lines
As the step size gets smaller, the numerical approximation becomes more accurate and the error decreases.
gistlibby LogSnag