Category Archives: Numerical Method

C code using Forward Interpolation

Problem: The population of a town is given below as thousands
Year : 1891 1901 1911 1921 1931
Population : 46 66 81 93 101

Find the population of 1895 ?

 

Code:

Output:

forward interpolation

C code using lagrange method

Problem: The population of a town is given below as thousands
Year            : 1891  1901  1911  1921  1931
Population :  46       66      81      93     101

Find the population of 1895 ?

 

Code:

 Output:

lagrange method

C code using Backward Interpolation

Problem: The population of a town is given below as thousands
Year            : 1891  1901  1911  1921  1931
Population :  46       66      81      93     101

Find the population of 1895 ?

 

Code:

 Output:

backward

C code using Gauss Seidel Method

Problem: Solve the following systems using gauss seidel method
5×1-x2-x3-x4=-4
-x1+10×2-x3-x4=12
-x1-x2+5×3-x4=8
-x1-x2-x3+10×4=34

Code:

 Output:

gauss seidel method

C code using Euler Method

Problem: Here we have to find dy/dx=x+y where y(0)=1 at the point x=0.05 and x=0.10 taking h=0.05

Algorithm:

  1. Start
  2. Define function
  3. Get the values of x0, y0, h and xn
    *Here x0 and y0 are the initial conditions
    h is the interval
    xn is the required value
  4. n = (xn – x0)/h + 1
  5. Start loop from i=1 to n
  6. y = y0 + h*f(x0,y0)
    x = x + h
  7. Print values of y0 and x0
  8. Check if x < xn
    If yes, assign x0 = x and y0 = y
    If no, goto 9.
  9. End loop i
  10. Stop

 

Code:

 Output:

euler

C code using Runge-Kutta 4th order method

Problem: Here we have to find y(0,2) and y(0,4), Given dy/dx=1+y^2 where y=0 when x=0

Algorithm:

Step 1: input x0,y0,h,last point n

Step 2:m1=f(xi,yi)

Step 3:m2=f(xi+h/2,yi+m1h/2)

Step 4:m3=f(xi+h/2,yi+m2h/2)

Step 5:m4=f(xi+h,yi+m3h)

Step 6:yi+1=yi+(m1+2m2+2m3+m4/6)h

Step 5:Display output

Code:

 Output:

runge kutta

C code for newton-raphson method

Problem: Here we have to find root for the polynomial x^3-8*x-4 upto 6D(decimal places)

Solution in C:

 Output:
2014-11-27 11_48_33-

C code using Weddle’s rule

Problem: Here we have to find integration for the (1/1+x*x)dx with lower limit =0 to upper limit = 6

Algorithm:

Step 1: input a,b,number of interval n

Step 2:h=(b-a)/n

Step 3:If(n%6==0)

Then , sum=sum+((3*h/10)*(y(a)+y(a+2*h)+5*y(a+h)+6*y(a+3*h)+y(a+4*h)+5*y(a+5*h)+y(a+6*h)));
a=a+6*h
and Weddle’s rule is applicable then go to step 6

Step 4: else, Weddle’s rule is not applicable

Step 5:Display output

Code:

Output:

2014-11-27 02_44_27-

C code using Trapezoidal Rule

Problem: Here we have to find integration for the (1/1+x*x)dx with lower limit =0 to upper limit = 6

Algorithm:

Step 1: input a,b,number of interval n

Step 2:h=(b-a)/n

Step 3:sum=f(a)+f(b)

Step 4:If n=1,2,3,……i

Then , sum=sum+2*y(a+i*h)

Step 5:Display output=sum *h/2

 

Code:

Output:

2014-11-27 08_59_58-

C code using Simpson’s 3/8 rule or Simpson’s three by eight rule

Problem: Here we have to find integration for the (1/1+x*x)dx with lower limit =0 to upper limit = 6

Algorithm:

Step 1: input a,b,number of interval n

Step 2:h=(b-a)/n

Step 3:sum=f(a)+f(b)

Step 4:If n is odd

Then , sum=sum+2*y(a+i*h)

Step 5: else, When   n I s even
Then, Sum = sum+3*y(a+i*h)

Step 6:Display output=sum *3* h/8

 

Code:

Output:

2014-11-27 02_44_27-

C code using Simpson’s 1/3 rule or Simpson’s one third rule

Problem: Here we have to find integration for the (1/1+x*x)dx with lower limit =0 to upper limit = 6

Algorithm:

Step 1: input a,b,number of interval n

Step 2:h=(b-a)/n

Step 3:sum=f(a)+f(b)+4*f(a+h)

Step 4:sum=sum+4*f(a+i*h)+2*f(a+(i-1)*h)

Step 5:Display output=sum * h/3

Code:

 Output:

2014-11-26 17_14_31-

C code for bisection method

The Bisection Method is a numerical method for estimating the roots of a polynomial f(x). It is one of the simplest and most reliable but it is not the fastest method.
Problem: Here we have to find root for the polynomial x^3+x^2-1
Solution in C:

Algorithm:

  1. Start
  2. Read a1, b1, TOL
    *Here a1 and b1 are initial guesses
    TOL is the absolute error or tolerance i.e. the desired degree of accuracy*
  3. Compute: f1 = f(a1) and f3 = f(b1)
  4. If (f1*f3) > 0, then display initial guesses are wrong and goto step 11
    Otherwise continue.
  5. root = (a1 + b1)/2
  6. If [ (a1 – b1)/root ] < TOL , then display root and goto step 11
    * Here [ ] refers to the modulus sign. *
    or f(root)=0 then display root
  7. Else, f2 = f(root)
  8. If (f1*f2) < 0, then b1=root
  9. Else if (f2*f3)<0 then a1=root
  10. else goto step 5
    *Now the loop continues with new values.*
  11. Stop

Output:

2014-11-09 03_03_31-

Another Problem Solving Code:
Problem:
Here we have to find root for the polynomial x^3+x^2-1 upto 4D

Output:2014-11-27 12_22_54-