How do i integrate an equation in C?

This is a discussion on How do i integrate an equation in C? within the C Programming forums, part of the General Programming Boards category; I need to write a program in C to integrate the following simple equation: dy/y= x dx and I have ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Unhappy How do i integrate an equation in C?

    I need to write a program in C to integrate the following simple equation:

    dy/y= x dx

    and I have no clue whatsoever on how to integrate it...

    Could somebody please show me the code or technique used to integrate equations in C?
    Last edited by adinclik; 05-28-2006 at 08:12 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    citizen says:
    does this math thing have like, any meaning? I wish I knew what he was doing though?
    Leon says:
    i have no idea
    I asked a friend who was more experienced at math than me, and I still have no idea what you're doing. I might be able to help more if I knew what exactly to read from that equation, because it could mean ...anything. From calculus to algebra.

    Math in general is easy to implement, all you have to do is type out the equation most of the time. Here's a function that converts a Fahrenheit tempurature to Centigrade, for example:
    Code:
    float convert_temp(float f) {
          return (f - 32) / 1.8;
    }

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    3
    Integration can only mean calculus. What you've proposed however seems like it would be fairly involved. For a definite integral you could go back to the basics of left / right hand sums with enough precision to get fairly close. But actually working with the equation like you suggest.... Considering software that does this (Maple) can run $500+ I'm going to guess it's fairly complicated, especially the type of equation(differential equation) you are asking for, I know a TI-89 can't do seperation of variables, but it can do normal integration. My advice would be to seek out an open source project that has accomplished this and study it, because I'm thinking it's going to be beyond the scope of a one or 2 paragraph explanation. I could be wrong though, feel free to correct me if someone knows better.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I'm kind of unsure myself at the moment as of what you're trying to accomplish. If you just don't need the indefinite integral, it's easy to see that it's y = e^(x^2/2), but I'm aussuming you have bounds, in which case you may want to look into Riemann sums...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I haven't done the maths behind integration, but I was given this task to do to find the area of a parabola between itself and the x-axis - using random numbers. It's quite accurate, but I don't know if it's what you were looking for.

    You'll have to analyse it to see how it works, but if you know the maths (which I don't), it shouldn't be too difficult. The main idea is that the program tracks how many randomly generated numbers fall under the parabola, then uses the known area of a rectangle to calculate the answer.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
        float x, y, ans;
        float rand_x, rand_y;
        int i, tally = 0, throws = 100000;
        
        srand( time( NULL ));
        
        for ( i = 0; i < throws; i++ )
        {
            rand_x = ( ( rand() % 2001 ) / 1000.0) - 1.0;
            rand_y = ( ( rand() % 1001 ) / 1000.0);
            
            y = -( rand_x * rand_x ) + 1;        //this is the equation of the line, -(x^2) + 1 in this case
            
            if ( rand_y <= y )
            {
                 tally++;
            }
            
        }
        
        printf("%d\n", tally);
        
        ans = ( (float) tally / (float) throws ) * 2.0;
        
        printf("ans = %.3f\n", ans);
        
        
        system("PAUSE");	
        return 0;
    }
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by RunDosRun
    Integration can only mean calculus. What you've proposed however seems like it would be fairly involved. For a definite integral you could go back to the basics of left / right hand sums with enough precision to get fairly close. But actually working with the equation like you suggest.... Considering software that does this (Maple) can run $500+ I'm going to guess it's fairly complicated, especially the type of equation(differential equation) you are asking for, I know a TI-89 can't do seperation of variables, but it can do normal integration. My advice would be to seek out an open source project that has accomplished this and study it, because I'm thinking it's going to be beyond the scope of a one or 2 paragraph explanation. I could be wrong though, feel free to correct me if someone knows better.
    OK. the equation I proposed was a put-together of two seperate functions. What I have to do is integrate the left side and the right side seperately... then make them equal to each other.

    Analytically, I get :

    ln (y) = (x^2)/2 + C as a result of $ dy/y = $ x dx

    (by the way $ is used for the integral sign)

    so when i solve for initial condition (y=1 when x=0), C=0.

    All I have to do is know how to integrate (dy/y) and (x dx) in C seperately in the intervals [0,1].

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    The general way (from what I know), to integrate defined integrals with computers is to use Riemann sums :

    http://mathworld.wolfram.com/RiemannSum.html

    Any help ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    I need to write a program in C to integrate the following simple equation:

    dy/y= x dx

    and I have no clue whatsoever on how to integrate it...
    You need a constraint (like y(1) = 2) if you want to solve the problem numerically. (If you want an analytic solution--psst, it's a one-liner!--use Mathematica, Maple, etc.)


    You could then take a Riemann sum, or use Euler's method, or try something fancier like a Runge-Kutta.

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208
    There are lots of ways to do definate integrals actually. Though it can get complicated quite quickly you can get very accurate results using many previous studied algorithms. I will list some that I know of in order of how complicated they are mathematically.

    Reiman Sums should work for this and is easy to implement
    Euler methods (there are many branches to this method) will obtain you far greater results
    Verlet Algorithms - these are what I use in my work though I'm not certain how well they pertain to non motion problems.

    But in general, there is no C standard library function which integrates, and if you want to do indefinate integration your basically going to have to write a function that follows the logic a human would when solving it. It would be rather tedious to write a program which covers the entire scope of integration techniques but if your sure all the integration your going to need is just basic polynomials, or all rational functions, etc, then I am sure you could easily implement a function which exploits the power rule, partial fractions, etc.

    If you need more information feel free to P.M. and I will gladly help you implement this (I am a computational physist and thus programming computers to do math is what I get paid for ) so if none of these posts helped you feel free to get in touch with me.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  10. #10
    Registered User
    Join Date
    May 2006
    Location
    Da World
    Posts
    6
    if you wanna intergrate a polynomial then do the following:

    store f(x) in a linked list..with the index of each term..and the coefficient of x
    Code:
    struct poly
    {
         int val;   // to store the index of each term of f(x)
         float coeff;  // to store the coefficient of x in each term
         struct poly *next;
    };
    then as you traverse the linked list just go on increasing the index by 1 and correspondingly divide the term by the index.
    Code:
    if f(x) is a*x^3+b*x^2 then following above procedure will give 
    (a/4)*x^4+(b/3)*x^3
    hope this helps
    Last edited by g33k; 06-01-2006 at 07:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. differential equation??
    By kypronite in forum Game Programming
    Replies: 11
    Last Post: 09-03-2008, 05:25 PM
  2. Help needed! Finding Root of an Equation?
    By reader in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 10:10 AM
  3. Replies: 15
    Last Post: 11-04-2006, 03:06 AM
  4. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 08:21 PM

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