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.
syms x
>> f = cot(x);
>> diff(f)
ans =
- cot(x)^2 - 1
>> diff(f,x,2)
ans =
2*cot(x)*(cot(x)^2 + 1)
Now, how might the code go?
a = 5*pi/8;
f = @(x) cot(x);
fa = cot(a);
fap = -cot(a)^2 - 1;
fapp = 2*cot(a)*(cot(a)^2 + 1);
P0 = @(x) repmat(fa,size(x));
P1 = @(x) fa + fap*(x-a);
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];
plot(a,fa,'ro')
xlabel 'X'
ylabel 'Y'
title 'Cot Taylor approximations'
hold on
fplot(f,interval,'r')
fplot(P0,interval,'m')
fplot(P1,interval,'g')
fplot(P2,interval,'b')
legend('f(a)','f(x)','P0(x)','P1(x)','P2(x)')
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.
fap
fap =
-1.17157287525381
fapp
fapp =
-0.97056274847714
They are not themselves functions of x at this point.
Best Answer