math function vs functions...

This is a discussion on math function vs functions... within the C Programming forums, part of the General Programming Boards category; I have the following problem... i am trying to print out a table of values for the taylor expansion of ...

  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,185
    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 12: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,301
    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, 12:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04: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, 08:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21