# math function vs functions...

• 02-18-2008
niceguy
math function vs functions...
I have the following problem... i am trying to print out a table of values for the taylor expansion of exp() (as a function) and the plain value of exp().

(i) cant get function to work?

(ii) how might I combine their values on a table?

Many thanks!

include
Code:

``` #include <stdio.h> #include <stdlib.h> #include <math.h>         double taylor(double x, int n)     {         int i;         double sum = 1.0;         double term= 1.0;         for (i=1; i<=n; i++)         {         term = (term*x)/i;         sum=sum+term;         }                          return sum;                     }```
• 02-18-2008
tabstop
You'll have to say what you mean by doesn't work. That function looks fine to me.

Your main program should call that function for various values of x and/or n to generate the table.
• 02-18-2008
robatino
You could write
Code:

```term *= x/i; sum += term;```
instead. Also, the posted code doesn't need any of the 3 include files.
• 02-18-2008
Tommo
Your taylor function doesn't look right to me. Maybe you need to refresh your memory.

Here's my quickly cooked up solution:
Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> long factorial( long n ) {         if( n == 1 )                 return 1;         else                 return n*factorial(n-1); } double exp_maclaurin(double x, int n) {         double sum = 1.0;         int i;         for(i=1;i<=n;i++)         {                 sum += pow(x,i)/factorial(i);         }         return sum; } int main() {         printf("Our function: exp(5) = %lf\n", exp_maclaurin(5,10));         printf("Built-in exp function: exp(5) = %lf\n", exp(5));         return 0; }```
Hope this helps.
• 02-18-2008
vart
Quote:

Originally Posted by Tommo

Here's my quickly cooked up solution:

Your quickly cooked solution does the same as the original code but in a slow manner
• 02-18-2008
Tommo
Yes, my apologies. At first glance his taylor function looked incorrect. Disregard my previous post.
• 02-18-2008
iMalc
Quote:

Originally Posted by niceguy
(i) cant get function to work?

(ii) how might I combine their values on a table?

(i) Wow really - Why not? The function itself looks fine, though you need to learn how to indent code.