You are trying to use diff
to differentiate a function. Out-of-the-box diff
performs a difference operation between pairs of elements. You don't want this. Instead, make your f
and fp
as actual function handles. First create the symbolic definition of your function f
, then differentiate this symbolic representation using the symbolic version of diff
(which you can just call with diff
itself), then create a MATLAB function with matlabFunction
out of this:
%// Define symbolic variable
syms x;
%// Define function symbolically
y = tan(x) - 2*x;
%// Define function handles (numerical) to the original and derivative
f = matlabFunction(y);
fp = matlabFunction(diff(y));
%// Now call Newton's Method
[y, iter] = newton(f, fp, 1.4);
Take note that f
and fp
are already function handles. That's what matlabFunction
returns, so there isn't a need to create a handle via @
as inputs into your Newton's Method function anymore.
Running this modification to your code, I get this for the root with the initial guess at x = 1.4
and the amount of iterations it took:
>> format long g
>> y
y =
1.16556118520721
>> iter
iter =
8
If the Symbolic Mathematics Toolbox is missing...
If, for some reason, you don't have the Symbolic Mathematics Toolbox, then what I suggested won't work. As such, you don't have a choice but to use the discrete approximation of the derivative to get this to work. However, we can still work with the code I wrote above, but fp
will have to be defined differently.
If you recall, the definition of the derivative is such that:

To get this to work in the discrete case, you make Δx
very small... something like 1e-10
for example.
As such, you would do this instead with anonymous functions:
%// Define function
f = @(x) tan(x) - 2*x;
%// Define derivative
h = 1e-10;
fp = @(x) (f(x + h) - f(x)) / h;
%// Now call Newton's Method
[y, iter] = newton(f, fp, 1.4);
With this, I get:
>> format long g;
>> y
y =
1.16556118520721
>> iter
iter =
8
I'd say that's pretty darn close!