MATLAB Answers

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

hgrlk

### hgrlk (view profile)

さんによって質問されました 2019 年 3 月 25 日

### John D'Errico (view profile)

さんによって 編集されました 2019 年 3 月 25 日
John D'Errico

### John D'Errico (view profile)

さんの 回答が採用されました
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

John D'Errico

### John D'Errico (view profile)

25 Mar 2019
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?
hgrlk

### hgrlk (view profile)

25 Mar 2019
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.
John D'Errico

### John D'Errico (view profile)

25 Mar 2019
I explained the error in your code in my answer. It is in not understanding how to build that Taylor expansion.

サインイン to comment.

R2015a

## 2 件の回答

John D'Errico

### John D'Errico (view profile)

님의 답변 25 Mar 2019
John D'Errico

### John D'Errico (view profile)

님이 편집함. 25 Mar 2019
채택된 답변

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.

hgrlk

### hgrlk (view profile)

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

サインイン to comment.

Torsten

### Torsten (view profile)

님의 답변 25 Mar 2019

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;
plot(x,P3(x),x,cot(x))

hgrlk

### hgrlk (view profile)

25 Mar 2019
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....
Torsten

### Torsten (view profile)

25 Mar 2019
In order to plot, you need to have P0(x) of the same size as x.
Try
plot(x,P0(x))
with
P0 = @(x) f(a)
to see that it doesn't work.

サインイン to comment.

Translated by