Numerical method
-
Upload
huy-pham-xuan -
Category
Documents
-
view
13 -
download
1
description
Transcript of Numerical method
PHAM XUAN HUYINTERNATIONAL PHYSICSMARCH 29, 2013
1
Report No.2
Overview2
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 Rule3
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 Code4
#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 15
N Times of Calling Func
Step-size Estimated Integral
Error
2 2 0.500000 0.450081 0.0208334 4 0.250000 0.457301 0.0052088 8 0.125000 0.459099 0.00130216 16 0.062500 0.459548 0.00032632 32 0.031250 0.459660 0.00008164 64 0.015625 0.459688 0.000020128 128 0.007813 0.459695 0.000005256 256 0.003906 0.459697 0.000001512 512 0.001953 0.459698 0.000000
1
0sin dxxI
Result 26
Range of integration from 0 to 3141.59N Times of
Calling Func
Step-size Integral Error
2 2 1570.795000 -4.168243 645962460.54 4 785.397500 -4.168243 161490615.18 8 392.698750 0.000000 40372653.716 16 196.349375 0.000346 10093163.432 32 98.174688 -0.000072 2523290.864 64 49.087344 -0.000129 630822.70
128 128 24.543672 -0.000142 157705.667256 256 12.271836 -0.000146 39426.419512 512 6.135918 -0.000146 9856.6049
Result 2 (cont)7
N Time of Calling Func
Step-size Integral Error
1000 1000 3.141590 0.000000 2583.849843
10000 10000 0.314159 0.000003 25.838498100000 100000 0.031416 0.000004 0.258385500000 500000 0.006283 0.000004 0.0103351000000 1000000 0.003142 0.000004 0.0025845000000 5000000 0.000628 0.000004 0.00010310000000 10000000 0.000314 0.000004 0.00002620000000 20000000 0.000157 0.000004 0.00000650000000 50000000 0.000063 0.000004 0.000001
Resume No.18
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, it’s 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 Rule9
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 interval’s 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 Code10
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 311
N Step-size Estimated Integral
Error
2 0.500000 0.459862 0.000347
4 0.250000 0.459708 0.000022
8 0.125000 0.459698 0.000001
16 0.062500 0.459698 0.000000
32 0.031250 0.459698 0.000000
64 0.015625 0.459698 0.000000
128 0.007813 0.459698 0.000000
256 0.003906 0.459698 0.000000
512 0.001953 0.459698 0.000000
N Times of calling
Function
Step-size Estimated Integral
Error
2 2 0.500000 0.459862 0.000347
4 4 0.250000 0.459708 0.000022
8 8 0.125000 0.459698 0.000001
16 16 0.062500 0.459698 0.000000
32 32 0.031250 0.459698 0.000000
64 64 0.015625 0.459698 0.000000
128 128 0.007813 0.459698 0.000000
256 256 0.003906 0.459698 0.000000
512 512 0.001953 0.459698 0.000000
Simpson 3/8Rule12
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 413
N Times of Calling Func
Step-size Estimated Integral
Error
3 3 0.333333 0.459771 0.0000696 6 0.166667 0.459702 0.0000049 9 0.111111 0.459699 0.00000115 15 0.055556 0.459698 0.00000033 33 0.030303 0.459698 0.00000066 66 0.015152 0.459698 0.000000
129 129 0.007752 0.459698 0.000000255 255 0.003922 0.459698 0.000000513 513 0.001949 0.459698 0.000000
1
0sin dxxI
Boole Rule14
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 515
N Step-size Estimated Integral Error2 0.500000 0.434201 0.0045764 0.250000 0.475858 0.0000718 0.125000 0.484699 0.00000116 0.062500 0.489841 0.00000032 0.031250 0.492588 0.00000064 0.015625 0.494006 0.000000
128 0.007813 0.494725 0.000000256 0.003906 0.495088 0.000000512 0.001953 0.495270 0.000000
1
0sin dxxI
Resume 216
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 code17
#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: %lf\n", TrueValue); getch(); }
Result19
N H Integral with h
Integral with h/2
Integral using R-E
2 0.500000 0.450081 0.457301 0.459708
4 0.250000 0.457301 0.459099 0.459698
8 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
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;}
20