# How to compute the upper incomplete gamma function in MATLAB?

I need to compute the upper incomplete gamma function $$\Gamma(0,i\pi K)$$ where $i=\sqrt{-1}$ and K is real.

I have some basic idea about what this "special function" is, but am not used to using it. I am confused because MATLAB refuses to compute this for me - I get an error message says that inputs must be real.

The CAS wxMaxima computes it no problem with or without complex or inputs. How can I implement this in MATLAB?

In MATLAB the command I am using is `1 - gammainc(a,z)`

In wxMaxima the command is `gamma_incomplete(a,z)` or `gamma_incomplete_regularized(a,z)`.

EDIT: What I mean to say here is that, in MATLAB, I want to get the same result that gamma_incomplete(a,z) gives in wxMaxima. For example, in wxMaxima the command `gamma_incomplete(0,%i*%pi);` gives the output `0.28114072518757*%i-0.073667912046426` Now what do I have to do in MATLAB to get the same result?

-

## 1 Answer

Your inputs are the wrong way round. Additionally, you should specify that you want the upper tail of the gamma function, rather than subtracting from 1. This gives more accurate values in the case where the real part of $\Gamma(x,a)$ is near to 1.

On Wolfram Alpha you have

$$\Gamma(1,i) = 0.5403 - 0.8415i$$

and in Matlab

````>> gammainc(1i,1,'upper')
ans = 
    0.5403 - 0.8415i
````

which matches up.

-

This is what I get when I enter pretty much the exact same thing. >> gammainc(i*pi,1,'upper')         ??? Error using ==> gammainc         Inputs must be real, full, and double or single. – ben Oct 11 '12 at 15:15
On my machine that returns `-1.0000 + 0.0000i`. What version of Matlab do you have? Have you checked that you don't have a variable called `i` in your environment? It's always good practice to declare complex numbers with the `1+2i` format, rather than using `i` on its own. It prevents errors where you accidentally overwrite `i` with some other value (e.g. if you use it in a loop counter). In this example you could write `1i*pi` instead of `i*pi`. – Chris Taylor Oct 11 '12 at 15:17
Version 7.10 R2010a.  When I enter "i" in the command line I get the following, so I think the program recognizes it as such: >> i ans =                   0 + 1.000000000000000i – ben Oct 11 '12 at 15:20
If you type `edit gammainc` and look at the last line of the header comment to get the revision number, what do you see? For example, I see `%   $Revision: 5.17.4.3 $  $Date: 2004/07/05 17:02:00 $`. – Chris Taylor Oct 11 '12 at 15:22
I get: %   $Revision: 5.17.4.5$  $Date: 2008/10/31 06:20:55$ – ben Oct 11 '12 at 15:23
show 5 more comments
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 4, "mathjax_display_tex": 2, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8528309464454651, "perplexity_flag": "middle"}