Final Assignment(Part 1)

Embed Size (px)

Citation preview

  • 8/2/2019 Final Assignment(Part 1)

    1/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    PART 1- ERRORS AND APPROXIMATIONSPROBLEM 1: ROUNDING-OFF AND FINDING THE ERROR

    Problem definition: Write a program in C that will ask the user to enter a real numberand the number of significant digits to which it will be rounded off. The user will bedisplayed by the rounded off value and the absolute error, relative error and percentageerror due to rounding off.

    Working principle: Due to limitations of computing aids, a digital computer, a deskcalculator or a human can provide only a very few finite number of digits for ournumerical calculations. Thus, we are bound to represent a number by an approximatenumber after discarding all but a proposed number of digits. The omissions of unwanteddigits 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 aunit in the nth place, leave the nth digit unchanged; if the discarded number is greaterthan half of a unit in the nth place, add 1 (one0 to the nth digit. If the discarded digit isexactly 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 thingsthat we are going to visit in the scenario of errors and approximations areAbsolute error,Relative errorand Percentage of error. Absolute error is defined as the differencebetween its true value and approximate or rounded-off value. The relative errorof anumber is known as the absolute error divided by its true value. The relative percentageerror 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#includevoid 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

  • 8/2/2019 Final Assignment(Part 1)

    2/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    for(i=0;i=5){k=k+1;

    }n=(float)k/x*10;

    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.56458ENTER THE NUMBER OF SIGNIFICANT DIGITS :3

    THE ROUNDED OFF VALUE IS : 23.565ABSOLUTE ERROR IS : -0.000420RELATIVE ERROR IS : -0.000018PERCENTAGE ERROR IS : -0.001781

    Discussions:

    In a numerical computation correct to n significant figures, the absolute error isless than or equal to a half a unit in the nth place. Thus, if a number be rounded tom decimal places, the absolute error Ea *10-m

    2

  • 8/2/2019 Final Assignment(Part 1)

    3/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    PART 2- NUMERICAL INTERPOLATIONPROBLEM 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 3.1 3.2 3.3 3.4 3.5f(x) 0 0.6 1.0 1.2 1.3

    Working principle: Interpolation is defined as the method of computing intermediatevalue of a function from a given series of values of the function. In this cases the functionor 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 quadraticinterpolation formulae are based on first and second degree polynomialapproximations.Newton has derived general forward difference interpolation formulae, corresponding for

    tables with constant interval h.The linear and quadratic (forward) interpolation formulaecorrespond to first and second order truncation, respectively. Let x0, x1, x2,,x n-1,xn bea set of equidistant values of argument(or independent variable) x i.e., x1-x0=x2-x1=x3-x2=.=xn-xn-1=h (say) and y0, y1, y2,,yn-1,yn the corresponding values of the functiony=f(x). then the value of y corresponding to a specific value of x lying near the beginningof 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#include

    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]);

    3

  • 8/2/2019 Final Assignment(Part 1)

    4/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    for(i=1;i

  • 8/2/2019 Final Assignment(Part 1)

    5/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    }

    Sample output:

    ENTER THE NUMBER OF OBSERVATIONS :5ENTER THE STEP SIZE :0.1ENTER THE INITIAL VAUE OF x :3.1ENTER THE 5 VALUES OF y :00.61.01.2

    1.3ENTER THE VALUE OF x FOR WHICH THE CORRESPONDING y HAS TO BEFOUND OUT :3.17THE 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 thenonly 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 datagiven for x.

    In the above program that is devised to calculate using Newtons forwardinterpolation formula first takes as input the number of values of both x and y wewant to with. Next value of the step size (h) is taken as the input and then theinitial 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 1y 2y 3y 4y

    5

  • 8/2/2019 Final Assignment(Part 1)

    6/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    00.6

    0.6 -0.20.4 0.0

    1.0 -0.2 0.1

    0.2 -0.11.2 -0.10.1

    1.3

    Now the formula is used- values of y0, 1y0 , 2y0 , 3y0 , 4y0 are taken from thedifference table, factorial of the constants and the powers of h are calculated andput 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 NewtonsBackward Interpolation technique.

    x 20 25 30 35 40 45f(x) 354 332 291 260 231 204

    Working principle: Interpolation is defined as the method of computing intermediatevalue of a function from a given series of values of the function. In this cases the functionor 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 quadraticinterpolation formulae are based on first and second degree polynomialapproximations.Newton has derived general backward difference interpolation formulae, correspondingfor tables with constant interval h. The linear and quadratic (forward) interpolationformulae 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=x3-x2=.=xn-xn-1=h (say) and y0, y1, y2,,yn-1,yn the corresponding valuesof the function y=f(x). then the value of y corresponding to a specific value of x lyingnear the beginning of the tabulated values is given by the Newtons ForwardInterpolation formula:

    y= yn + ((x-xn)/1!*h)*yn-1+((x-xn) (x-xn-1)/ (2!*h2

    )*2

    yn-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 y0which is a function of x.Here x the given value for which y has to be known using the aforesaid formula.

    6

  • 8/2/2019 Final Assignment(Part 1)

    7/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    Source code:

    #include

    #includevoid 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

  • 8/2/2019 Final Assignment(Part 1)

    8/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    arr[c]=mat[i][0];c++;

    }}

    }

    Y+=arr[0];

    for(i=0;i

  • 8/2/2019 Final Assignment(Part 1)

    9/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    First we are to check that whether the values of x are equidistant. If they are thenonly we can apply Newtons Interpolation.

    After the check is done then we either derive the step size by subtracting anyvalue (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 datagiven for x.

    In the above program that is devised to calculate using Newtons backwardinterpolation formula first takes as input the number of values of both x and y wewant to with. Next value of the step size (h) is taken as the input and then theinitial value of x. Then the corresponding values of y are taken as input. Hence wefinish off with taking inputs for the program.

    Next the difference table is calculated with the help of a 2*2 matrix in theprogram. The difference table is shown below :

    y 1y 2y 3y 4y 5y354

    -22332 -19

    -41 29291 10 -37

    -31 -8 45

    260 2 8-29 0

    231 2-27

    204

    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 andput into the formula in their proper places.

    PROBLEM 3: Lagranges Interpolation.

    9

  • 8/2/2019 Final Assignment(Part 1)

    10/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    Problem definition: Write a program in C to find the value of f(2) using LagrangesInterpolation technique.

    x 0 1 2 3 4

    f(x) -12 0 - 6 12

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

    Source code:

    #include

    #include#includevoid 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

  • 8/2/2019 Final Assignment(Part 1)

    11/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    k = i;for(j=0;j

  • 8/2/2019 Final Assignment(Part 1)

    12/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    As Lagrangian functions wr(x)= w(x)-((x-xr)w`(xr)) are invariant under lineartransformation like x=a+bt, it is convenient to use the linear transformation beforestarting the computations.

    Lagranges Interpolation formula is used when the step size between the twovalues of x is not the same.

    PART 2- NUMERICAL INTEGRATIONPROBLEM 1: Trapezoidal Method.

    Problem definition: Write a C program to find the value of usingTrapezoidal rule.Working principle: The process of evaluating a definite integral from a set of tabulatedvalues of the integrand f(x) is called numerical integration. This process when applied toa function of a single variable , is known as quadrature.The trapezoidal rule is a numericalmethod that approximates the value of a definite integral. We consider the definite integralab 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 + 1points 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 theintegral 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 byrepresenting f(x) by an interpolating formula and then integrating it between the givenlimits. In this way, we can derive quadrature formula for approximate integration of afunction 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 byputting the values from the interval in the function to be integrated.

    Source code:

    #include#include#include

    double func(double);

    12

  • 8/2/2019 Final Assignment(Part 1)

    13/27

    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

  • 8/2/2019 Final Assignment(Part 1)

    14/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    -------------------------------

    ENTER THE LOWER LIMIT OF THE FUNCTION :0ENTER THE UPPER LIMIT OF THE FUNCTION :

    1ENTER THE NUMBER OF SUB-INTERVALS :10THE AREA UNDER THE CURVE IS : 0.701725

    Discussions:

    The Trapezoidal Rule is used for numerical integration. This is the simplest ruleto 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 iscalculated by taking the number of intervals as the inputs, let it be n in this caseand find h by the formula (upper limit-lower limit)/2. Also the function to beintegrated is taken as the input and the next values of x are calculated bymultiplying 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 y

    0 1.0000000.1 0.9900500.2 0.960789

    0.3 0.9139310.4 0.8521440.5 0.7788010.6 0.6976760.7 0.6126260.8 0.5272920.9 0.444858

    14

  • 8/2/2019 Final Assignment(Part 1)

    15/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    Now the values of y are put in the general formula and from there we get thevalue of the definite integral.

    The program uses a function in which the function of the integration is workedout 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 is6.0388 %.

    PROBLEM 2: Simpsons 1/3rd Rule.

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

    Working principle: The process of evaluating a definite integral from a set of tabulatedvalues of the integrand f(x) is called numerical integration. This process when applied toa function of a single variable, is known as quadrature. Simpson's 1/3 Rule NumericalIntegration is used to estimate the value of a definite integral. It works by creating aneven number of intervals and fitting a parabola in each pair of intervals. Simpson's ruleprovides the exact result for a quadratic function or parabola.

    General formula, I1/3

    = h/3*[(y0+y

    n) + 2*(y

    1+ y

    3+ y

    5+ + y

    2n-1) + 4*(y

    2+ y

    4+ y

    6+

    .. + y2n)]

    Source code:

    #include#include

    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

  • 8/2/2019 Final Assignment(Part 1)

    16/27

    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

  • 8/2/2019 Final Assignment(Part 1)

    17/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

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

    Discussions:

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

    In this method, the initial value of x and the step size (h) is taken as input or h iscalculated by taking the number of intervals as the inputs, let it be n in this caseand find h by the formula (upper limit-lower limit)/2. Also the function to beintegrated 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 y

    0 1.000000

    0.1 0.9900500.2 0.9607890.3 0.9139310.4 0.8521440.5 0.7788010.6 0.6976760.7 0.6126260.8 0.5272920.9 0.444858

    In the rule we will see that the initial and the final terms are simply added, but thevalues of y with odd indices are added together and multiplied with 2 and theeven indices are added and multiplied with 4.

    The exact value of the integration will be 0.74682, but we have achieved0.720343 after calculating it using Simpsons 1/3rd Rule. So the error percentageis 3.541%, which is less than the Trapezoidal Rule.

    17

  • 8/2/2019 Final Assignment(Part 1)

    18/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    PROBLEM 3: Weddles Rule.

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

    Working principle: The process of evaluating a definite integral from a set of tabulatedvalues of the integrand f(x) is called numerical integration. This process when applied toa 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#include#include

    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

  • 8/2/2019 Final Assignment(Part 1)

    19/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    for(i=0;i

  • 8/2/2019 Final Assignment(Part 1)

    20/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    and find h by the formula (upper limit-lower limit)/2. Also the function to beintegrated is taken as the input and the next values of x are calculated bymultiplying 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 y

    0 1.0000000.1 0.9900500.2 0.9607890.3 0.9139310.4 0.8521440.5 0.7788010.6 0.6976760.7 0.6126260.8 0.5272920.9 0.444858

    In the rule we will see that the initial and the final terms are simply added, but thevalues of y with odd indices are added together and multiplied with 2 and theeven indices are added and multiplied with 4.

    The exact value of the integration will be 0.74682, but we have achieved0.743871 after calculating it using Weddles 1/3rd Rule. So the error percentage is0.3948%, which is less than the Simpsons 1/3rd Rule and Trapezoidal Rule.

    20

  • 8/2/2019 Final Assignment(Part 1)

    21/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    PART 3- NUMERICAL DIFFERENTIATIONPROBLEM 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-ordernumerical procedure for solving ordinarydifferential equations (ODEs) with a given initial value. It is the most basic kind ofexplicit method fornumerical integration of ordinary differential equations. It is a simpleand single-step but a crude method for solving an ordinary initial value differentialequation, where the solution will be obtained as a set of tabulated values of variables xand y.Consider the problem of calculating the shape of an unknown curve which starts at agiven 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 becomputed 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 denotebyA0, is known. Then, from the differential equation, the slope to the curve at A0 can becomputed, and so, the tangent line.Take a small step along that tangent line up to a point A1. If we pretend thatA1 is still onthe curve, the same reasoning as for the pointA0 above can be used. After several steps, apolygonal curve A0, A1, A2, A3 is computed. In general, this curve does not diverge toofar from the original unknown curve, and the error between the two curves can be madesmall 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#include

    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

    http://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Computational_sciencehttp://en.wikipedia.org/wiki/Leonhard_Eulerhttp://en.wikipedia.org/wiki/Numerical_analysishttp://en.wikipedia.org/wiki/Ordinary_differential_equationhttp://en.wikipedia.org/wiki/Ordinary_differential_equationhttp://en.wikipedia.org/wiki/Initial_value_problemhttp://en.wikipedia.org/wiki/Explicit_and_implicit_methodshttp://en.wikipedia.org/wiki/Numerical_ordinary_differential_equationshttp://en.wikipedia.org/wiki/Slopehttp://en.wikipedia.org/wiki/Tangent_linehttp://en.wikipedia.org/wiki/Polygonal_curvehttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Computational_sciencehttp://en.wikipedia.org/wiki/Leonhard_Eulerhttp://en.wikipedia.org/wiki/Numerical_analysishttp://en.wikipedia.org/wiki/Ordinary_differential_equationhttp://en.wikipedia.org/wiki/Ordinary_differential_equationhttp://en.wikipedia.org/wiki/Initial_value_problemhttp://en.wikipedia.org/wiki/Explicit_and_implicit_methodshttp://en.wikipedia.org/wiki/Numerical_ordinary_differential_equationshttp://en.wikipedia.org/wiki/Slopehttp://en.wikipedia.org/wiki/Tangent_linehttp://en.wikipedia.org/wiki/Polygonal_curve
  • 8/2/2019 Final Assignment(Part 1)

    22/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    do{Y[i+1]=Y[i]+h*(3*X[i]*X[i]+Y[i]);i++;X[i]=X[i-1]+h;

    }

    while(X[i]

  • 8/2/2019 Final Assignment(Part 1)

    23/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    PROBLEM 2: Modified Eulers Method

    Problem definition: Given that dy/dx = 3x2 + y, y(0)=4. Find the value of y(0.5) usingModified Eulers Method.

    Working principle: The Modified Eulers method for solving numerical differentialequations gives us a rapid and moderately accurate answer up to a desired degree ofaccuracy.

    General formula: y(i+1) = yi + h* f(xi + h/2,yi + h/2* f(xi,yi))

    Source code:

    #include#include

    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]

  • 8/2/2019 Final Assignment(Part 1)

    24/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    Sample output:

    ENTER THE INITIAL VALUE OF x :

    0ENTER THE INITIAL VALUE OF y :4ENTER THE VALUE OF THE STEP SIZE :0.1THE VALUE OF y(0.5) IS : 7.291253

    Discussions:

    As the name suggests, we can obviously state that a betterment over the previousmethod, 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 calculatedor accepted in the first case. For example we accept x to be 0 in its initial pointand step size to be 0.1, so the next values of x will be 0.1, 0.2, 0.3, and so on aslong we are not satisfied with xs value for which we want to find the answer ofthe derivative of the function.

    The values of y are calculated until we get the value of x to satisfy the exitcondition (here the do-while loop) of the loop, with the help of the generalformula.

    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) usingRunga-Kutta Second Order Method.

    Working principle: The Runge-Kutta method for the numerical solution of an ordinarydifferential equation give us a greater accuracy and also avoid the disadvantages of theTaylors 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) andk2 = h* f(xi +h, yi+ k1)

    24

  • 8/2/2019 Final Assignment(Part 1)

    25/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    Source code:

    #include#include

    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]

  • 8/2/2019 Final Assignment(Part 1)

    26/27

    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 aslong we are not satisfied with xs value for which we want to find the answer ofthe derivative of the function.

    The values of y are calculated until we get the value of x to satisfy the exitcondition (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, itwill 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) usingRunga-Kutta Fourth Order Method.

    Working principle: The Runge-Kutta method for the numerical solution of an ordinarydifferential equation give us a greater accuracy and also avoid the disadvantages of theTaylors 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) andk4= h* f(xi +h, yi+ k3)

    Source code:

    #include#include

    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

  • 8/2/2019 Final Assignment(Part 1)

    27/27

    NUMERICAL ANALYSIS DEBJIT BANERJEE

    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]