Thread: How to code these functions?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    How to code these functions?

    Hi, all,
    Now I am coding for a statistic project. I have got some chi square and need to figure out the p value.
    The formulas are like these:
    T(z) = the integral from zero to infinity of (t^(z-1))*e^(-t)dt
    f(x)=(1/(T(1/2)*2^(1/2)))*x^(-1/2)*e^(-x/2)
    p=the integral from a(a constant) to infinity of f(x)dx

    Does anybody have any idea of how to code these? Any advice is highly appreciated!
    Cicci

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Put a semicolon after what you've just written and replace the '^'s with pow () functions and you've pretty much got the guts of the function.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thank you! samGwilliam!
    What about the integral? what about the infinity?

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by Cicci View Post
    Thank you! samGwilliam!
    What about the integral? what about the infinity?
    Not a clue, mate.

    But the mathematics you wrote will translate to C with minimal hassle.

    E.g. f(x)=(1/(T(1/2)*2^(1/2)))*x^(-1/2)*e^(-x/2)

    Code:
    float f (float x, float t)
    {
       return 1 / (t (1 / 2) * pow (2, (1 / 2)))) * pow (x, (-1 / 2)) * pow (e, (-x / 2));
    }
    Or something.
    Last edited by samGwilliam; 07-10-2007 at 12:49 PM.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by Cicci View Post
    Thank you! samGwilliam!
    What about the integral? what about the infinity?
    Integral: use calculus (no the computer doesn't know calculus unless you teach it)
    Infinity: INFINITE
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thank you!

    My goal is the "p=the integral from a(a constant) to infinity of f(x)dx"

    I just don't know how to code a function including integral.

    Do you have any idea of this part?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For e^x use exp(x).

    Edit: And for the square root of 2 or x, use sqrt(2) or sqrt(x) resp., and for x^(-1/2), use 1./sqrt(x). Generally, the more specialized functions are more efficient (certainly no less efficient, since they could be implemented in terms of the less specialized ones).
    Last edited by robatino; 07-10-2007 at 04:57 PM.

  8. #8
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by robatino View Post
    For e^x use exp(x).
    Ah, yes.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Maxima shows that T(1/2) is equal to sqrt(pi) (the following isn't formatted properly since spaces get screwed up when copied and pasted). I believe this is common knowledge but it's been a long time since I did basic calculus.

    Maxima 5.12.0 http://maxima.sourceforge.net
    Using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (aka GCL)
    Distributed under the GNU Public License. See the file COPYING.
    Dedicated to the memory of William Schelter.
    This is a development version of Maxima. The function bug_report()
    provides bug reporting information.
    (%i1) T(z) := integrate(t^(z-1)*%e^(-t), t, 0, inf);
    z - 1 - t
    (%o1) T(z) := integrate(t %e , t, 0, inf)
    (%i2) T(1/2);
    (%o2) sqrt(%pi)
    (%i3)

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    How to teach the computer use calculus?

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thank you! robatino!

    Then I have got the T(1/2)=pi^(1/2), how about the "p" part?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    >How to teach the computer use calculus?

    You either call a symbolic algebra library from C, or code a numerical integration routine directly. I'm not familiar with the details.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Cicci View Post
    Thank you! robatino!

    Then I have got the T(1/2)=pi^(1/2), how about the "p" part?

    Maxima 5.12.0 http://maxima.sourceforge.net
    Using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (aka GCL)
    Distributed under the GNU Public License. See the file COPYING.
    Dedicated to the memory of William Schelter.
    This is a development version of Maxima. The function bug_report()
    provides bug reporting information.
    (%i1) f(x) := x^(-1/2)*%e^(-x/2);
    - 1 - x
    --- ---
    2 2
    (%o1) f(x) := x %e
    (%i2) integrate(f(x), x);
    sqrt(x)
    (%o2) sqrt(2) sqrt(%pi) erf(-------)
    sqrt(2)
    (%i3)

    The formatting is screwed up, but Maxima knows the indefinite integral, so you can install and run it for yourself. Then you just plug in the limits to get the definite integral.

    Edit: BTW, if you're using certain Linux distributions such as Fedora, Maxima may be in one of your default repositories (for example, in Fedora, "yum install maxima maxima-gui" works).
    Last edited by robatino; 07-10-2007 at 01:37 PM.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Cicci View Post
    What about the integral?
    Use an approximate meathod, like Simpson's Rule. There is no built in C function for this, so you'll have to do it yourself.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For this particular problem, it reduces to evaluating the error function erf(x).

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

    Edit: Some implementations provide the function erf(), though it's not in standard C/C++.
    Last edited by robatino; 07-10-2007 at 01:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-29-2007, 05:59 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM