Results 1 to 8 of 8
- 02-02-2012, 01:40 PM #1
Senior 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:
Thanks :)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(n-1,X)); } public static int factorial (int n){ if(n>1)return n*factorial(n-1); else return 1; } }
- 02-02-2012, 01:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Something weird about a math function
Your factorial method overflows for (reasonably) large values of n.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2012, 02:05 PM #3
Senior 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!
- 02-02-2012, 02:14 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2012, 03:32 PM #5
Senior 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!
- 02-02-2012, 04:34 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Something weird about a math function
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2012, 04:37 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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, 04:40 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Checking if a math function is valid
By HardToHandle in forum New To JavaReplies: 3Last Post: 01-18-2012, 01:40 AM -
painting math function graph
By batia in forum Advanced JavaReplies: 5Last Post: 09-17-2011, 08:58 PM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM -
Weird problem upon calling same function twice
By alin_ms in forum New To JavaReplies: 2Last Post: 12-20-2008, 06:14 PM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks