Numerical Methods
Algorithms & C
Program
Dr. Nirav Vyas – Associate Professor, Dept. of Science & Humanities, FoET
Atmiya University, INDIA
1. Bisection Method
2. Power Method
3. Lagrange’s Interpolation Method
Bisection Method
Algorithm & C Program
Bisection Method - Algorithm
1. Start
2. Define function f(x)
3. Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0
4. Choose pre-specified tolerable error e
5. Calculate new approximated root as x2 = (x0 + x1)/2
6. Calculate f(x0)f(x2)
a) if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2
b) if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1
c) if f(x0)f(x2) = 0 then goto (8)
7. if |f(x2)| > e then goto (5) otherwise goto (8)
8. Display x2 as root.
9. Stop
Bisection Method – C Program
▪ Program: Finding real roots of nonlinear equation using Bisection Method
▪ Header files
▪ User define function for nonlinear equation: x^3 – 9x + 1 = 0
▪ Declaration of variables
▪ Input from the user
▪ Checking of the input values
▪ Input from the user for tolerable error
▪ Setting the output screen format
▪ Start the looping structure until fc gets less then tolerable error
▪ Inside loop first find c value using bisection method & function of c
▪ Print on screen all values of step, a, b, c and fc
▪ Now to check whether c will replace a or b
▪ Increase the step size by 1
▪ Print the root of the equation
Power Method
Algorithm & C Program
Power Method - Algorithm
▪ 1. Start
▪ 2. Read Order of Matrix (n) and Tolerable Error (e)
▪ 3. Read Matrix A of Size n x n
▪ 4. Read Initial Guess Vector X of Size n x 1
▪ 5. Initialize: Lambda_Old = 1
▪ 6. Multiply: X_NEW = A * X
▪ 7. Replace X by X_NEW
▪ 8. Find Largest Element (Lamda_New) by Magnitude from X_NEW
▪ 9. Normalize or Divide X by Lamda_New
▪ 10. Display Lamda_New and X
▪ 11. If |Lambda_Old - Lamda_New| > e then set Lambda_Old = Lamda_New and goto
step (6) otherwise goto step (12)
▪ 12. Stop
Power Method – C Program
▪ Program: Write a C program to find Larges Eigen Value and Eigen Vector
using Power Method.
▪ Header files
▪ Universal Constant (maximum size of matrix)
▪ Declaration of variables
▪ Title and input from the user
▪ Reading Matrix from user (order n x n)
▪ Reading initial Guess Vector from user (order n x 1)
▪ Initializing Lambda_old
▪ Multiplication
▪ Replacing old eigen vector with new
▪ Finding largest numerical value from the vector
▪ Normalization – dividing each element of vector with the largest value
▪ Display step, eigen value and eigen vector
▪ Checking the accuracy (either repeat the process or stop )
Lagrange’s Interpolation
Method
Algorithm & C Program
Lagrange’s Interpolation Method - Algorithm
▪ 1. Start
▪ 2. Read number of data (n) ( how many pairs of (x,y) )
▪ 3. Read data and for
▪ 4. Read the value of whose corresponding value of is to be determined.
▪ 5. Initialize: = 0
▪ 6. Formula for Lagrange’s Interpolation: y= where
▪ 7. Display the value of as interpolated value.
▪ 8. Stop
Lagrange’s Interpolation Method – C Program
▪ Program: Write a C program to interpolate value of y for the given value of x
from the set of data given by the user using Lagrange’s interpolation formula.
▪ Header files
▪ Declaration of variables
▪ Read number of data (n) and read data and for
▪ Read the value of whose corresponding value of is to be determined
▪ Implementing Lagrange’s formula
▪ Display interpolated value y
Numerical Methods Algorithm and C Program

Numerical Methods Algorithm and C Program

  • 1.
    Numerical Methods Algorithms &C Program Dr. Nirav Vyas – Associate Professor, Dept. of Science & Humanities, FoET Atmiya University, INDIA
  • 2.
    1. Bisection Method 2.Power Method 3. Lagrange’s Interpolation Method
  • 3.
  • 4.
    Bisection Method -Algorithm 1. Start 2. Define function f(x) 3. Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0 4. Choose pre-specified tolerable error e 5. Calculate new approximated root as x2 = (x0 + x1)/2 6. Calculate f(x0)f(x2) a) if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2 b) if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1 c) if f(x0)f(x2) = 0 then goto (8) 7. if |f(x2)| > e then goto (5) otherwise goto (8) 8. Display x2 as root. 9. Stop
  • 5.
    Bisection Method –C Program ▪ Program: Finding real roots of nonlinear equation using Bisection Method ▪ Header files ▪ User define function for nonlinear equation: x^3 – 9x + 1 = 0
  • 6.
    ▪ Declaration ofvariables ▪ Input from the user
  • 7.
    ▪ Checking ofthe input values ▪ Input from the user for tolerable error ▪ Setting the output screen format
  • 8.
    ▪ Start thelooping structure until fc gets less then tolerable error ▪ Inside loop first find c value using bisection method & function of c ▪ Print on screen all values of step, a, b, c and fc
  • 9.
    ▪ Now tocheck whether c will replace a or b ▪ Increase the step size by 1 ▪ Print the root of the equation
  • 10.
  • 11.
    Power Method -Algorithm ▪ 1. Start ▪ 2. Read Order of Matrix (n) and Tolerable Error (e) ▪ 3. Read Matrix A of Size n x n ▪ 4. Read Initial Guess Vector X of Size n x 1 ▪ 5. Initialize: Lambda_Old = 1 ▪ 6. Multiply: X_NEW = A * X ▪ 7. Replace X by X_NEW ▪ 8. Find Largest Element (Lamda_New) by Magnitude from X_NEW ▪ 9. Normalize or Divide X by Lamda_New ▪ 10. Display Lamda_New and X ▪ 11. If |Lambda_Old - Lamda_New| > e then set Lambda_Old = Lamda_New and goto step (6) otherwise goto step (12) ▪ 12. Stop
  • 12.
    Power Method –C Program ▪ Program: Write a C program to find Larges Eigen Value and Eigen Vector using Power Method. ▪ Header files ▪ Universal Constant (maximum size of matrix) ▪ Declaration of variables
  • 13.
    ▪ Title andinput from the user ▪ Reading Matrix from user (order n x n)
  • 14.
    ▪ Reading initialGuess Vector from user (order n x 1) ▪ Initializing Lambda_old ▪ Multiplication
  • 15.
    ▪ Replacing oldeigen vector with new ▪ Finding largest numerical value from the vector ▪ Normalization – dividing each element of vector with the largest value
  • 16.
    ▪ Display step,eigen value and eigen vector ▪ Checking the accuracy (either repeat the process or stop )
  • 17.
  • 18.
    Lagrange’s Interpolation Method- Algorithm ▪ 1. Start ▪ 2. Read number of data (n) ( how many pairs of (x,y) ) ▪ 3. Read data and for ▪ 4. Read the value of whose corresponding value of is to be determined. ▪ 5. Initialize: = 0 ▪ 6. Formula for Lagrange’s Interpolation: y= where ▪ 7. Display the value of as interpolated value. ▪ 8. Stop
  • 19.
    Lagrange’s Interpolation Method– C Program ▪ Program: Write a C program to interpolate value of y for the given value of x from the set of data given by the user using Lagrange’s interpolation formula. ▪ Header files ▪ Declaration of variables
  • 20.
    ▪ Read numberof data (n) and read data and for ▪ Read the value of whose corresponding value of is to be determined
  • 21.
    ▪ Implementing Lagrange’sformula ▪ Display interpolated value y