November 7th, 2013, 08:51 PM

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^xe^(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;
}
November 8th, 2013, 01:29 AM

> sinh(x)= (e^xe^(x)/2)
Your code has misplaced (), if this is correct.
November 8th, 2013, 03:19 AM

Originally Posted by salem
> sinh(x)= (e^xe^(x)/2)
Your code has misplaced (), if this is correct.
Sorry I don't understand what you mean by it has misplaced ().
November 8th, 2013, 10:06 AM

hyperbolic sine is
sinh(x)= (e^xe^(x))/2
not
sinh(x)= (e^xe^(x)/2)
[code]
Code tags[/code] are essential for python code and Makefiles!
November 8th, 2013, 11:01 AM

Originally Posted by b49P23TIvg
hyperbolic sine is
sinh(x)= (e^xe^(x))/2
not
sinh(x)= (e^xe^(x)/2)
Ok i've corrected that but I still don't get the right values for sinh.
November 8th, 2013, 11:05 AM

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?
November 8th, 2013, 11:54 AM

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?
November 8th, 2013, 12:19 PM

Yes that's what I mean.
The original code didn't have it.
November 8th, 2013, 12:46 PM

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.
November 8th, 2013, 12:57 PM

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.
November 8th, 2013, 12:59 PM

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?
November 8th, 2013, 05:53 PM

Originally Posted by bullet
Can you post the full code now?
I did? Are you guy not able to see it?
November 8th, 2013, 06:20 PM

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.
November 8th, 2013, 08:11 PM

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 reposting 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.
November 9th, 2013, 01:13 AM

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.