# Thread: How to code these functions?

1. ## 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. 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. Thank you! samGwilliam!

4. Originally Posted by Cicci
Thank you! samGwilliam!
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.

5. Originally Posted by Cicci
Thank you! samGwilliam!
Integral: use calculus (no the computer doesn't know calculus unless you teach it)
Infinity: INFINITE

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. 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).

8. Originally Posted by robatino
For e^x use exp(x).
Ah, yes.

9. 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)
Dedicated to the memory of William Schelter.
This is a development version of Maxima. The function bug_report()
provides bug reporting information.
(&#37;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. How to teach the computer use calculus?

11. Thank you! robatino!

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

12. >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. Originally Posted by Cicci
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)
Dedicated to the memory of William Schelter.
This is a development version of Maxima. The function bug_report()
provides bug reporting information.
(&#37;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).

14. Originally Posted by Cicci