Computational Science I - Problem Set 2 | CAAM 420, Assignments of Mathematics

Material Type: Assignment; Class: COMPUTATIONAL SCIENCE I; Subject: Comp. & Applied Mathematics; University: Rice University; Term: Fall 2007;

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-ild
koofers-user-ild 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CAAM420 Homework 2 Fall 2007
Xin Wang
October 17, 2007
1 Introduction
In this homework we were asked to write different C programs to evaluate SN, where
SN=
N
X
n=1
n4.(1.1)
There are two usual methods to accomplish this job. One is to directly compute the sum using
a ’for’ loop, the other one is to find the formula for SN. The limit of ’int’ type in C language
prevents us to calculate out the right answer when nis large enough. From this experience,
we obtained some techniques when handling large numeber using ’int’ type.
In the part 2, we were required to write C program to implement the algorithm in Home-
work 1 for computing π.
2 Method
The formula for SNas a 5th order polynomial of N can be assumed as,
SN=a0+a1N+a2N2+a3N3+a4N4+a5N5(2.1)
where the coefficients (a0,· · · , a5) can be derived from the following linear equations,
1 1 1 1 1 1
1222232425
1332333435
1442434445
1552535455
1662636465
a0
a1
a2
a3
a4
a5
=
S1
S2
S3
S4
S5
S6
(2.2)
Then we can obtain this formula,
SN=1
5N5+1
2N4+1
3N31
30N. (2.3)
The Rabinowitz and Wagons spigot algorithm, which is based on the following expansion,
was used to compute the digits of π,
π=
X
i=0
(i!)22i+1
(2i+ 1)!.(2.4)
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Computational Science I - Problem Set 2 | CAAM 420 and more Assignments Mathematics in PDF only on Docsity!

CAAM420 Homework 2 Fall 2007

Xin Wang

October 17, 2007

1 Introduction

In this homework we were asked to write different C programs to evaluate SN , where

SN =

∑^ N

n=

n

4

. (1.1)

There are two usual methods to accomplish this job. One is to directly compute the sum using

a ’for’ loop, the other one is to find the formula for SN. The limit of ’int’ type in C language

prevents us to calculate out the right answer when n is large enough. From this experience,

we obtained some techniques when handling large numeber using ’int’ type.

In the part 2, we were required to write C program to implement the algorithm in Home-

work 1 for computing π.

2 Method

The formula for SN as a 5

th order polynomial of N can be assumed as,

SN = a 0 + a 1 N + a 2 N

2

  • a 3 N

3

  • a 4 N

4

  • a 5 N

5 (2.1)

where the coefficients (a 0 , · · · , a 5 ) can be derived from the following linear equations,

        1 1 1 1 1 1

1 2 2

2 2

3 2

4 2

5

2 3

3 3

4 3

5

2 4

3 4

4 4

5

2 5

3 5

4 5

5

2 6

3 6

4 6

5

a 0

a 1

a 2

a 3

a 4

a 5

S 1

S 2

S 3

S 4

S 5

S 6

Then we can obtain this formula,

SN =

N

5

N

4

N

3 −

N. (2.3)

The Rabinowitz and Wagons spigot algorithm, which is based on the following expansion,

was used to compute the digits of π,

π =

∞ ∑

i=

(i!)

2 2

i+

(2i + 1)!

3 Computation Results

3.1 the Results for Computing SN

We have done the calculations of SN for N = { 3 , 4 , 50 , 51 , 100 , 200 }. The C version program

summation.c calculates the values using both integer precision and double precision. In the

the Matlab version program summation.m there is no distinction between double and integer

precision [1].

The results are given in the following table:

SN sum in C

(int)

formula in

C (int)

sum in C (dou-

ble)

formula in C

(double)

sum in Mat-

lab

formula in

Matlab

S 3 98 98 98.00 98.00 98 98

S 4 354 354 354.00 354.00 354 354

S 50 65666665 65666665 6566665.00 65666665.00 65666665 65666665

S 51 72431866 72431866 72431866.00 72431866.00 72431866 72431866

S 100 2050333330 -379163399 2050333330.00 2050333330.00 2050333330 2050333330

S 200 378157220 373169931 64802666660.00 64802666660.00 64802666660 64802666660

Table 1: the computed results for SN with different methods. In this table ’sum’ stand for

the direct computing method and ’formula’ for calculations with (2.3) [1]

3.2 the Results for the Part 2

The following table lists the value of each required digit,

digit 119 1334 19998 999990

value 6 0 5 1

Table 2: the computed digits of π [1]

The following tabel lists the time for computing the given digits using Matlab and C

language,

Digit of π Time needed in MATLAB Time needed in C

100 0.05s 0.00s

1000 2.99s 0.56s

10000 189.20s 26.66s

1000000 >> 72h 72h27m41.00s

Table 3: the time consuming in Matlab and C language [1]

for (n=1;n<=N; ++n)

sum_1 += nnn*n;

sum_2 = -1.0/30N+0.2NNNNN+0.5NNNN+1.0/3NN*N;

printf("Values calculated in integer precision\n");

printf("sum_1 = %d\nsum_2 = %d\n", sum_1, sum_2);

}

/* This function does the same as do_int but with double precision */

void do_double(int N)

{

double sum_1;

double sum_2;

int n;

sum_1=0.0;

for (n=1;n<=N; ++n)

sum_1 += nnn*n;

sum_2 = -1.0/30N+0.2NNNNN+0.5NNNN+1.0/3NN*N;

printf("Values calculated in double precision\n");

printf("sum_1 = %f\nsum_2 = %f\n", sum_1, sum_2);

}

main(int argc, char *argv[]) {

int N;

/* Get value from command line */

N= (int) atoi(argv[1]);

/* Do calculations and print output */

printf("N=%d\n",N);

printf("-------\n");

do_int(N);

printf("-------\n");

do_double(N);

}

summation.m

function summation(N)

sum_1 = 0;

for i=[1:N]

sum_1=sum_1+i^4;

end

sum_2 = N^5/5+N^4/2+N^3/3-N/30;

sum_

sum_

Code of calc pi.c

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

/* Declare functions */

void init(int*, int);

void print_array(int*, int);

void fix(int *,int);

int proc_digit(int*, int);

void find_prime(int*, int, int);

/* Intialize a vector to 2 in all entries */

void init(int* A, int N)

{

int i;

for(i=0; i<N; i=i+1)

A[i]=2;

}

/* Optional routine for printing an entire array */

void print_array(int *B,int N)

{

int i;

for(i=0;i<N;++i)

printf("%d\n", B[i]);

printf("----------\n");

}

/* Fix all elements that are on value 10 */

void fix(int *B, int n)

{

int i;

for(i=n-1;i>=0;--i) {

if(B[i]==10) {

B[i]=0;

B[i-1]+=1;

}

break;

}

}

}

main(int argc, char *argv[]) {

int *A; int *D;

int j,n,N,p;

clock_t c1, c2;

if (argc != 3) {

printf("Wrong number of arguments!\n");

printf("Please use: ./summation N flag\n");

exit(1);

}

n = (int) atoi(argv[1]);

p = (int) atoi(argv[2]);

N = (int) floor(10*(n+0.0)/(3.0));

A = calloc(N, sizeof(int));

D = calloc(n, sizeof(int));

/* Initialize matrix A */

init(A,N);

c1=clock();

/* Calculate the digits one by one */

for(j=0;j<n;++j)

D[j] = proc_digit(A,N);

/* Correct the erroneous digits with value 10 */

fix(D,n);

/* Optional output */

/print_array(D,n);/

/* Time and print output */

printf("------------\n");

printf("The digit value is: %d\n", D[j-1]);

c2=clock();

printf("It took: %f seconds\n", (double) (c2-c1)/CLOCKS_PER_SEC);

printf("------------\n");

/* Find the prime number with p digits */

if(p>0) {

c1=clock();

find_prime(D, n, p);

c2=clock();

printf("It took: %f seconds\n", (double)(c2-c1)/CLOCKS_PER_SEC);

}

printf("------------\n");

free(D); free(A);

}

References

[1] Eelco Nederkoor. Caam 420: Computational science 1 Problem Set 2.