MATLAB Answers


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

Asked by hgrlk
on 25 Mar 2019 at 8:24
Latest activity Edited by John D'Errico
on 25 Mar 2019 at 9:44
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.
clear all
close all
syms x
x = pi/8 : 0.01: 7*pi/8;
f = cot(x);
a = 5*pi/8;
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


Let me see if I understand this. You are writing a Taylor series for cot(x), expaded around a point a. In that series, you use the function cot(x). Can you see the fundamental problem in what you have done?
Yes, I use the function cot(x), expaded around a point a. Also for x ∈ [pi/8 , 7pi/8].
There is someting wrong in my code bu i cannot see. Also I could not write another different code for the problem. Thanks.
I explained the error in your code in my answer. It is in not understanding how to build that Taylor expansion.

Sign in to comment.




2 Answers

Answer by John D'Errico
on 25 Mar 2019 at 9:02
Edited by John D'Errico
on 25 Mar 2019 at 9:44
 Accepted 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];
xlabel 'X'
ylabel 'Y'
title 'Cot Taylor approximations'
hold on
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 =
fapp =
They are not themselves functions of x at this point.

  1 Comment

Thank you for answering so fast :) I understood your code and my mistakes.

Sign in to comment.

Answer by Torsten
on 25 Mar 2019 at 9:01

f = @(x)cot(x);
a = 5*pi/8;
P0 = @(x)f(a)*ones(size(x));
P1 = @(x)f(a)+(- f(a).^2 - 1).*(x-a);
P2 = @(x) f(a)+(- f(a).^2 - 1).*(x-a)+(2.*f(a).*(f(a).^2 + 1))/factorial(2).*((x-a).^2);
P3 = @(x)f(a)+(- f(a).^2 - 1).*(x-a)+(2.*f(a).*(f(a).^2 + 1))/factorial(2).*((x-a).^2)+(- 4.*f(a).^2.*(f(a).^2 + 1) - 2.*(f(a).^2 + 1).^2)/factorial(3).*((x-a).^3);
x = pi/8:0.01:7*pi/8;


Thank you It worked!!! I understand the @ but I couldn't understand why you used ones(size(x))
I wonder could you explain that? Thank you again....
In order to plot, you need to have P0(x) of the same size as x.
P0 = @(x) f(a)
to see that it doesn't work.

Sign in to comment.