MATLAB: I need matlab code for cot(x) Taylor Polynomial Function

codecotxhelpMATLABtaylor series

For x in range [pi/8 , 7pi/8] and f(x)=cot(x) I need to plot Taylor Polynomial P0(x),P1(x) and P2(x) of f about the base point x=a=5pi/8. increment step size must be 0.01
I cannot write the code, I tried something with writing the differentation manuel but when I plot the graph P0, P2 didnt gave me anything.
Also xmin=0.1 xmax=3 and ymin=-2,5 ymax=2.5
Thanks for helping.
clc
clear all
close all
syms x
x = pi/8 : 0.01: 7*pi/8;
f = cot(x);
a = 5*pi/8;
P0=cot(a);
P1=cot(a)+(- cot(a).^2 - 1).*(x-a);
P2=cot(a)+(- cot(a).^2 - 1).*(x-a)+(2.*cot(a).*(cot(a).^2 + 1))/factorial(2).*((x-a).^2);
P3=cot(a)+(- cot(a).^2 - 1).*(x-a)+(2.*cot(a).*(cot(a).^2 + 1))/factorial(2).*((x-a).^2)+(- 4.*cot(a).^2.*(cot(a).^2 + 1) - 2.*(cot(a).^2 + 1).^2)/factorial(3).*((x-a).^3);
xlabel('x axis')
ylabel ('y axis')
xlim([0.1 3])
ylim([-2.5 2.5])
grid on

Best Answer

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); % 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];
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.