You are on page 1of 20

Report No.

2
1

PHAM XUAN HUY


INTERNATIONAL PHYSICS
MARCH 29, 2013

Overview
2

One main problem in Standard Numerical Methods

that is good for studying algorithm and also discover


the power of computer is Numerical Integration.
Ones can use formulas from Newton-Cotes method
and may develop higher exact formulas if needed.
Ones can also reduce step size h when run program
on computer. This can lead ones find out computer
power.
Actually, without computer help this methods would
become useless because hand calculation is almost
impossible.

Trapezoidal Rule
3

This method approximates the curve under integral sign by a

straight line which goes through two end-points of a small


interval.
The formulas of integral and its error in a step-size long
interval:

Multiple-application trapezoidal rule:

Trapezoidal C Code
4

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double IntFunc(double u)
{return (double)sin(u);}
double SeDeri(double y)
{return (double)abs(sin(y));}
main()
{
int i, j, N[9]= {1000,10000,100000,500000,1000000,5000000,10000000,20000000,50000000};
double a,b,h,SUM, SUM1;
double X, Xmid;
double Integral[9];
double Error[9];
double TrueValue;
//Module Calculates The Integral And Its Error
printf("Input value a: \n"); scanf("%lf",&a);
printf("Input value b: \n"); scanf("%lf",&b);
for(j=0;j<=8;j++)
{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
SUM = IntFunc(a) + IntFunc(b);
SUM1=0;
for(i=1;i<=(N[j]-1);i++)
{
X=a+(double)i*h;
SUM=SUM+2*IntFunc(X);
//Xmid=a+(double)i*(h/2);
SUM1=SUM1 + SeDeri(Xmid);
}
Integral[j]=(h/2)*SUM;
Error[j]=(b-a)*pow(h,2)/12;
}
//Module Prints Out The Results
printf("Estimated Integral\n");
for(j=0;j<=8;j++)
printf("Integral[%d]:
%lf
Error[%d]: %lf\n", j,Integral[j], j, Error[j]);
TrueValue = cos(a) - cos(b);
printf("True value:
%lf\n", TrueValue);
getch();
}

Result 1
5

I sin x dx
1

Times of
Calling Func

Step-size

Estimated
Integral

Error

0.500000

0.450081

0.020833

0.250000

0.457301

0.005208

0.125000

0.459099

0.001302

16

16

0.062500

0.459548

0.000326

32

32

0.031250

0.459660

0.000081

64

64

0.015625

0.459688

0.000020

128

128

0.007813

0.459695

0.000005

256

256

0.003906

0.459697

0.000001

512

512

0.001953

0.459698

0.000000

Result 2
6

Range of integration from 0 to 3141.59


N

Times of
Calling Func

Step-size

Integral

Error

1570.795000

-4.168243

645962460.5

785.397500

-4.168243

161490615.1

392.698750

0.000000

40372653.7

16

16

196.349375

0.000346

10093163.4

32

32

98.174688

-0.000072

2523290.8

64

64

49.087344

-0.000129

630822.70

128

128

24.543672

-0.000142

157705.667

256

256

12.271836

-0.000146

39426.419

512

512

6.135918

-0.000146

9856.6049

Result 2 (cont)
7

Time of
Calling Func

Step-size

Integral

Error

1000

1000

3.141590

0.000000

2583.849843

10000

10000

0.314159

0.000003

25.838498

100000

100000

0.031416

0.000004

0.258385

500000

500000

0.006283

0.000004

0.010335

1000000

1000000

0.003142

0.000004

0.002584

5000000

5000000

0.000628

0.000004

0.000103

10000000

10000000

0.000314

0.000004

0.000026

20000000

20000000

0.000157

0.000004

0.000006

50000000

50000000

0.000063

0.000004

0.000001

Resume No.1
8

As it can be seen error of multiple trapezoidal rule proportional to

square h and range (b-a) and also average value of second order
derivative of function under integral sign.
The tables in three slide above show us the effect of value of step-size
h on the integral result.
In the first of three, because the range is from 0 to 1 then the step-size
h<1 causes small error.
In the second table, range of integration increase 3000 times. If N
keep unchanged then error raises very big. In the third slide when use
large number of iteration N that mean reducing h leads to small error
again.
In the case the integration range is large, its necessary to run the code
with large number N.
Code shows that the function Sin(x) is call N times. As a result, the

code is optimized.

Simpson 1/8 Rule


9

The main difference between Simpson 1/8 rule and

Trapezoidal rule is that Simpson 1/8 approximate function


under integral sign by a parabola which go through two end
points of interval and the intervals mid-point not a straight.
From the approximate approach, the formula of this method
is derived:
The multiple Simpson 1/8 formulas:

Error:

Simpson 1/8 Code


10
for(j=0;j<=8;j++)
{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
OddSum=0;
EvenSum=0;
for(i=1;i<=(N[j]-1);i++)

X=a+(double)i*h;

if((i%2)==1)

OddSum=OddSum + IntFunc(X);

else

EvenSum=EvenSum + IntFunc(X);

}
Integral[j]=(h/3)*( IntFunc(a)+IntFunc(b)+4*OddSum+2*EvenSum );
Error[j]=(b-a)*pow(h,4)/180;

Result 3
11

NN
2
4
8

2
4
8

16
16
32
32
64
64
128
128
256
256
512
512

Times
of
Step-size
calling
Function
0.500000
2
0.250000
4
0.125000
8
0.062500
16
0.031250
32
0.015625
64
0.007813
128
0.003906
256
0.001953
512

Step-size
Error
EstimatedEstimated
Integral Error
Integral
0.459862
0.500000
0.459862
0.459708
0.250000
0.459708
0.459698
0.125000
0.459698
0.459698
0.062500
0.459698
0.459698
0.031250
0.459698
0.459698
0.015625
0.459698
0.459698
0.007813
0.459698
0.459698
0.003906
0.459698
0.459698
0.001953
0.459698

0.000347
0.000347
0.000022
0.000022
0.000001
0.000001
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

Simpson 3/8Rule
12
for(j=0;j<=8;j++)
{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
OddSum=0;
EvenSum=0;
for(i=1;i<=(N[j]-1);i++)

X=a+(double)i*h;

if( (i%3)==0)

EvenSum=EvenSum + IntFunc(X);

else

OddSum=OddSum + IntFunc(X);

}
Integral[j]=(3*h/8)*( IntFunc(a)+IntFunc(b)+3*OddSum+2*EvenSum );
Error[j]=(b-a)*pow(h,4)*3/80;
}

Result 4
13

I sin x dx
1

Times of
Calling Func

Step-size

Estimated
Integral

Error

0.333333

0.459771

0.000069

0.166667

0.459702

0.000004

0.111111

0.459699

0.000001

15

15

0.055556

0.459698

0.000000

33

33

0.030303

0.459698

0.000000

66

66

0.015152

0.459698

0.000000

129

129

0.007752

0.459698

0.000000

255

255

0.003922

0.459698

0.000000

513

513

0.001949

0.459698

0.000000

Boole Rule
14
for(j=0;j<=8;j++)
{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
Sum13=0;
Sum2=0;
Sum04=0;
for(i=1;i<=(N[j]-1);i++)

X=a+(double)i*h;

if( ((i%4)==1) || ((i%4)==3 ))

Sum13=Sum13 + IntFunc(X);

if((i%4)==2)

Sum2=Sum2 + IntFunc(X);

else

Sum04=Sum04 + IntFunc(X);

}
Integral[j]=(2*h/45)*( IntFunc(a)+IntFunc(b)+32*Sum13+7*Sum04+12*Sum04 );
Error[j]=(b-a)*pow(h,6)*41/140;
}

Result 5
15

I sin x dx
1

Step-size

Estimated Integral

Error

0.500000

0.434201

0.004576

0.250000

0.475858

0.000071

0.125000

0.484699

0.000001

16

0.062500

0.489841

0.000000

32

0.031250

0.492588

0.000000

64

0.015625

0.494006

0.000000

128

0.007813

0.494725

0.000000

256

0.003906

0.495088

0.000000

512

0.001953

0.495270

0.000000

Resume 2
16

Actually, using Simpson Rule or Boole Rule give the

result converging faster to the true value and also the


error is smaller in some order of h.

However the same result can also be achieved by using

Trapezoidal Rule and reducing step-size h. This depends


on the power of computer.

Richardson Extrapolation C code


17

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double IntFunc(double u)
{return (double)sin(u);}
double SeDeri(double y)
{return (double)abs(sin(y));}
double TrapeInte(double aa, double bb,int n)
{
int i;
double sum=0, x,hh;
hh=(bb-aa)/n;
if(n==1)
return( (double)(hh/2)*(IntFunc(aa)+(IntFunc(bb))));
if (i>1)
{
for(i=1;i<n;i++)
{
x=aa+i*hh;
sum=sum+IntFunc(x);
}
return ( hh*( (IntFunc(aa)+IntFunc(bb))/2 + sum ) );
}
else exit;
}

Code (cont)
18

main()
{
int i, j, N[9]= {2,4,8,16,32,64,128,256,512};
double a,b,h,SUM1,I1;
double h2,SUM2,I2,I,SUM;
double Inte1[9], Inte2[9], Inte[9];
double Er1[9], Er2[9], Er[9];
double TrueValue;
//Module Calculates The Integral And Its Error
printf("Input value a: \n"); scanf("%lf",&a);
printf("Input value b: \n"); scanf("%lf",&b);
for(j=0;j<=8;j++)
{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
SUM1=0;SUM2=0;SUM=0;
for(i=1;i<=N[j];i++)
{
I1 = TrapeInte(a+(i-1)*h, a+i*h,1);
SUM1= SUM1+I1;
I2 = TrapeInte(a+(i-1)*h, a+i*h,2);
SUM2= SUM2+I2;
I = (4*I2 - I1)/3;
SUM = SUM+I;
}
Inte1[j]=SUM1;
Er1[j]=(b-a)*pow(h,2)/12;
Inte2[j]=SUM2;
Er2[j]=(b-a)*pow(h,2)/48;
Inte[j] =SUM;
Er[j] =
}
//Module Prints Out The Results
printf("Estimated Integral\n");
for(j=0;j<=8;j++)
printf("Inte1[%d]: %lf
Inte2[%d]: %lf\n", j,Inte1[j],j,Inte2[j]);
for(j=0;j<=8;j++)
printf("Inte[%d]: %lf\n",j,Inte[j]);

TrueValue = cos(a) - cos(b);


printf("True value:
getch();
}

%lf\n", TrueValue);

Result
19

Integral
with h

Integral
with h/2

Integral
using R-E

0.500000

0.450081

0.457301

0.459708

0.250000

0.457301

0.459099

0.459698

0.125000

0.459099

0.459548

0.459698

16

0.062500

0.459548

0.459660

0.459698

32

0.031250

0.459660

0.459688

0.459698

64

0.015625

0.459688

0.459695

0.459698

128

0.007813

0.459695

0.459697

0.459698

256

0.003906

0.459697

0.459698

0.459698

512

0.001953

0.459698

0.459698

20
int i, j, N[9]= {2,4,8,16,32,64,128,256,512};
double a,b,h,SUM,SUM1,SUM2;
double X, Xmid, TrueValue;
double Integral[9], Error[9];

//Module Calculates The Integral And Its Error

printf("Input value a: \n"); scanf("%lf",&a);


printf("Input value b: \n"); scanf("%lf",&b);
for(j=0;j<=8;j++)

{
h=(b-a)/N[j];
printf("Value of step size h%d: %lf\n",j,h);
SUM1=0; SUM2=0;
for(i=0;i<=(N[j]-1);i++)

{
X=a+(double)i*h;

Xmid=X+h/2;

SUM1=SUM1+IntFunc(X);

SUM2=SUM2+IntFunc(Xmid);

}
SUM=IntFunc(b)-IntFunc(a)+2*SUM1+4*SUM2;
Integral[j]=(h/6)*SUM;
Error[j]=(b-a)*pow(h,4)/180;
}

You might also like