0% found this document useful (0 votes)
2 views

Numerical Methods 1

Uploaded by

John Doe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Numerical Methods 1

Uploaded by

John Doe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Numerical Methods

PRACTICAL CLASS
BY
ER. SHWETA SAH
Algorithm for Bisection Method
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 go to (8)
7. if |f(x2)| > e then go to (5) otherwise go to (8)
8. Display x2 as root.
9. Stop
Algorithm for Regula-Falsi Method

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 - ((x0-x1) * f(x0))/(f(x0) - f(x1))
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 go to (8)
7. if |f(x2)|>e then go to (5) otherwise go to (8)
8. Display x2 as root.
9. Stop
Algorithm for Newton Raphson
Method
1. Start
2. Define function as f(x)
3. Define first derivative of f(x) as g(x)
4. Input initial guess (x0), tolerable error (e) and maximum iteration (N)
5. Initialize iteration counter i = 1
6. If g(x0) = 0 then print "Mathematical Error" and goto (12) otherwise goto (7)
7. Calculate x1 = x0 - f(x0) / g(x0)
8. Increment iteration counter i = i + 1
9. If i >= N then print "Not Convergent" and go to (12) otherwise goto (10)
10.If |f(x1)| > e then set x0 = x1 and go to (6) otherwise goto (11)
11.Print root as x1
12.Stop
Algorithm for Secant Method
1. Start
2. Define function as f(x)
3. Input initial guesses (x0 and x1), tolerable error (e) and maximum iteration (N)
4. Initialize iteration counter i = 1
5. If f(x0) = f(x1) then print "Mathematical Error" and goto (11) otherwise goto (6)
6. Calculate x2 = x1 - (x1-x0) * f(x1) / ( f(x1) - f(x0) )
7. Increment iteration counter i = i + 1
8. If i >= N then print "Not Convergent" and go to (11) otherwise goto (9)
9. If |f(x2)| > e then set x0 = x1, x1 = x2 and go to (5) otherwise goto (10)
10.Print root as x2
11.Stop
Algorithm for Fixed point Iteration
Method
1. Start
2. Define function f(x)
3. Define function g(x) which is obtained from f(x)=0 such that x = g(x) and |g'(x) < 1|
4. Choose initial guess x0, Tolerable Error e and Maximum Iteration N
5. Initialize iteration counter: step = 1
6. Calculate x1 = g(x0)
7. Increment iteration counter: step = step + 1
8. If step > N then print "Not Convergent" and goto (12) otherwise goto (10)
9. Set x0 = x1 for next iteration
10.If |f(x1)| > e then go to step (6) otherwise goto step (11)
11.Display x1 as root.
12.Stop
Algorithm for Lagrange’s
Interpolation
1. Start
2. Read number of data (n)
3. Read data Xi and Yi for i=1 ton n
4. Read value of independent variables say xp whose corresponding value of dependent say yp is to be determined.
5. Initialize: yp = 0
6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i
6. Display value of yp as interpolated value.
7. Stop
Algorithm for Linear Regression
1. Start
2. Read Number of Data (n)
3. For i=1 to n:
Read Xi and Yi
Next i
4. Initialize:
sumX = 0
sumX2 = 0
sumY = 0
sumXY = 0
5. Calculate Required Sum
For i=1 to n:
sumX = sumX + Xi
sumX2 = sumX2 + Xi * Xi
sumY = sumY + Yi
sumXY = sumXY + Xi * Yi Next i
6. Calculate Required Constant a and b of y = a + bx:
b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
a = (sumY - b*sumX)/n
7. Display value of a and b
8. Stop
Algorithm for exponential
Regression
1. Start
2. Read Number of Data (n)
3. For i=1 to n: Read xi and yi Next i
4. Define:
Y=logy
5. Initialize:
sumx = 0
sumx2 = 0
sumy = 0
sumXY = 0
6. Calculate Required Sum
For i=1 to n:
sumx=sumx +x[i];
sumx2=sumx2 +x[i]*x[i];
sumy=sumy +Y[i];
sumxy=sumxy +x[i]*Y[i] Next i
7. Calculate Required Constant A and b of Y = A + bX:
A=((sumx2*sumy -sumx*sumxy)/(n*sumx2-sumx*sumx)
b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)

8. Transformation of A to
a: a = exp(A)
9. Display value of a and b
10. Stop
Derivatives Using Newton’s Forward
Difference Formula
1. Start
2. Read number of data (n)
3. Read data points for x and y:
For i = 0 to n-1
Read X and Y
i i,0

Next i
4. Read calculation point where derivative is required (xp)
5. Set variable flag to 0
6. Check whether given point is valid data point or not.
If it is valid point then get its position at variable index
For i = 0 to n-1
If |xp - X | < 0.0001
i

index = i
flag = 1
break from loop
End If
Next i
7. If given calculation point (xp) is not in x-data then terminate the process.
If flag = 0
Print "Invalid Calculation Point"
Exit
End If
8. Generate forward difference table
For i = 1 to n-1
For j = 0 to n-1-i
Y = Y
j,i
- Y
j+1,i-1 j,i-1

Next j
Next i
9. Calculate finite difference: h = X1 - X0
10.Set sum = 0 and sign = 1
11.Calculate sum of different terms in formula to find derivatives using Newton's forward difference formula:
For i = 1 to n-1-index
term = (Y ) / i
index, i
i

sum = sum + sign * term


sign = -sign Next i
12.Divide sum by finite difference (h) to get result
first_derivative = sum/h
13.Display value of first_derivative
14.Stop
Derivatives Using Newtons Backward
Difference Formula

1. Start
2. Read number of data (n)
3. Read data points for x and y:
For i = 0 to n-1
Read Xi and Yi,0
Next i
4. Read calculation point where derivative is required (xp)
5. Set variable flag to 0
6. Check whether given point is valid data point or not.
If it is valid point then get its position at variable index
For i = 0 to n-1
If |xp - Xi| < 0.0001
index = i
flag = 1
break from loop
End If
Next i
7. If given calculation point (xp) is not in
x-data then terminate the process.
If flag = 0
Print "Invalid Calculation Point"
Exit
End If
8. Generate backward difference table
For i = 1 to n-1
For j = n-1 to i (Step -1)
Yj,i = Yj,i-1 - Yj-1,i-1
Next j
Next i
9. Calculate finite difference: h = X1 - X0
10. Set sum = 0
11. Calculate sum of different terms in formula
to find derivatives using Newton's backward
difference formula:
For i = 1 to index
term = (Yindex, i)i / i
sum = sum + term
Next i
12. Divide sum by finite difference (h) to get result
first_derivative = sum/h
13. Display value of first_derivative
14. Stop
Trapezoid's rule

1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. Calculate: Integration value = Integration Value + 2* f(k)
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/2
12. Display Integration value as required answer
13. Stop
Simpson's 1/3 Rule

1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 4 * f(k)
End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/3
12. Display Integration value as required answer
13. Stop
Simpson's 3/8 Rule

1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 3 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise Integration Value = Integration Value + 3 * f(k)
End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size*3/8
12. Display Integration value as required answer
13. Stop
Euler's Method Algorithm (Ordinary Differential
Equation)

1. Start
2. Define function f(x,y)
3. Read values of initial condition(x0 and y0), number of steps (n) and calculation point (xn)
4. Calculate step size (h) = (xn - x0)/b
5. Set i=0
6. Loop yn = y0 + h * f(x0 + i*h, y0) y0 = yn i = i + 1 While i < n
7. Display yn as result
8. Stop
Ordinary Differential Equation Using Fourth
Order Runge Kutta (RK) Method Algorithm

Start
2. Define function f(x,y)
3. Read values of initial condition(x0 and y0), number of steps (n) and calculation point (xn)
4. Calculate step size (h) = (xn - x0)/n
5. Set i=0
6. Loop
k1 = h * f(x0, y0)
k2 = h * f(x0+h/2, y0+k1/2)
k3 = h * f(x0+h/2, y0+k2/2)
k4 = h * f(x0+h, y0+k3)
k = (k1+2*k2+2*k3+k4)/6
yn = y0 + k
i = i + 1
x0 = x0 + h
y0 = yn
While i < n
7. Display yn as result
8. Stop

You might also like