Taylor series for arctan
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to estimate the value of arctan using taylor series method and I have written this code but I keep getting error meassages?!
"The function should accept 3 parameters: value of , number of significant figures accuracy i.e. , and the maximum number of iterations. In the function, use in order to continue until the falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final and the number of iterations it took.Submit the snapshots showing the program running from the command prompt for x = 0.4 and 0.8, and plot Absolute true error vs. number of terms."
function [Tay1,n,ea]=Tayler(x,k,n_max)
es=0.5*10^(2-k)
Tay(1)=0;
sum=0;
n=1;
ea=6;
while(ea>es && n<n_max)
sum=sum+(-1)^(n+1)*(x^(2*n-1))/(2*n-1);
Tay(n+1)=sum;
ea=abs(Tay(n+1)-Tay(n));
Tay1=Tay(end);
n=n+1;
end
%%%%%%%%% function calling
x=1;
k=5;
n_max=100;
[Tay1,n,ea]=Tayler(x,k,n_max)
es =5.0000e-04
Tay1 =0.7879
n =100
ea =0.0051
6 comentarios
Geoff Hayes
el 6 de Mzo. de 2020
Abdullah - please read declare function name, inputs, and outputs to understand how functions are defined and how they are called/used. I'm guessing that you didn't actually write this code and so have copied it from somewhere or from someone else. Since the function Tayler (sp) has been defined with three inputs, then you must pass in three input parameters. When you call it like
>> Tayler
you are not passing in ANY input parameters. So the function throws the error "Not enough input arguments" when it tries to use one of the inputs in the function body. Instead, you need to define and pass in these input parameters like with the code (that you have pasted above)
>> x=1;
>> k=5;
>> n_max=100;
>>[Tay1,n,ea]=Tayler(x,k,n_max)
I'm wondering if all of the code defined after
%%%%%%%%% function calling
is included in your Tayler.m file. Is it?
Respuestas (3)
David Hill
el 6 de Mzo. de 2020
function [Tay1,n,ea]=Tayler(x,k,n_max)
Tay1=repmat(x,1,n_max).^(1:2:2*n_max).*(-1).^(0:n_max-1)./(1:2:2*n_max);
Tay2=Tay1(abs(Tay1)>10^-k);
Tay3=cumsum(Tay2);
n=length(Tay2);
Tay1=Tay3(end);
ea=abs(atan(x)-Tay3);
plot(1:n,ea);
ea=ea(end);
0 comentarios
Geoff Hayes
el 6 de Mzo. de 2020
Abdullah - there are two parts to the code that you have shown. The code for the function and the sample code that can be used to call this function. For example, in a file called Taylor.m, you should have this code only
function [Tay1,n,ea]=Taylor(x,k,n_max)
es=0.5*10^(2-k)
Tay(1)=0;
taylorSum=0;
n=1;
ea=6;
while(ea>es && n<n_max)
taylorSum = taylorSum+(-1)^(n+1)*(x^(2*n-1))/(2*n-1);
Tay(n+1)=taylorSum;
ea=abs(Tay(n+1)-Tay(n));
n=n+1;
end
Tay1=Tay(end);
All I've changed is the name of the function (from Tayler to Taylor), replaced sum with taylorSum, and moved the Tay1=Tay(end); outside of the loop. I haven't tested the code or made any other changes since this is homework and so it is for you to understand what the code is doing (and where it may be going wrong). And that is all that should be in your m-file for Taylor.m.
The remaining code is simply an example of the input and output of running this function.
% x, k, and n_max are the input variables
x=1; % value of x
k=5; % number of significant figures accuracy
n_max=100; % maximum iterations for function
% here we are calling the function with those input variables (parameters)
[Tay1,n,ea]=Tayler(x,k,n_max)
and here is the (supposed) output of the function with the above input
es = 5.0000e-04 % this is the error threshold based on input k
Tay1 = 0.7879 % the approximate value of arctan(x) at the end of the program
n = 100 % the number of iterations it took
ea = 0.0051 % the error (?) difference between the last two elements in expansion
The code body, and in particular
while(ea>es && n<n_max)
taylorSum = taylorSum+(-1)^(n+1)*(x^(2*n-1))/(2*n-1);
Tay(n+1)=taylorSum;
ea=abs(Tay(n+1)-Tay(n));
n=n+1;
end
is implementing the Taylor series expansion for arctan (though I do question the formula given http://people.math.sc.edu/girardi/m142/handouts/10sTaylorPolySeries.pdf and other references). I think that you will need to determine if the code is actually implemented correctly.
0 comentarios
Khakoo Mal
el 25 de Dic. de 2021
But, this is valid for x is less or equal to 1. can anyone guide for x>1.
2 comentarios
Walter Roberson
el 25 de Dic. de 2021
format long g
syms x
around = 2;
terms = 10;
attay = taylor(atan(x), x, around, 'Order', terms)
attay0 = taylor(atan(x), x, 0, 'Order', terms)
fplot(attay, [-10 10]); title('taylor around 2')
fplot(attay0, [-10 10]); title('taylor around 0')
atan([-10 10])
double(subs(attay, x, [-10 10]))
double(subs(attay0, x, [-10 10]))
If you look at the terms, the approximation gets worse as you add more terms, unless you are sampling withing +/- 1 of the point of approximation.
John D'Errico
el 25 de Dic. de 2021
Editada: John D'Errico
el 25 de Dic. de 2021
Note that the Taylor series for the atan function will be poor for abs(x) even remotely large. abs(x)>1 is probably where I would expect it to be poor, and even close to 1 will not be good.
The simplest solution is to use range reduction methods. That is, if you cannot handle abs(x) > 1, then the simple identities are things like
atan(x) = -atan(-x)
atan(x) = pi/2 - atan(1/x)
Now you need never worry about large x.
For example...
x = 2;
atan(x)
pi/2 - atan(1/x)
As you can see, both return the same result.
If even x near 1 is a problem (convergence will be pretty slow at x==1 and even close to 1) then there are other identities one can use even there.
Ver también
Categorías
Más información sobre Mathematics and Optimization en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!