I think what you do not understand is how to build that series expansion. A Taylor polynomial would be of the form:
P(x) = f(a) + f'(a)*(x-a) + ...
f''(a)*(x-a)^2/factorial(2) + ...
f'''(a)*(x-a)^3/factorial(3) + ...
You will truncate it at any order you wish. However, the ONLY dependence in there on x is in those powers of (x-a). Think about it like this, IF we allowed you to use cot(x) itself in the Taylor polynomial for cot(x), then the best approximation for cot(x) would be simply f(x) = cot(x).
So, we do not have the function itself evaluated at x in there. That would be rather mathematically incestuous. But, we are allowed to use the derivatives of f, evaluated at the expansion point. So, here are the first couple of derivatives.
>> f = cot(x);
- cot(x)^2 - 1
2*cot(x)*(cot(x)^2 + 1)
Now, how might the code go?
a = 5*pi/8;
f = @(x) cot(x); % for comparison purposes
% these next are now constants, evaluated at x=a.
fa = cot(a);
fap = -cot(a)^2 - 1;
% Alternatively, I could have written:
% fap = double(subs(diff(f,x,1),x,a));
fapp = 2*cot(a)*(cot(a)^2 + 1);
% Alternatively: I could have written:
% fapp = double(subs(diff(f,x,2),x,a));
% the zeroth order "polynomial"
% Note the way I carefully constructed P0, so
% that it returns a constant of the same size as x.
P0 = @(x) repmat(fa,size(x));
% linear Taylor polynomial
P1 = @(x) fa + fap*(x-a);
% quadratic Taylor polynomial
P2 = @(x) fa + fap*(x-a) + fapp*(x-a).^2/2;
So, each of P0, P1, P2 are now functions you can use, as well as f.
interval = [pi/8,7*pi/8];
title 'Cot Taylor approximations'
I'll let you finsh your work, because it looks like you wanted to add a 3rd order polynomial in there too. The extension is now simple, just adding one more term to the polynomial. Note that the cubic polynomial should now start to look very much more like cot(x).
In any event, the values fap, fapp, etc., are all now constants.
They are not themselves functions of x at this point.