Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in C Paper – VIII B.

Similar presentations


Presentation on theme: "Programming in C Paper – VIII B."— Presentation transcript:

1 Programming in C Paper – VIII B

2 Numerical Integration Simplest Approach
The area is broken up in narrow rectangles i. e., small straight lines of the form : y = const. are fitted Area = f(x1) h + f(x2) h + f(x3) h +    = Si f(xi) h

3 Programme for (i = 0; i<= n – 1 ; i++) # include < stdio.h>
# include <math.h> # define f(x) x*x void main () { int i, n ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = 0.0; for (i = 0; i<= n – 1 ; i++) x = a + i* h; sum = sum + f(x); } sum = sum *h; printf(“ The value of the integral = %f”, sum);

4 Trapezoidal Rule The area is broken up in narrow trapezioms
i. e., small straight lines of the form : y = mx + c are fitted between two points Area = {f(x0) + f(x1)} h/2 + {f(x1) + f(x2)} h/2 +    + {f(xn –1 ) + f(xn)} h/2 = f(x0)h/2 + f(x1) h + f(x2)} h +   f(xn) h/2 = {f(x0) + 2 S1n –1 f(xi) + f(xn)} h/2

5 Programme (Trapezoidal Rule)
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = f(a) + f(b); for (i = 1; i<= n – 1 ; i++) x = a + i* h; sum = sum + 2*f(x); } sum = sum *h/2; printf(“ The value of the integral = %f”, sum);

6 Simpson’s 1/3 Rule Small parabolae, i.e., 2nd degree curves of the form : y = a + bx + cx2 are fitted with each set of three points. (3 points are needed to fix 3 consts. a, b, c.) Area = {f(x0) + 4f(x1) + 2f(x2) + 4f(x3) + 2f(x4) +    + f(xn)} h/3 Note that Simpson’s 1/3 rule requires even no. of intervals. The Max. error ≤ h4 (b – a) │f4(x)│max /180.

7 Programme (Simpson’s 1/3 Rule)
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = f(a) + f(b); for (i = 1; i<= n – 1 ; i++) x = a + i* h; if (i % 2 = 0) Or if (i / 2 * 2 = 0) sum = sum + 2*f(x); else sum = sum + 4*f(x); } sum = sum *h/3; printf(“ The value of the integral = %f”, sum);

8 Simpson’s 1/3 Rule (2nd Alternative)
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = f(a) + f(b); for (i = 1; i<= n – 1 ; i = i + 2) sum = sum + 4*f(a + i* h); for (i = 2; i<= n – 2 ; i = i + 2) sum = sum + 2*f(a + i* h); sum = sum *h/3; printf(“ The value of the integral = %f”, sum); }

9 Simpson’s 1/3 Rule (3rd Alternative)
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n, m = 4 ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = f(a) + f(b); for (i = 1; i<= n – 1 ; i++) x = a + i* h; sum = sum + m*f(x); m = 6 – m; } sum = sum *h/3; printf(“ The value of the integral = %f”, sum);

10 Simpson’s 3/8 Rule Small 3rd degree curves of the form :
y = a + bx + cx2 + dx3 are fitted with each set of four points. (4 points are needed to fix 4 consts. a, b, c, d.) Area = {f(x0) + 3f(x1) + 3f(x2) + f(x3) + 3f(x4) + 3f(x5) + f(x6) +    + f(xn)} 3h/8 Note that Simpson’s 3/8 rule requires no. of intervals divisible by 3.

11 Programme (Simpson’s 3/8 Rule)
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n ; float a, b, sum ; printf(“ Give me the lower limit, the upper limit and the no. of divisions”); scanf(“ %f %f %d”, &a, &b, &n); sum = f(a) + f(b); for (i = 1; i<= n – 1 ; i++) x = a + i* h; if (i % 3 = 0) sum = sum + f(x); else sum = sum + 3*f(x); } sum = sum *3*h/8; printf(“ The value of the integral = %f”, sum);

12 Integration with variable number of Intervals
Set a number of intervals, say n = 10 Calculate the Integral I Save I, say as I1 Increase n, say n = n + 2 and re-calculate I If │I – I1│ ≤ the required accuracy, then I is the answer, otherwise repeat the above process Remember, n should always be 2m for Simpson’s 1/3 Rule and 3m for Simpson’s 3/8 Rule

13 Example with Simpson’s 1/3 Rule
# include < stdio.h> # include <math.h> # define f(x) x*x void main () { int i, n = 10, m = 4 ; float a, b, sum = 0.0, sum1 ; printf(“ Give me the lower limit and the upper limit and the accuracy”); scanf(“ %f %f %f”, &a, &b, &ac); do { sum1 = sum; I = f(a) + f(b); for (i = 1; i<= n – 1 ; i++) x = a + i* h; I = I + m*f(x); m = 6 – m; } I = I *h/3; n = n + 2; } while (fabs (sum – sum1) > ac) printf(“ The value of the integral = %f, the no. of intervals %f”, sum, n – 2);

14 Soln. of Linear Eqn. Gauss-Jordan Method
a1 x + b1 y + c1 z = d (1) a2 x + b2 y + c2 z = d (2) a3 x + b3 y + c3 z = d (3) Solve (1) for x : x = – (d1 + b1 y + c1 z) / a (4) Solve (2) for y : y = – (d2 + a2 x + c2 z) / b (5) Solve (3) for z : z = – (d3 + a3 x + b3 y) / c (6) Now substitute : the guess values of y and z on the RHS of eqn. (4) the guess values of x and z on the RHS of eqn. (5) the guess values of x and y on the RHS of eqn. (6) to get a new set of values for x, y and z. If the new set of values are close enough to the previous set of values, you got the answers. Otherwise, replace the old set of values by the new ones on the RHS of eqn. (4), (5) and (6) and repeat the process.

15 Programme (Gauss-Jordan Method)
# include < stdio.h> # include <math.h> void main () { float x0, y0, z0, x, y, z, ac; printf(“ Give me the guess values of x, y, z and the accuracy”); scanf(“ %f %f %f %f”, &x0, &y0, &z0, &ac); label : x = – (d1 + b1* y0 + c1*z0)/a1; /* a1, b1, etc. are supposed to be known */ y = – (d2 + a2* x0 + c2*z0)/b2; z = – (d3 + a3* x0 + b3*y0)/c3; if (fabs(x – x0) <= ac && fabs(y – y0) <= ac && fabs(z – z0) <= ac) printf (“ x = %f, y = %f, z = %f”, x0, y0, z0); else x0 = x; y0 = y; z0 = z; goto label; }

16 Soln. of Linear Eqn. Gauss-Seidel Method
a1 x + b1 y + c1 z = d (1) a2 x + b2 y + c2 z = d (2) a3 x + b3 y + c3 z = d (3) Solve (1) for x : x = – (d1 + b1 y + c1 z) / a (4) Solve (2) for y : y = – (d2 + a2 x + c2 z) / b (5) Solve (3) for z : z = – (d3 + a3 x + b3 y) / c (6) Now substitute : the guess values of y and z on the RHS of eqn. (4) the new value of x and the initial guess value of z on the RHS of eqn. (5) the new values of x and y on the RHS of eqn. (6) The process yields a new set of values for x, y and z. If the new set of values are close enough to the previous set of values, you got the answers. Otherwise, replace the initial guess values of x, y, z by the new values and repeat the process. * This method usually leads to a faster convergence than Gauss-Jordan method.

17 Programme (Gauss-Seidel Method)
# include < stdio.h> # include <math.h> void main () { float x0, y0, z0, x, y, z, ac; printf(“ Give me the guess values of x, y, z and the accuracy”); scanf(“ %f %f %f %f”, &x0, &y0, &z0, &ac); label : x = – (d1 + b1* y0 + c1*z0)/a1; y = – (d2 + a2* x + c2*z0)/b2; z = – (d3 + a3* x + b3*y)/c3; if (fabs(x – x0) <= ac && fabs(y – y0) <= ac && fabs(z – z0) <= ac) printf (“ x = %f, y = %f, z = %f”, x0, y0, z0); else x0 = x; y0 = y; z0 = z; goto label; }

18 Quadratic Eqn. /* Eqn. of the form : ax^2 + bx + c = 0 */ # include < stdio.h> # include <math.h> void main () { float a, b, c, disc, x1, x2, re, im; printf(“ Give me the coefficients a, b, c”); scanf(“ %f %f %f”, &a, &b, &c); disc = b*b – 4*a*c, if (disc > 0) {x1 = (- b + sqrt(disc)) / (2*a); x2 = (- b - sqrt(disc)) / (2*a); printf (“ Real and distinct roots : \n); printf (“ x1 = %f, x2 = %f”, x1, x2); } else if (disc == 0) {x1 = (- b / (2*a)); printf (“ Real and equal roots : \n); printf (“ x1 = %f, x1); else {re = (- b / (2*a)); im = sqrt( -disc) / (2*a); printf (“ Complex roots : \n); printf (“ x1 = %f + i %f, x2 = %f – i %f”, re, im, re, im);

19 Soln. of Non-linear Eqns. Bi-section Method
If f(a) < 0 and f(b) > 0, then f(x) = 0 for some x (say x0) between a and b. (One way to check this is : f(a)*f(b) should be < 0) Take c = (a + b)/2. If f(a) < 0 and f(c) > 0, then x0 lies between a and c. If instead, f(c) < 0 and f(b) > 0, then x0 lies between c and b.

20 Programme (Bi-section Method)
# include < stdio.h> # include <math.h> #define f(x) sin(x) void main () { float a, b, c, ac; printf(“Give me the limiting values a and b”); scanf(“%f %f ”, &a, &b); printf(“Give me the required accuracy”); scanf(“%f ”, &ac); if (f(a)*f(b) > 0) printf (“Possibly, no solution within these limits”); else { while (fabs(a – b) > ac) {c = (a + b) /2; if f(a)*f(c) < 0 then b = c; else a = c; } printf (“ The soln. for f(x) = 0 is : %f”, c);

21 Soln. of Non-linear Eqns. Newton-Raphson Method
By Taylor’s theorem, f(x)  f(x0) + h f(x0) So, if f(x) = 0, then h  – f(x0) / f(x0) and x = x0 + h f(x0)  x0 – f(x0) / f(x0) Now take x = x0 and repeat the process until │x – x0│ is small enough.

22 Programme Newton-Raphson Method
# include < stdio.h> # include <math.h> #define f(x) sin(x) #define df(x) cos(x) void main () { float x0, x, ac; printf(“ Give me the guess value x0 and the required accuracy”); scanf(“ %f %f”, &x0, &ac); label : x = x0 – f(x0) / df(x0); if (fabs(x – x0) > = ac) x0 = x; goto label; else printf (“ The soln for f(x) = 0 is : %f”, x0); }


Download ppt "Programming in C Paper – VIII B."

Similar presentations


Ads by Google