Results 1 to 8 of 8
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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(n-1,X));
    	}
    	
    	public static int factorial (int n){
    		
    		if(n>1)return n*factorial(n-1);	
    		else return 1;
    		
    	}
    	
    }
    Thanks :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Something weird about a math function

    Your factorial method overflows for (reasonably) large values of n.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Something weird about a math function

    Quote Originally Posted by Mapisto View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Something weird about a math function

    I really needed this calculous refreshment :|
    I get it now :)

    Thanks man!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Something weird about a math function

    Quote Originally Posted by Mapisto View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Something weird about a math function

    Quote Originally Posted by Tolls View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Checking if a math function is valid
    By HardToHandle in forum New To Java
    Replies: 3
    Last Post: 01-18-2012, 01:40 AM
  2. painting math function graph
    By batia in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2011, 08:58 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  4. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 06:14 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •