# Computing Pi

Numerical integration is a method of computing an approximation of the area under the curve of a function, especially when the exact integral cannot be solved. For example, the value of the constant $$\pi$$ can be defined by the following integral. However, rather than solve this integral exactly, we can approximate the solution by use of numerical integration:

$$\pi = \int_{0}^{1}\frac{4}{1+x^2}\mathit{dx}$$

The following C code is an implementation of the numerical integration midpoint rectangle rule to solve the integral just shown. To compute an approximation of the area under the curve, we must compute the area of some number of rectangles (num_rects) by finding the midpoint (mid) of each rectangle and computing the height of that rectangle (height), which is simply the function value at that midpoint. We add together the heights of all the rectangles (sum) and, once computed, we multiply the sum of the heights by the width of the rectangles (width) to determine the desired approximation of the total area (area) and the value of $$\pi$$.

int main(void) {
long num_rects = 100000;
double mid, height, width, area;
double sum = 0.0;

width = 1.0 / (double) num_rects;
for (long i = 0; i < num_rects; i++) {
mid = (i + 0.5) * width;
height = 4.0 / (1.0 + mid * mid);
sum += height;
}
area = width * sum;
printf("Computed pi = %f\n", area);
return 0;
}


Reference: [BRESHEARS] pp. 31 & 32.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8310145,"math_prob":0.9995228,"size":1416,"snap":"2019-13-2019-22","text_gpt3_token_len":369,"char_repetition_ratio":0.14518413,"word_repetition_ratio":0.024193548,"special_character_ratio":0.2881356,"punctuation_ratio":0.13970588,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999291,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-24T07:23:53Z\",\"WARC-Record-ID\":\"<urn:uuid:1d313496-a15d-4d29-88c2-654b6d7ae20e>\",\"Content-Length\":\"8036\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:69fd0b04-3b3b-4b7c-b134-868ef2397945>\",\"WARC-Concurrent-To\":\"<urn:uuid:73caabd5-db4c-49ab-a162-6fbfa4437a78>\",\"WARC-IP-Address\":\"34.212.143.74\",\"WARC-Target-URI\":\"http://34.212.143.74/apps/s201911/tc3039/notes_computing_pi/\",\"WARC-Payload-Digest\":\"sha1:RC6RKFPF7YDWEHOZK4SYHLMYODQZDLEN\",\"WARC-Block-Digest\":\"sha1:LT6ZSLKXEBUYGQJESDAOJYPTLEIROLOH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232257553.63_warc_CC-MAIN-20190524064354-20190524090354-00438.warc.gz\"}"}