Thread: math function vs functions...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    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;
            
            
         }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You could write
    Code:
    term *= x/i;
    sum += term;
    instead. Also, the posted code doesn't need any of the 3 include files.
    Last edited by robatino; 02-18-2008 at 01:56 PM.

  4. #4
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Tommo View Post


    Here's my quickly cooked up solution:
    Your quickly cooked solution does the same as the original code but in a slow manner
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Yes, my apologies. At first glance his taylor function looked incorrect. Disregard my previous post.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by niceguy View Post
    (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.
    We can't help you if you don't show us the problem.

    (ii) This could mean a zillion different things. Explain yourself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM