Results 1 to 8 of 8
 02022012, 02:40 PM #1Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
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:
Java 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(n1,X)); } public static int factorial (int n){ if(n>1)return n*factorial(n1); else return 1; } }
 02022012, 02:57 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: Something weird about a math function
Your factorial method overflows for (reasonably) large values of n.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 02022012, 03:05 PM #3Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
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!
 02022012, 03:14 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: Something weird about a math function
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 02022012, 04:32 PM #5Senior Member
 Join Date
 Dec 2011
 Posts
 102
 Rep Power
 0
Re: Something weird about a math function
I really needed this calculous refreshment :
I get it now :)
Thanks man!
 02022012, 05:34 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: Something weird about a math function
Build a wall around Donald Trump; I'll pay for it.
 02022012, 05:37 PM #7Moderator
 Join Date
 Apr 2009
 Posts
 13,372
 Rep Power
 24
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!
 02022012, 05:40 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Similar Threads

Checking if a math function is valid
By HardToHandle in forum New To JavaReplies: 3Last Post: 01182012, 02:40 AM 
painting math function graph
By batia in forum Advanced JavaReplies: 5Last Post: 09172011, 08:58 PM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Weird problem upon calling same function twice
By alin_ms in forum New To JavaReplies: 2Last Post: 12202008, 07:14 PM 
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08062007, 03:31 AM
Bookmarks