As a programming exercise I have written a Matlab function which finds the derivative of a function using the finite difference method. In a script I have called the function and wish to check it using the built-in functions, except I am having trouble implementing this.
In order to get a check I use diff(eqn) to get the differentiated equation, however I am not sure how to use this equation to then solve for a particular value, eg. x = 2.
Here is my code:
syms x
eqn= cos(x);
%set value for the derivative to be evaluated at
x2 = 2;
%create function handle
f = @(x) cos(x);
%call finite difference function
yderiv = derivative(f,x2)
%use built-in to get differentiated function
ycheck = diff(eqn)
With the output:
yderiv = -0.9093
ycheck =
-sin(x)
Any help on how to solve ycheck at x2, or a different approach to use would be most appreciated.
Cheers :)