Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0

    Program for finding e^x and sinh using power series


    Hello Dev Shed members. I need some help. While giving me advice keep in mind I'm a student and only have about 4 months of C Language experience. Now on to the question.

    I want to write a program using the power series: 1 + x + x^2/2! + .....

    Σ x^n/n!
    n=0

    So using the series above i must derive e^x. Adding terms from the series up to the first term that is less than 10^-6 in absolute value.

    then using e^x i must find sinh(hyperbolic sine)
    sinh(x)= (e^x-e^(-x)/2)

    the program should print a table of values for the function
    ,sinh, for x = −1 to 1 in increments of 0.1.

    So I got my code to print the x values but no luck on the hyperbolic sin.

    Here is my code

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define ACCURACY 0.000001 
    
    long factorial (int);
    double exponential (double);
    double sinh (double);
    
    int main()
    {
        double x;
        int result;
        
        for (x=-1.0; x<=1.0; x=x+0.1)
             
            printf("X is: %lf\n", x);
            
            result =  sinh (x);
            
            printf("Hyperbolic sine = %lf\n", result);
            
            
            system("pause");
            return 0;
    }
    
    double exponent(double x)
    	{
    	   
    	   int n;
    	   double term;
    	   
    	   
    	   n=0;
    	   while (1) 
    	   {
            term= pow(x, n) / factorial(n);
            if( term < ACCURACY && term> -ACCURACY )
    	          break;
    	          n++;
    	   }
           
    	   return term;
    	}
    	
    	
    	 
    double sinh (double x)
    	{
        
        double hyperbolicsin;
        
    	         
        hyperbolicsin= (exponent(x)-exponent(-x))/2;
         
        return hyperbolicsin;
    	    
    	}
    	
    
    long factorial (int n)
    	{
    	    long factorial=1;
    	    
    	    while(n > 1)
    	    {
    	            factorial*= n;
    	            n--;
    	    }
    	     
    	    return factorial;
    	}
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > sinh(x)= (e^x-e^(-x)/2)
    Your code has mis-placed (), if this is correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by salem
    > sinh(x)= (e^x-e^(-x)/2)
    Your code has mis-placed (), if this is correct.
    Sorry I don't understand what you mean by it has misplaced ().
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    hyperbolic sine is
    sinh(x)= (e^x-e^(-x))/2
    not
    sinh(x)= (e^x-e^(-x)/2)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    hyperbolic sine is
    sinh(x)= (e^x-e^(-x))/2
    not
    sinh(x)= (e^x-e^(-x)/2)
    Ok i've corrected that but I still don't get the right values for sinh.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by vilodic
    Ok i've corrected that but I still don't get the right values for sinh.
    How are you accumulating the terms in exponent?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by bullet
    How are you accumulating the terms in exponent?
    Code:
    double exponent(double x)
    	{
    	   
    	   int n;
    	   double term;
    	   double sum=0;
     
    	    n = 0;
    	   while (1) 
    	   {
            term= pow(x, n) / factorial(n);
            sum+= term;
            if( term < ACCURACY  && term> -ACCURACY )
    	          break;
    	          n++;
    	   }
           
    	   return sum;
    	}
    Is this what you mean?
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Yes that's what I mean.

    The original code didn't have it.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by bullet
    Yes that's what I mean.

    The original code didn't have it.
    I'm not sure what I'm doing wrong. I'm able to print values from -1 to 1 (for x) and I don't think there is anything wrong caculating e^x but when finding sinh i don't get the correct numbers.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Since you still haven't told us what those "wrong values" are, we can only speculate.

    But I guess result shouldn't be an int.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by vilodic
    I'm not sure what I'm doing wrong. I'm able to print values from -1 to 1 (for x) and I don't think there is anything wrong caculating e^x but when finding sinh i don't get the correct numbers.
    Can you post the full code now?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by bullet
    Can you post the full code now?
    I did? Are you guy not able to see it?
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    So what about the int result bug I pointed out? Have you fixed it? Are you still getting the wrong result? What wrong result do you get?

    I mean, if you like the discussion, we can go on for some more pages. But I suggest we get to the point and fix the problem.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    So what about the int result bug I pointed out? Have you fixed it? Are you still getting the wrong result? What wrong result do you get?

    I mean, if you like the discussion, we can go on for some more pages. But I suggest we get to the point and fix the problem.
    Ok. I'm re-posting the code. I've made changes such changing the int result.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define ACCURACY 0.000001 
    
    long factorial (int);
    double exponent (double);
    double sinh (double);
    
    int main()
    {
        double x;
        double result;
        double sum = 0;
        
        for (x=-1.0; x<=1.0; x=x+0.1)
           {  
            printf("X is: %.1lf\n", (x));
            result =  sinh (x);
            }
            sum= exponent(x);
            printf("e^x is %lf\n",sum);
            
            
           
            
            system("pause");
            return 0;
    }
    
    double exponent(double x)
    	{
    	   
    	   int n;
    	   double term;
    	   double sum=0;
     
    	    n = 0;
    	   while (1) 
    	   {
            term= pow(x, n) / factorial(n);
            sum+= term;
            if( term < ACCURACY  && term> -ACCURACY )
    	          break;
    	          n++;
    	   }
           
    	   return sum;
    	}
    	
    	
    	 
    double sinh (double x)
    	{
        
        double hyperbolicsin;
        
    	if ( x>=-1 && x<=1) 
        {        
        hyperbolicsin= (exponent(x)-exponent(-x)/2);
        
        printf("sinh is %lf\n",hyperbolicsin); 
        }
        
    	    
    	}
    	
    
    long factorial (int n)
    	{
    	   int factres= 1;
    	   while (n > 1)
    	   {
                 factres = factres*n;
                 n--;
            }
            return (factres);
    	}
    Ok my program is giving a value of 3.004.. to e^x/exponent function. I want it to give me 2.71.
  28. #15
  29. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by vilodic
    Ok my program is giving a value of 3.004..
    And that's correct for x = 1.1. You're doing the calculation of e^x after the for loop, at which point x has just exceeded 1.0. I guess you meant the calculation to be inside of the loop.

    This is clearly a problem of your completely messed up formatting. You've stumbled upon your sloppy indentation.

    Correct formatting is crucial for correct code. It's not an optional prettifying step you may or may not do at the end. If you mess up the the layout, you'll almost always mess up the content as well, because you simply won't understand your own code.

    So I strongly suggest you grab a proper editor or IDE, choose your favorite indent style, choose an indent depth and then stick to that. If you accidentally break the formatting in your code, you fix it just like you fix a bug -- because it actually is a bug.

    I know that things like formatting or variable names don't seem to be important when you've just started to program ("The compiler doesn't care, so why should I?"). But you'll find out that those seemingly trivial things have a huge impact on the quality and correctness of your code. The compiler doesn't care about readbility. But your brain does, and that's where the code actually gets produced.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo