Vous êtes sur la page 1sur 27

NUMERICAL ANALYSIS

DEBJIT BANERJEE

PART 1- ERRORS AND APPROXIMATIONS


PROBLEM 1: ROUNDING-OFF

AND FINDING THE ERROR

Problem definition: Write a program in C that will ask the user to enter a real number and the number of significant digits to which it will be rounded off. The user will be displayed by the rounded off value and the absolute error, relative error and percentage error due to rounding off. Working principle: Due to limitations of computing aids, a digital computer, a desk calculator or a human can provide only a very few finite number of digits for our numerical calculations. Thus, we are bound to represent a number by an approximate number after discarding all but a proposed number of digits. The omissions of unwanted digits of a given number are done by a process which is known as rounding-off rules. The general rules for roundingoff a number to n-significant figures are: Discard all digits to the right of the nth place, if the discarded number is less than half a unit in the nth place, leave the nth digit unchanged; if the discarded number is greater than half of a unit in the nth place, add 1 (one0 to the nth digit. If the discarded digit is exactly half a unit in the nth digit, leaving the nth digit unaltered if it is an even number, but increase it by 1 (one) if is an odd number. And now coming to the error section. Errors are generally caused by rounding-off, chopping off or truncation operations done on the real number. The three different things that we are going to visit in the scenario of errors and approximations are Absolute error, Relative error and Percentage of error. Absolute error is defined as the difference between its true value and approximate or rounded-off value. The relative error of a number is known as the absolute error divided by its true value. The relative percentage error is defined as the relative error multiplied by 100. Thus, absolute error Ea = |VT-VA|, relative error Er = Ea/VT= |VT-VA|/ VT and, relative percentage error Ep = Er*100 = (|VT-VA|/ VT) *100. Source code: #include<stdio.h> #include<conio.h> void main() { float num,n; int i,m; long int x=1,z,y,k,a,b; clrscr(); printf("ENTER THE REAL NUMBER : \n"); scanf("%f",&num); printf("ENTER THE NUMBER OF SIGNIFICANT DIGITS :\n"); scanf("%d",&m); 1

NUMERICAL ANALYSIS for(i=0;i<=m;i++) x=x*10; z=num*x; y=z%10; k=z/10; if(y>=5) { k=k+1; } n=(float)k/x*10;

DEBJIT BANERJEE

a=k/(x/10); b=k%(x/10); printf("\nTHE ROUNDED OFF VALUE IS : %ld.%ld",a,b); printf("\nABSOLUTE ERROR IS : %f",num-n); printf("\nRELATIVE ERROR IS : %f",((num-n)/num)); printf("\nPERCENTAGE ERROR IS : %f",(((num-n)/num)*100)); getch(); } Sample output: ENTER THE REAL NUMBER : 23.56458 ENTER THE NUMBER OF SIGNIFICANT DIGITS : 3 THE ROUNDED OFF VALUE IS : 23.565 ABSOLUTE ERROR IS : -0.000420 RELATIVE ERROR IS : -0.000018 PERCENTAGE ERROR IS : -0.001781 Discussions: In a numerical computation correct to n significant figures, the absolute error is less than or equal to a half a unit in the nth place. Thus, if a number be rounded to m decimal places, the absolute error Ea *10-m

NUMERICAL ANALYSIS

DEBJIT BANERJEE

PART 2- NUMERICAL INTERPOLATION


PROBLEM 1: Newtons

Forward Interpolation.

Problem definition: Write a program in C to find the value of f(3.17) using Newtons Forward Interpolation technique. x f(x) 3.1 0 3.2 0.6 3.3 1.0 3.4 1.2 3.5 1.3

Working principle: Interpolation is defined as the method of computing intermediate value of a function from a given series of values of the function. In this cases the function or the relation between two sets are unknown, the only thing known is a set of values, i.e. a value corresponding to the other among the two sets. The linear and quadratic interpolation formulae are based on first and second degree polynomial approximations. Newton has derived general forward difference interpolation formulae, corresponding for tables with constant interval h. The linear and quadratic (forward) interpolation formulae correspond to first and second order truncation, respectively. Let x0, x1, x2,,x n-1,xn be a set of equidistant values of argument(or independent variable) x i.e., x1-x0=x2-x1=x3x2=.=xn-xn-1=h (say) and y0, y1, y2,,yn-1,yn the corresponding values of the function y=f(x). then the value of y corresponding to a specific value of x lying near the beginning of the tabulated values is given by the Newtons Forward Interpolation formula:

y= y0 + ((x-x0)/1!*h)*y0 + ((x-x0) (x-x1)/ (2!*h2)*2y0 + ((x-x0) (x-x1) (x-x2)/ (3!*h3)*3 y0 + ((x-x0) (x-x1) (x-x2) . (x-xn))/ (n! *hn)*n yn
which is a function of x. Here x the given value for which y has to be known using the aforesaid formula. Source code: #include<stdio.h> #include<conio.h> void main(){ float x[50],y[50],arr[50],mat[10][10],Y=0.0,X=0.0,h=0.0,xm=1.0,hp=1.0; int r,c,n,i,j=0,l,k,fact=1,v=1; clrscr(); printf("ENTER THE NUMBER OF OBSERVATIONS :\n"); scanf("%d",&n); printf("ENTER THE STEP SIZE :\n"); scanf("%f",&h); printf("ENTER THE INITIAL VAUE OF x :\n"); scanf("%f",&x[0]);

NUMERICAL ANALYSIS for(i=1;i<n;i++){ x[i]=x[0]+h*i; } printf("ENTER THE %d VALUES OF y :\n",n); for(i=0;i<n;i++){ scanf("%f",&y[i]); arr[i]=y[i]; }

DEBJIT BANERJEE

printf("ENTER THE VALUE OF x FOR WHICH THE CORRESPONDING y HAS TO BE FOUND OUT :\n");

scanf("%f",&X); c=n; for(r=0;r<n;r++){ for(k=0;k<c;k++){ mat[r][j]=arr[k+1]-arr[k]; j++; } c--; for(l=0;l<c;l++){ arr[l]=mat[r][l]; } j=0; } c=n; arr[0]=y[0]; c=1; for(i=0;i<n;i++){ arr[c]=mat[i][0]; c++; } Y+=arr[0]; for(i=0;i<n;i++){ for(j=1;j<=v;j++){ xm*=X-x[j-1]; hp*=h; fact*=v; } Y+=(xm*arr[i+1])/(hp*fact); v++; hp=1,fact=1,xm=1; } printf("THE INTERPOLATED VALUE OF y FOR x=%f IS %f\n",X,Y); getch(); 4

NUMERICAL ANALYSIS } Sample output:

DEBJIT BANERJEE

ENTER THE NUMBER OF OBSERVATIONS : 5 ENTER THE STEP SIZE : 0.1 ENTER THE INITIAL VAUE OF x : 3.1 ENTER THE 5 VALUES OF y : 0 0.6 1.0 1.2 1.3 ENTER THE VALUE OF x FOR WHICH THE CORRESPONDING y HAS TO BE FOUND OUT : 3.17 THE INTERPOLATED VALUE OF y FOR x=3.170000 IS 0.430256 Discussions: First we are to check that whether the values of x are equidistant. If they are then only we can apply Newtons Interpolation. After the check is done then we either derive the step size by subtracting any value (from the second value onwards) from its higher value. Then we device the difference table (which is included in our program). Then with the help of the formula which is stated in the working principle we calculate the interpolated value for which the value of x in given by the user. Newtons forward interpolation formula is generally used for those values of x which lie in the first part obviously lesser than the middle value(s) among the data given for x. In the above program that is devised to calculate using Newtons forward interpolation formula first takes as input the number of values of both x and y we want to with. Next value of the step size (h) is taken as the input and then the initial value of x. Then the corresponding values of y are taken as input. Hence we finish off with taking inputs for the program. Next the difference table is calculated with the help of a 2*2 matrix in the program. The difference table is shown below :

1y

2y 5

3y

4y

NUMERICAL ANALYSIS 0 0.6 0.6 0.4 1.0 0.2 1.2 0.1 1.3 -0.1 -0.2 -0.1 -0.2 0.0

DEBJIT BANERJEE

0.1

Now the formula is used- values of y0, 1y0 , 2y0 , 3y0 , 4y0 are taken from the difference table, factorial of the constants and the powers of h are calculated and put into the formula in their proper places.

PROBLEM 2: Newtons

Backward Interpolation.

Problem definition: Write a program in C to find the value of f(42) using Newtons Backward Interpolation technique. x f(x) 20 354 25 332 30 291 35 260 40 231 45 204

Working principle: Interpolation is defined as the method of computing intermediate value of a function from a given series of values of the function. In this cases the function or the relation between two sets are unknown, the only thing known is a set of values, i.e. a value corresponding to the other among the two sets. The linear and quadratic interpolation formulae are based on first and second degree polynomial approximations. Newton has derived general backward difference interpolation formulae, corresponding for tables with constant interval h. The linear and quadratic (forward) interpolation formulae correspond to first and second order truncation, respectively. Let x0, x1, x2, ,x n-1,xn be a set of equidistant values of argument(or independent variable) x i.e., x1x0=x2-x1=x3-x2=.=xn-xn-1=h (say) and y0, y1, y2,,yn-1,yn the corresponding values of the function y=f(x). then the value of y corresponding to a specific value of x lying near the beginning of the tabulated values is given by the Newtons Forward Interpolation formula: y= yn + ((x-xn)/1!*h)*yn-1+((x-xn) (x-xn-1)/ (2!*h2)*2yn-2+((x-xn) (x-xn-1) (x-xn-2)/ (3! *h3)*3 yn-3 + ((x-xn) (x-xn-1) (x-xn-2) . (x-x0))/ (n!*hn)*n y0 which is a function of x. Here x the given value for which y has to be known using the aforesaid formula.

NUMERICAL ANALYSIS

DEBJIT BANERJEE

Source code: #include<stdio.h> #include<conio.h> void main(){ float x[50],y[50],arr[50],mat[10][10],Y=0.0,X=0.0,h=0.0,xm=1.0,hp=1.0; int r,c,n,i,j=0,l,k,fact=1,v=1; clrscr(); printf("ENTER THE NUMBER OF OBSERVATIONS :\n"); scanf("%d",&n); printf("ENTER THE STEP SIZE :\n"); scanf("%f",&h); printf("ENTER THE INITIAL VAUE OF x :\n"); scanf("%f",&x[0]); for(i=1;i<n;i++){ x[i]=x[0]+h*i; } printf("ENTER THE %d VALUES OF y :\n",n); for(i=0;i<n;i++){ scanf("%f",&y[i]); arr[i]=y[i]; }
printf("ENTER THE VALUE OF x FOR WHICH THE CORRESPONDING y HAS TO BE FOUND OUT :\n");

scanf("%f",&X); c=n; for(r=0;r<n;r++){ for(k=0;k<c;k++){ mat[r][j]=arr[k+1]-arr[k]; j++; } c--; for(l=0;l<c;l++){ arr[l]=mat[r][l]; } j=0; } c=n; arr[0]=y[n-1]; c=1; for(i=0;i<n;i++){ for(j=0;j<n;j++){ if((i+j)==(n-2)){ 7

NUMERICAL ANALYSIS arr[c]=mat[i][0]; c++; } } } Y+=arr[0]; for(i=0;i<n;i++){ for(j=1;j<=v;j++){ xm*=X-x[n-j]; hp*=h; fact*=v; } Y+=(xm*arr[i+1])/(hp*fact); v++; hp=1,fact=1,xm=1; }

DEBJIT BANERJEE

printf("THE INTERPOLATED VALUE OF y FOR x=%f : %f\n",X,Y); getch(); } Sample output: ENTER THE NUMBER OF OBSERVATIONS : 6 ENTER THE STEP SIZE : 5 ENTER THE INITIAL VAUE OF x : 20 ENTER THE 6 VALUES OF y : 354 332 291 260 231 204 ENTER THE VALUE OF x FOR WHICH THE CORRESPONDING y HAS TO BE FOUND OUT : 42 THE INTERPOLATED VALUE OF y FOR x=42.000000 : 218.056183 Discussions:

NUMERICAL ANALYSIS

DEBJIT BANERJEE

First we are to check that whether the values of x are equidistant. If they are then only we can apply Newtons Interpolation. After the check is done then we either derive the step size by subtracting any value (from the second value onwards) from its higher value. Then we device the difference table (which is included in our program). Then with the help of the formula which is stated in the working principle we calculate the interpolated value for which the value of x in given by the user. Newtons backward interpolation formula is generally used for those values of x which lie in the first part obviously lesser than the middle value(s) among the data given for x. In the above program that is devised to calculate using Newtons backward interpolation formula first takes as input the number of values of both x and y we want to with. Next value of the step size (h) is taken as the input and then the initial value of x. Then the corresponding values of y are taken as input. Hence we finish off with taking inputs for the program. Next the difference table is calculated with the help of a 2*2 matrix in the program. The difference table is shown below : y 354 332 -41 291 -31 260 -29 231 -27 204 2 2 0 10 -8 8 1y -22 -19 29 -37 45 2y 3y 4y 5y

Now the formula is used- values of y0, 1yn , 2yn , 3yn , 4yn are taken from the difference table, factorial of the constants and the powers of h are calculated and put into the formula in their proper places.

PROBLEM 3: Lagranges

Interpolation.
9

NUMERICAL ANALYSIS

DEBJIT BANERJEE

Problem definition: Write a program in C to find the value of f(2) using Lagranges Interpolation technique. x f(x) 0 -12 1 0 2 3 6 4 12

Working principle: Lagranges Interpolation formula is used when the step size between the two values of x is not the same. When y0 = f(x0), y1 = f(x1), . , yn = f(xn) are only the known values of y=f(x) on the set set of (n+1) distinct arguments x0, x1, x2,,x n In general not equally spaced , the Lagranges Interpolation formula is :

Source code: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c; clrscr(); printf("\nENTER THE VALUE OF n : "); scanf("%d",&n); for(i=0; i<n; i++){ printf("\nENTER THE VALUE OF x%d: ",i); scanf("%f",&x[i]); printf("\nENTER THE VALUE OF f(x%d): ",i); scanf("%f",&y[i]); }
printf("\nENTER THE VALUE OF f(x) FOR WHICH VALUE HAS TO BE FOUND OUT : ");

scanf("%f",&p); for(i=0;i<n;i++){ temp = 1;

10

NUMERICAL ANALYSIS k = i; for(j=0;j<n;j++){ if(k==j){ continue; } else{ temp = temp * ((p-x[j])/(x[k]-x[j])); } } f[i]=y[i]*temp; } for(i=0;i<n;i++){ sum = sum + f[i]; } printf("\nTHE VALUE OF f(%.1f) = %f ",p,sum); getch(); } Sample output: ENTER THE VALUE OF n : 4 ENTER THE VALUE OF x0: 0 ENTER THE VALUE OF f(x0): -12 ENTER THE VALUE OF x1: 1 ENTER THE VALUE OF f(x1): 0 ENTER THE VALUE OF x2: 3 ENTER THE VALUE OF f(x2): 6 ENTER THE VALUE OF x3: 4 ENTER THE VALUE OF f(x3): 12

DEBJIT BANERJEE

ENTER THE VALUE OF f(x) FOR WHICH VALUE HAS TO BE FOUND OUT : 2 THE VALUE OF f(2.0) = 4.000000 Discussions: Lagranges Interpolation Formula, to solve is very labourious.

11

NUMERICAL ANALYSIS

DEBJIT BANERJEE

As Lagrangian functions wr(x)= w(x)-((x-xr)w`(xr)) are invariant under linear transformation like x=a+bt, it is convenient to use the linear transformation before starting the computations. Lagranges Interpolation formula is used when the step size between the two values of x is not the same.

PART 2- NUMERICAL INTEGRATION


PROBLEM 1: Trapezoidal

Method.

Problem definition: Write a C program to find the value of Trapezoidal rule.

using

Working principle: The process of evaluating a definite integral from a set of tabulated values of the integrand f(x) is called numerical integration. This process when applied to a function of a single variable , is known as quadrature. The trapezoidal rule is a numerical method that approximates the value of a definite integral. We consider the definite integral ab f(x)dx within a definite interval a and b. We assume that f(x) is continuous on [a, b] and we divide [a, b] into n subintervals of equal length x = (b a)/n using the n + 1 points x0 = a, x1 = a + x, x2 = a + 2x, . . . , xn = a + nx = b. We can compute the value of f(x) at these points. y0 = f(x0), y1 = f(x1), y2 = f(x2), . . . , yn = f(xn). We approximate the integral by using n trapezoids formed by using straight line segments between the points (xi1, yi1) and (xi, yi) for 1 i n. The problem of numerical integration is solved by representing f(x) by an interpolating formula and then integrating it between the given limits. In this way, we can derive quadrature formula for approximate integration of a function defined by a set of numerical values only. General formula, IT = h/2[(y0+yn) + 2*(y1 + y2 + y3 + . + yn-1)] where h is the step size and y0, y1, y2, .... ,yn-1,yn are the derived values of y found out by putting the values from the interval in the function to be integrated. Source code: #include<stdio.h> #include<conio.h> #include<math.h> double func(double);

12

NUMERICAL ANALYSIS

DEBJIT BANERJEE

void main(){ double a,b,h,x[50],y[50],IT=0.0,intr; int n=0,i; clrscr(); printf("\t\tTRAPEZOIDAL RULE :\n\t\t------------------\n\n\n\n\n"); printf("ENTER THE LOWER LIMIT OF THE FUNCTION :\n"); scanf("%lf",&a); printf("ENTER THE UPPER LIMIT OF THE FUNCTION :\n"); scanf("%lf",&b); printf("ENTER THE NUMBER OF SUB-INTERVALS :\n"); scanf("%d",&n); h=(b-a)/n; x[0]=a; for(i=2;i<=n+1;i++){ x[i-1]=x[0]+(i-1)*h; } for(i=0;i<=n;i++){ y[i]=func(x[i]); } /*printf("\nX \t\tY\n"); for(i=0;i<=n;i++){ printf("%f",x[i]); printf("%f\t\t\t",y[i]); printf("\n"); } */ for(i=1;i<n-1;i++){ intr+=y[i]; } intr*=2; IT=(h/2)*((y[0]+y[n])+intr); printf("THE AREA UNDER THE CURVE IS : %lf\n",IT); getch(); } double func(double x){ double r=0.0,xs=0.0; xs=x*x*-1; r=exp(xs); return(r); } Sample output: TRAPEZOIDAL RULE :

13

NUMERICAL ANALYSIS ------------------------------ENTER THE LOWER LIMIT OF THE FUNCTION : 0 ENTER THE UPPER LIMIT OF THE FUNCTION : 1 ENTER THE NUMBER OF SUB-INTERVALS : 10 THE AREA UNDER THE CURVE IS : 0.701725

DEBJIT BANERJEE

Discussions: The Trapezoidal Rule is used for numerical integration. This is the simplest rule to calculate the integration of any non-elementary functions such as

In this method, the initial value of x and the step size (h) is taken as input or h is calculated by taking the number of intervals as the inputs, let it be n in this case and find h by the formula (upper limit-lower limit)/2. Also the function to be integrated is taken as the input and the next values of x are calculated by multiplying h with loop variable value and then adding it to the initial value of x. The values of y are achieved by putting the value of x in the given function, here

The values are manually solved and are given below: For the generated table below the number of interval is taken 10. So h=(1-0)/10, or h=0.1. x 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 1.000000 0.990050 0.960789 0.913931 0.852144 0.778801 0.697676 0.612626 0.527292 0.444858

14

NUMERICAL ANALYSIS

DEBJIT BANERJEE

Now the values of y are put in the general formula and from there we get the value of the definite integral. The program uses a function in which the function of the integration is worked out using the values of x. the function return the value of y from the corresponding value of x. The exact value of the integration will be 0.74682, but we have achieved 0.701725 after calculating it using Trapezoidal Rule. So the error percentage is 6.0388 %.

PROBLEM 2: Simpsons

1/3rd Rule.
using

Problem definition: Write a C program to find the value of Simpsons 1/3rd rule.

Working principle: The process of evaluating a definite integral from a set of tabulated values of the integrand f(x) is called numerical integration. This process when applied to a function of a single variable, is known as quadrature. Simpson's 1/3 Rule Numerical Integration is used to estimate the value of a definite integral. It works by creating an even number of intervals and fitting a parabola in each pair of intervals. Simpson's rule provides the exact result for a quadratic function or parabola. General formula, I1/3 = h/3*[(y0+yn) + 2*(y1 + y3 + y5 + + y2n-1) + 4*(y2 + y4 + y6 + .. + y2n)] Source code: #include<stdio.h> #include<conio.h> double func(double); void main(){ double a,b,h,x[50],y[50],IT=0.0,intr1,intr2; int n=0,i; clrscr(); printf("\t\tSIMPSON'S 1/3rd RULE :\n\t\t----------------------\n\n\n\n\n"); printf("ENTER THE LOWER LIMIT OF THE FUNCTION :\n"); scanf("%lf",&a); printf("ENTER THE UPPER LIMIT OF THE FUNCTION :\n");

15

NUMERICAL ANALYSIS

DEBJIT BANERJEE

scanf("%lf",&b); printf("ENTER THE NUMBER OF SUB-INTERVALS :\n"); scanf("%d",&n); h=(b-a)/n; x[0]=a; for(i=2;i<=n+1;i++){ x[i-1]=x[0]+(i-1)*h; } for(i=0;i<=n;i++){ y[i]=func(x[i]); } /*printf("\nX \t\tY\n"); for(i=0;i<=n;i++){ printf("%f",x[i]); printf("%f\t\t\t",y[i]); printf("\n"); } */ for(i=1;i<n-1;i++){ if(i%2!=0) intr1+=y[i]; else intr2+=y[i]; } intr1*=2; intr2*=4; IT=(h/3)*((y[0]+y[n])+intr1+intr2); printf("THE AREA UNDER THE CURVE IS : %lf\n",IT); getch(); } double func(double x){ double r=0.0,xs=0.0; xs=x*x*-1; r=exp(xs); return(r); } Sample output: SIMPSON'S 1/3rd RULE : -------------------------------ENTER THE LOWER LIMIT OF THE FUNCTION : 0 ENTER THE UPPER LIMIT OF THE FUNCTION :

16

NUMERICAL ANALYSIS 1 ENTER THE NUMBER OF SUB-INTERVALS : 10 THE AREA UNDER THE CURVE IS : 0.720343 Discussions:

DEBJIT BANERJEE

The Simpsons 1/3rd Rule is used for numerical integration. This is a better rule to calculate the integration of any non-elementary functions than the previous or the trapezoidal rule, such as

In this method, the initial value of x and the step size (h) is taken as input or h is calculated by taking the number of intervals as the inputs, let it be n in this case and find h by the formula (upper limit-lower limit)/2. Also the function to be integrated is taken as the input and the next values of x are calculated by multiplying h with loop variable value and then adding it to the initial value of x. The values of y are achieved by putting the value of x in the given function, here

The values are manually solved and are given below: For the generated table below the number of interval is taken 10. So h=(1-0)/10, or h=0.1. x 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 1.000000 0.990050 0.960789 0.913931 0.852144 0.778801 0.697676 0.612626 0.527292 0.444858

In the rule we will see that the initial and the final terms are simply added, but the values of y with odd indices are added together and multiplied with 2 and the even indices are added and multiplied with 4. The exact value of the integration will be 0.74682, but we have achieved 0.720343 after calculating it using Simpsons 1/3rd Rule. So the error percentage is 3.541%, which is less than the Trapezoidal Rule.

17

NUMERICAL ANALYSIS
PROBLEM 3: Weddles

DEBJIT BANERJEE

Rule.
using Weddles

Problem definition: Write a C program to find the value of rule.

Working principle: The process of evaluating a definite integral from a set of tabulated values of the integrand f(x) is called numerical integration. This process when applied to a function of a single variable, is known as quadrature. General formula, IW = (3*h)/10*[(1*y0 + 5*y1 + 1*y2 + 6*y3 + 1*y4 + 5*y5 + 1*y6) + (1*y6 + 5*y7 + 1*y8 + 6*y9 + 1*y10 + 5*y11 + 1*y12) + .] Source code: #include<stdio.h> #include<conio.h> #include<math.h> double func(double); void main(){ double a,b,h,x[50],y[50],IT=0.0,intr1,intr2; int n=0,i; clrscr(); printf("\t\tWEDDEL'S RULE :\n\t\t------------------\n\n\n\n\n"); printf("ENTER THE LOWER LIMIT OF THE FUNCTION :\n"); scanf("%lf",&a); printf("ENTER THE UPPER LIMIT OF THE FUNCTION :\n"); scanf("%lf",&b); printf("ENTER THE NUMBER OF SUB-INTERVALS :\n"); scanf("%d",&n); h=(b-a)/n; x[0]=a; for(i=2;i<=n+1;i++){ x[i-1]=x[0]+(i-1)*h; } for(i=0;i<=n;i++){ y[i]=func(x[i]); }

18

NUMERICAL ANALYSIS for(i=0;i<n;i++){ sum+=c[index]*y[i]; index++; if(index==7){ index=0,i--; } } sum=(sum*3*h)/10; printf("THE AREA UNDER THE CURVE IS : %f\n",sum); printf("THE AREA UNDER THE CURVE IS : %lf\n",IT); getch(); } double func(double x){ double r=0.0,xs=0.0; xs=x*x*-1; r=exp(xs); return(r); } Sample output: WEDDLE'S RULE : ------------------------ENTER THE LOWER LIMIT OF THE FUNCTION : 0 ENTER THE UPPER LIMIT OF THE FUNCTION : 1 ENTER THE NUMBER OF SUB-INTERVALS : 10 THE AREA UNDER THE CURVE IS : 0.743871 Discussions:

DEBJIT BANERJEE

The Weddles Rule is used for numerical integration. This is a perpetual rule to calculate the integration of any non-elementary functions than the previous two i.e. Trapezoidal Rule and Simpsons 1/3rd Rule, such as

This rule gives an answer which is at this moment the most correct and nearest to the original calculated value. In this method, the initial value of x and the step size (h) is taken as input or h is calculated by taking the number of intervals as the inputs, let it be n in this case 19

NUMERICAL ANALYSIS

DEBJIT BANERJEE

and find h by the formula (upper limit-lower limit)/2. Also the function to be integrated is taken as the input and the next values of x are calculated by multiplying h with loop variable value and then adding it to the initial value of x. The values of y are achieved by putting the value of x in the given function, here

The values are manually solved and are given below: For the generated table below the number of interval is taken 10. So h=(1-0)/10, or h=0.1. x 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 1.000000 0.990050 0.960789 0.913931 0.852144 0.778801 0.697676 0.612626 0.527292 0.444858

In the rule we will see that the initial and the final terms are simply added, but the values of y with odd indices are added together and multiplied with 2 and the even indices are added and multiplied with 4. The exact value of the integration will be 0.74682, but we have achieved 0.743871 after calculating it using Weddles 1/3rd Rule. So the error percentage is 0.3948%, which is less than the Simpsons 1/3rd Rule and Trapezoidal Rule.

20

NUMERICAL ANALYSIS

DEBJIT BANERJEE

PART 3- NUMERICAL DIFFERENTIATION


PROBLEM 1: Eulers

Method

Problem definition: Given that dy/dx = 3x2 + y, y(0)=4. Find the value of y(0.5) using Eulers Method. Working principle: In mathematics and computational science, the Euler method, named after Leonhard Euler, is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic kind of explicit method for numerical integration of ordinary differential equations. It is a simple and single-step but a crude method for solving an ordinary initial value differential equation, where the solution will be obtained as a set of tabulated values of variables x and y. Consider the problem of calculating the shape of an unknown curve which starts at a given point and satisfies a given differential equation. Here, a differential equation can be thought of as a formula by which the slope of the tangent line to the curve can be computed at any point on the curve, once the position of that point has been calculated. The idea is that while the curve is initially unknown, its starting point, which we denote by A0, is known. Then, from the differential equation, the slope to the curve at A0 can be computed, and so, the tangent line. Take a small step along that tangent line up to a point A1. If we pretend that A1 is still on the curve, the same reasoning as for the point A0 above can be used. After several steps, a polygonal curve A0, A1, A2, A3 is computed. In general, this curve does not diverge too far from the original unknown curve, and the error between the two curves can be made small if the step size is small enough and the interval of computation is finite. General formula: y(i+1) = yi + h* f(xi,yi) Source code: #include<stdio.h> #include<conio.h> void main(){ int i=0; float X[10],Y[10],h; clrscr(); printf("ENTER THE INITIAL VALUE OF x : \n"); scanf("%f",&X[0]); printf("ENTER THE INITIAL VALUE OF y : \n"); scanf("%f",&Y[0]); printf("ENTER THE VALUE OF THE STEP SIZE :\n"); scanf("%f",&h);

21

NUMERICAL ANALYSIS do{ Y[i+1]=Y[i]+h*(3*X[i]*X[i]+Y[i]); i++; X[i]=X[i-1]+h; } while(X[i]<=0.5); printf("THE VALUE OF y(0.5) IS : %f",Y[i]); getch(); }

DEBJIT BANERJEE

Sample output: ENTER THE INITIAL VALUE OF x : 0 ENTER THE INITIAL VALUE OF y : 4 ENTER THE VALUE OF THE STEP SIZE : 0.1 THE VALUE OF y(0.5) IS : 7.267079 Discussions: Eulers Method is used to find the derivative if a particular function at certain value of with respect to which we are to differentiate the function. This the primitive method of doing the aforesaid task. In the given code we take as inputs the initial value of x, y and the step size (h), and then calculate the further values of x by adding h to the previously calculated or accepted in the first case. For example we accept x to be 0 in its initial point and step size to be 0.1, so the next values of x will be 0.1, 0.2, 0.3, and so on as long we are not satisfied with xs value for which we want to find the answer of the derivative of the function. The values of y are calculated until we get the value of x to satisfy the exit condition (here the do-while loop) of the loop, with the help of the general formula. This method is too much laborious when h is very small. This method depends on the step size and smaller the step size the better the accuracy of the result yielded.

22

NUMERICAL ANALYSIS
PROBLEM 2: Modified

DEBJIT BANERJEE

Eulers Method

Problem definition: Given that dy/dx = 3x2 + y, y(0)=4. Find the value of y(0.5) using Modified Eulers Method. Working principle: The Modified Eulers method for solving numerical differential equations gives us a rapid and moderately accurate answer up to a desired degree of accuracy. General formula: y(i+1) = yi + h* f(xi + h/2 ,yi + h/2* f(xi,yi)) Source code: #include<stdio.h> #include<conio.h> void main(){ float X[10],Y[10],h; int i=0; clrscr(); printf("ENTER THE INITIAL VALUE OF x : \n"); scanf("%f",&X[0]); printf("ENTER THE INITIAL VALUE OF y : \n"); scanf("%f",&Y[0]); printf("ENTER THE VALUE OF THE STEP SIZE :\n"); scanf("%f",&h); do{
Y[i+1]=Y[i]+h*(3*(X[i]*(h/2))*(X[i]*(h/2))+(Y[i]+ ((h/2)*(3*(X[i]*X[i])+Y[i]))));

i++; X[i]=X[i-1]+h; } while(X[i]<=0.5); printf("THE VALUE OF y(0.5) IS : %f",Y[i]); getch(); }

23

NUMERICAL ANALYSIS

DEBJIT BANERJEE

Sample output: ENTER THE INITIAL VALUE OF x : 0 ENTER THE INITIAL VALUE OF y : 4 ENTER THE VALUE OF THE STEP SIZE : 0.1 THE VALUE OF y(0.5) IS : 7.291253 Discussions: As the name suggests, we can obviously state that a betterment over the previous method, that is the Eulers Method is the Modified Eulers Method. It gives the answer which is closer to the actual value, better than that of the previous method. In the given code we take as inputs the initial value of x, y and the step size (h), and then calculate the further values of x by adding h to the previously calculated or accepted in the first case. For example we accept x to be 0 in its initial point and step size to be 0.1, so the next values of x will be 0.1, 0.2, 0.3, and so on as long we are not satisfied with xs value for which we want to find the answer of the derivative of the function. The values of y are calculated until we get the value of x to satisfy the exit condition (here the do-while loop) of the loop, with the help of the general formula.

PROBLEM 3: Runga-Kutta

Method Second Order

Problem definition: Given that dy/dx = 3x2 + y, y(0)=4. Find the value of y(0.5) using Runga-Kutta Second Order Method. Working principle: The Runge-Kutta method for the numerical solution of an ordinary differential equation give us a greater accuracy and also avoid the disadvantages of the Taylors Series method which demands the higher order total derivatives of y(x). General formula y(i+1) = yi + (k1+k2) where k1 = h* f(xi,yi) and k2 = h* f(xi +h, yi+ k1)

24

NUMERICAL ANALYSIS

DEBJIT BANERJEE

Source code: #include<stdio.h> #include<conio.h> void main(){ float X[10],Y[10],h,k1=0,k2=0; int i=0; printf("ENTER THE INITIAL VALUE OF x :\n"); scanf("%f",&X[0]); printf("ENTER THE INITIAL VALUE OF y :\n"); scanf("%f",&Y[0]); printf("ENTER THE VALUE OF STEP SIZE :\n"); scanf("%f",&h); do{ k1=h*(3*X[i]*X[i]+Y[i]); k2=h*(3*(X[i]+h)*(X[i]+h)+(Y[i]+k1)); Y[i+1]=Y[i]+0.5*(k1+k2); i++; X[i]=X[i-1]+h; }while(X[i]<=0.5); printf("THE VALUE OF y(0.5) IS : %f.\n",Y[i]); getch(); } Sample output: ENTER THE INITIAL VALUE OF x : 0 ENTER THE INITIAL VALUE OF y : 4 ENTER THE VALUE OF STEP SIZE : 0.1 THE VALUE OF y(0.5) IS : 7.536007. Discussions: With the general formula of this method we can get the output if we supply the value of x, y and the step size (h). In the given code we take as inputs the initial value of x, y and the step size (h), and then calculate the further values of x by adding h to the previously calculated or accepted in the first case. For example we accept x to be 0 in its initial point

25

NUMERICAL ANALYSIS

DEBJIT BANERJEE

and step size to be 0.1, so the next values of x will be 0.1, 0.2, 0.3, and so on as long we are not satisfied with xs value for which we want to find the answer of the derivative of the function. The values of y are calculated until we get the value of x to satisfy the exit condition (here the do-while loop) of the loop, with the help of the general formula. The advantage of this method is that the computational formulae demand only the functional values at some selected points. This method is simple and gives us a result of moderately better accuracy. The main disadvantage of this method id that there is no practically suitable way of checking the computation. So, if any computational error in any stage occurs, it will be propagated in the subsequent stage and remains undetected.

PROBLEM 4: Runga-Kutta

Method Fourth Order

Problem definition: Given that dy/dx = 3x2 + y, y(0)=4. Find the value of y(0.5) using Runga-Kutta Fourth Order Method. Working principle: The Runge-Kutta method for the numerical solution of an ordinary differential equation give us a greater accuracy and also avoid the disadvantages of the Taylors Series method which demands the higher order total derivatives of y(x). General formula y(i+1) = yi + 1/6*(k1+2k2 2k3 + k4) Where, k1 = h* f(xi,yi), k2 = h* f(xi +h/2, yi+ k1/2), k3 = h* f(xi +h/2, yi+ k2/2) and k4= h* f(xi +h, yi+ k3) Source code: #include<stdio.h> #include<conio.h> void main(){ float X[10],Y[10],h,k1=0,k2=0,k3=0,k4=0; int i=0; clrscr(); printf("ENTER THE INITIAL VALUE OF x :\n"); scanf("%f",&X[0]); printf("ENTER THE INITIAL VALUE OF y :\n"); scanf("%f",&Y[0]); printf("ENTER THE VALUE OF STEP SIZE :\n"); scanf("%f",&h); do{ 26

NUMERICAL ANALYSIS k1=h*(3*X[i]*X[i]+Y[i]); k2=h*(3*(X[i]+h/2)*(X[i]+h/2)+(Y[i]+k1/2)); k3=h*(3*(X[i]+h/2)*(X[i]+h/2)+(Y[i]+k2/2)); k4=h*(3*(X[i]+h)*(X[i]+h)+(Y[i]+k3)); Y[i+1]=Y[i]+((k1+2*k2+2*k3+k4)/6); i++; X[i]=X[i-1]+h; }while(X[i]<=0.5); printf("THE VALUE OF y(0.5) IS : %f.\n",Y[i]); getch(); } Sample output: ENTER THE INITIAL VALUE OF x : 0 ENTER THE INITIAL VALUE OF y : 4 ENTER THE VALUE OF STEP SIZE : 0.1 THE VALUE OF y(0.5) IS : 7.541185. Discussions:

DEBJIT BANERJEE

With the general formula of this method we can get the output if we supply the value of x, y and the step size (h). In the given code we take as inputs the initial value of x, y and the step size (h), and then calculate the further values of x by adding h to the previously calculated or accepted in the first case. For example we accept x to be 0 in its initial point and step size to be 0.1, so the next values of x will be 0.1, 0.2, 0.3, and so on as long we are not satisfied with xs value for which we want to find the answer of the derivative of the function. The values of y are calculated until we get the value of x to satisfy the exit condition (here the do-while loop) of the loop, with the help of the general formula. The advantage of this method is that the computational formulae demand only the functional values at some selected points. This method is simple and gives us a result of moderately better accuracy. The main disadvantage of this method id that there is no practically suitable way of checking the computation. So, if any computational error in any stage occurs, it will be propagated in the subsequent stage and remains undetected.

27

Vous aimerez peut-être aussi