# Something weird about a math function

• 02-02-2012, 02:40 PM
Mapisto
Something weird about a math function
Hi,

I've been asked to write a program that calculates cos(x) value with it's Taylor equivalent.
I've done it in 2 ways: 1) a loop 2)Recursion.

The thing is, it gives me the right values for every X between 0 and 2pi (using radians), as long as I'm giving it the "n" value of 10. If I input let's say n=100, it gives me "NaN".
What's more weird, if I stay with n=10, but give it a value of X=3 (pi), it gives me a wrong answer :|

You can find the Taylor equivalent of cos(x) here: Taylor series - Wikipedia, the free encyclopedia

And here's my program:

Code:

```import java.util.Scanner; public class RecursionEx2 {                 public static void main(String[] args) {                 Scanner input= new Scanner(System.in);                                 System.out.print("Please enter your n: ");                 int n= input.nextInt();                 System.out.print("Please enter your X: ");                 double X= input.nextDouble();                 loop(n,X);                 System.out.println("Rec: "+rec(n,X));                 }         public static void loop (int n, double X){                 double res=0;                 for(int i=0; i<=n; i++){                         res+=((Math.pow(-1, i)*Math.pow(X, 2*i))/(factorial(2*i)));                        }                 System.out.println("Loop: "+res);                         }                         public static double rec (int n, double X){                 if (n==0) return (Math.pow(-1, n)*Math.pow(X, 2*n)/factorial(2*n));                 else return  ((Math.pow(-1, n)*Math.pow(X, 2*n)/factorial(2*n))+rec(n-1,X));         }                 public static int factorial (int n){                                 if(n>1)return n*factorial(n-1);                        else return 1;                         }         }```
Thanks :)
• 02-02-2012, 02:57 PM
JosAH
Re: Something weird about a math function
Your factorial method overflows for (reasonably) large values of n.

kind regards,

Jos
• 02-02-2012, 03:05 PM
Mapisto
Re: Something weird about a math function
Oh.. right!
Forgot how fast factorials grow :|
But that explains only the n problem.

What about X when it's larger than 2 (pi)?

Thanks!
• 02-02-2012, 03:14 PM
JosAH
Re: Something weird about a math function
Quote:

Originally Posted by Mapisto
Oh.. right!
Forgot how fast factorials grow :|
But that explains only the n problem.

What about X when it's larger than 2 (pi)?

Thanks!

A Taylor expansion around zero (a McLaurin expansion actually) is quite inaccurate the further away you approximate a function value. Have a look at this link and pay special attention to the figure on the right side of the page. It is a Taylor expansion of the sine function. See how inaccurate it is around 2*pi.

kind regards,

Jos
• 02-02-2012, 04:32 PM
Mapisto
Re: Something weird about a math function
I really needed this calculous refreshment :|
I get it now :)

Thanks man!
• 02-02-2012, 05:34 PM
JosAH
Re: Something weird about a math function
Quote:

Originally Posted by Mapisto
I really needed this calculous refreshment :|
I get it now :)

Thanks man!

You're welcome of course; you could always try the Taylow expansion around pi if you want to know a value (a bit) larger than that; you do need to know the derivatives around pi (instead of zero). See the link I posted in a previous reply.

kind regards,

Jos
• 02-02-2012, 05:37 PM
Tolls
Re: Something weird about a math function
You're just stringing random words together now...and I'm not sure some of them aren't simply made up!
• 02-02-2012, 05:40 PM
JosAH
Re: Something weird about a math function
Quote:

Originally Posted by Tolls
You're just stringing random words together now...and I'm not sure some of them aren't simply made up!

Yes, sometimes my brains go bzzzzt! and I start talking like that; don't tell my old mom about it ;-)

kind regards,

Jos