Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Numerical Method Lab
M(CS)491
Syllabus
Assignments On Topics
Interpolation
Newton Forward Interpolation
Backward Forward Interpolation
Lagrange’s Interpolation
Newton divided difference
Numerical Integration
Trapezoidal Rule
Simpson’s 1/3 Rule
Simpson’s 3/8 Rule
Weddle’s Rule
Numerical solution of a system of
linear equation
Gauss elimination
Matrix Inversion, Gauss Seidal
Algebraic Equation
Bisection
Regular-falsi
Newton Raphson
Ordinary Differential Equation Euler’s method
Finding determinant of a matrix
Runga Kutta 2nd order
Sarrus’ Method
Upper Triangular Method
Assignments
Week 1:
1: Write a program in C to find the determinant of a 3 X 3 matrix using Sarrus’ Method.
2: Write a program in C to find addition, multiplication, subtraction and transpose of two matrices.
3: Write a program in C to find the determinant of an n X n matrix using Upper Triangular form.
4: Write a program in C to solve system of linear equation using Cramer’s rule.
Week 2:
5: Write a program in C to find the inverse of n X n matrix and find the unknowns by solving the linear
equations.
6: Write a program in C to find the solution of a system of linear equations using Gauss Elimination Method.
Week 3:
7: Write a program in C to implement Newton’s forward interpolation method.
8: Write a program in C to implement Newton’s backward interpolation method.
Week4:
9: Write a program in C to implement Lagrange’s interpolation method.
10: Write a program in C to implement Newton’s divided difference method.
Week 5:
11: Write a program in C to find the solution of system of linear equations using Gauss - Seidel method.
Week 6:
12: Write a program in C to implement Trapezoidal rule.
13: Write a program in C to implement Simpson’s 1/3 rule.
Week 7:
14: Write a program in C to implement Simpson’s 3/8 rule.
15: Write a program in C to implement Weddle rule.
Week 8:
16: Write a program in C to find the roots of a linear equation using Bisection Method.
Week 9:
17: Write a program in C to implement Euler’s equation and modified Euler’s modified equation.
Week 10:
18: Write a program in C to implement Regular-falsi method.
19: Write a program in C to implement Newton Raphson method.
Week 1
Assignment 1: Sarrus’ Method
Sarrus' rule or Sarrus' scheme is a method and a memorization scheme to
compute the determinant of a 3×3 matrix.
Consider a 3×3 matrix
then its determinant can be computed by the
following scheme:
Write out the first 2 columns of the matrix to the right of the 3rd column, so that you have 5 columns in a row. Then
add the products of the diagonals going from top to bottom (solid) and subtract the products of the diagonals going
from bottom to top (dashed). These yields:
Let det(A) be the determinant of the diagonal element from top to bottom(solid).
Let det(B) be the determinant of the diagonals going from bottom to top (dashed).
det (A) = a11a22a33 + a12a23a31 + a13a21a32
det (B) = a31a22a13 + a32a23a11 + a33a21a12
Therefore according to the rule of Sarrus Method,
det (M) = det(A) – det(B)
But for 2 X 2 matrix or n X n matrix (except 3X3 matrix) Sarrus method is not applicable.
Let us take an example for 2 X2 matrix:
M=
det (A) = (2*4) + (3*1)
= 8+3
= 11
det (B) = (3*1) + (4*2)
=11
But the determinant of this matrix is:
det (M) = (2*4) – (1*3)
= 8 – 3
= 5
Therefore,
det (M) = det(A) – det(B)
= 11 – 11
= 0
So we can conclude that Sarrus method is only applicable for 3 X 3 matrix not for any other ordered matrix.
[
2 3
] 1 4
Pseudo Code:
// a[i][j]: Value of coefficient matrix row wise
float detcalc(int n,int arr[n][n])
{
float a,b,d,t;
int i,j;
a=0.0;
b=0.0;
for(i=0 to n)
{
t=1.0;
for(j=0 to n)
t=t*arr[j][(j+i+n)%n];
a=a+t;
t=1.0;
for(j=0 to n)
t=t*arr[n-j-1][(j+i+n)%n];
b=b+t;
}
d=a-b;
// Return the values.
}
Example:
Enter the value of n : 3
Enter the value of the matrix:
Row 1. 1 2 3
Row 2. 4 5 6
Row 3. 7 8 9
Determinant of the matrix : 0
Assignment 2: Addition, Subtraction, Multiplication and Division
Pseudo Code:
void add()
{
// Variables for loops and data
for(i=0 to n-1)
for(j=0 to n-1)
c[i][j]=a[i][j]+b[i][j];
}
void sub(int n, int a[n][n], int b[n][n], int c[n][n])
{
//Variables for loops and data
for(i=0 to n-1)
for(j=0 to n-1)
c[i][j]=b[i][j]-a[i][j];
}
void mult(int n, int a1[n][n], int a2[n][n], int a3[n][n])
{
// Variables for loop and data.
for(i=0 to n-1)
{
for(j=0 to n-1)
{
m=0;
for(k=0 to n-1)
m=m+(a1[i][k]*a2[j][k]);
a3[i][j]=m;
}
}
}
void trans(int n, int a1[n][n], int a2[n][n])
{
int i,j;
for(i=0 to n-1)
{
for(j=0 to n-1)
a2[i][j]=a1[j][i];
}
}
Example:
Enter the order of the matrix: 3
Choice: 3
Enter the value for matrix 1:
Row 1. 2 3 5
Row 2. 4 1 2
Row 3. 6 3 1
Enter the value for matrix 2:
Row 1. 1 3 2
Row 2. 4 3 1
Row 3. 2 4 3
The product is:
24 35 22
12 23 15
20 31 18
Assignment 3: Upper Triangular Method
In the mathematical discipline of linear algebra, a triangular matrix is a special kind of square matrix. A
square matrix is called lower triangular if all the entries above the main diagonal are zero. Similarly, a
square matrix is called upper triangular if all the entries below the main diagonal are zero.
A triangular matrix U of the form
Written explicitly,
Pseudo Code:
// a[i][j]: Value of coefficient matrix row wise.
void uptri()
{
// Variable declarations.
for (i=0 to n)
{
for ( j=i+1 to n)
{
t=a1[j][i]/a1[i][i];
for (k=0 to n)
{
a1[j][k]=a1[j][k]-(a1[i][k]*t);
}
}
}
}
Example:
Enter the value of n: 3
Enter the value of the matrix:
Row 1. 5 7 2
Row 2. 1 3 8
Row 3. 1 1 1
Determinant of the matrix : 20
Assignment 4: Cramer’s Rule
Pseudo Code:
// a[i][j]: Values of coefficient matrix.
// b[i]: Values of right hand side constants.
float cramer()
{
// Variable declarations
for(i=0 to n)
{
for(j=i+1 to n)
{
t=a1[j][i]/a1[i][i];
for(k=0 to n)
{
a1[j][k]=a1[j][k]-(a1[i][k]*t);
}
}
}
t=1;
for(i=0 to n)
t=t*a1[i][i];
return t;
}
Example:
Enter the value of n: 3
Enter the value of the matrix:
Row 1. 6 5 2
Row 2. 1 3 1
Row 3. 1 2 3
X[0] = 3.000000 X[1] = 2.000000 X[2] = 1.000000
Week 2
Assignment 5: Inverse of a Matrix
In linear algebra, an n-by-n square matrix A is called invertible if there exists an n-by-n square matrix B such that
where, In denotes the n-by-n identity matrix and the multiplication used is ordinary matrix multiplication.
If this is the case, then the matrix B is uniquely determined by A and is called the inverse of A, denoted by A−1.
The adjugate of a matrix can be used to find the inverse of as follows:
If is an invertible matrix, then
Pseudo Code:
void invert()
{
// Declarations.
d = det(n,a); // det() finds the determinant
if(d==0)
// Non-Invertible Matrix.
// Calculating the co-factor matrix.
for(i=0 to n)
{
for(j=0 to n)
{
t=a1[i][j];
m=0;
for(k=0 to n)
{
if(k==i)
continue;
o=0;
for(l=0 to n)
{
if(l==j)
continue;
b[m][n]=a1[k][l];
n++;
}
m++;
}
a1[i][j]=pow(-1,i+j)*det(n-1,b); // det() finds the determinant
}
}
// Transposing the co-factor matrix to find adjoint matrix.
for(i=0 to n)
{
for(j=0 to n)
{
t=a1[i][j];
a1[i][j]=a1[j][i];
a1[j][i]=t;
}
}
// Print the matrix while dividing with determinant.
}
Example:
Enter the value of n-> 3
Enter the value of the matrix:
Row 1. 5 7 2
Row 2. 1 3 8
Row 3. 1 1 1
The inverse is:
-0.2500 -0.2500 2.5000
0.3500 0.1500 -1.9000
-0.1000 0.1000 0.8000
Assignment 6: Gauss Elimination Method
This is the elementary elimination method and it reduces the system of equations to an equivalent upper
triangular matrix, which can be solved by back substitution.
Pseudo Code:
void gauss()
{
//c[i]=right hand side constants
//a[i][j]=values row wise
for(k=0 to n-1)
{
for(i=k+1 to n)
{
for(j=k+1 to n)
a[i][j]=a[i][j]-(a[i][k]/a[k][k])*a[k][j];
c[i]=c[i]-(a[i][k]/a[k][k])*c[k];
}
}
x[n-1]=c[n-1]/a[n-1][n-1];
print(the solution is: n-1,x[n-1] );
for(k=0 to n-1)
{
i=n-k-2;
for(j=i+1 to n)
c[i]=c[i]-(a[i][j]*x[j]);
x[i]=c[i]/a[i][i];
print(solution : x[i]);
}
}
Example:
Input:
Enter the matrix value: 3
6 5 2 30
1 3 1 10
1 2 3 10
Output:
Original matrix is:
6.000000 5.000000 2.000000 30.000000
1.000000 3.000000 1.000000 10.000000
1.000000 2.000000 3.000000 10.000000
Combined Upper Triangular Matrix is:
6.000000 5.000000 2.000000 30.000000
-0.000000 2.166667 0.666667 5.000000
-0.000000 -0.000000 2.307693 2.307693
X[0] = 3.000000 X[1] = 2.000000 X[2] = 1.000000
Week 3
Assignment 7: Newton’s Forward Interpolation Method
This is an Nth
degree polynomial approximation formula to the function f(x). If y0, y1, y2, ……, n are the
values of y = f(x) corresponding to equidistant values of x =x0, x1, x2, ……, n, where xi – xi-1 = h, for i = 1, 2, , ….., , then y = y0 + u/! ∆ 0 + u(u- / !∆2
y0 + …………+ u u- …… u- n- / ! ∆n y0, where u = (x – x0)/h.
Pseudo Code:
Function NFI ()
Read n, x
For I = 1 to n by 1 do
Read x[i], y[i]
End for
If ((x < x[i] or (x > x[n]))
Print “Value lies out of boundary”
//Calculating p
p = (x – x [1]) / (x [2]-x [1])
// Forward diff table
For j = 1 to (n-1) by 1 do
{
For i =1 to (n - j) by 1 do
{
If (j=1) Then
d[i][j] = y [i+1] – y[i]
Else
d[i][1j] = d[i+1][j-1] – d[i][j-1]
}
}
// Applying Formula
Sum =y [1]
For I = 1 to (n-1) by 1 do
{
Prod = 1
For j =0 to (i-1) by 1 do
Prod = prod * (p-j)
m = fact(i)
Sum = sum + (d[1][i] * prod) / m
}
Print “Ans is”, Sum
End Function
Example:
Enter the value of n (No. of data pairs - 1):
7
Enter the initial values of x:
0
Enter the step size:
1
Enter the values of y
1 2 4 7 11 16 22 29
Enter the required no. of interpolated values of y:
6
Enter the 6 values of X for which values of y are required:
-0.4 0.8 3.5 5 6.3 7.7
The values of X and Y are: -0.400000 0.880000
The values of X and Y are: 0.800000 1.720000
The values of X and Y are: 3.500000 8.875000
The values of X and Y are: 5.000000 16.000000
The values of X and Y are: 6.300000 23.995003
The values of X and Y are: 7.700000 34.494999
Assignment 8: Newton’s Backward Interpolation
This is again an Nth
degree polynomial approximation formula to the function f(x). If , , , ……, are the values of y = f(x) corresponding to equidistant values of x =x0, x1, x2, ……, n, where xi – xi-1 = h, for i = 1, 2, 3,
….., , the = n + u/! ∆ n + u u+ / !∆2 yn + …………+ {u u+ …… u+ - } / ! ∆n
yn, where u = (x – xn)/h.
Pseudo Code:
Function NBI ()
Read n, x
For I = 1 to n by 1 do
Read x[i], y[i]
If ((x < x[i] or (x > x[n]))
Print “Value lies out of boundary”
//Calculating p
p = (x – x [1]) / (x [2]-x [1])
// Forward diff table
For j = 1 to (n-1) by 1 do
{
For i =1 to (n - j) by 1 do
{
If (j=1) Then
d[i][j] = y [i+1] – y[i]
Else
d[i][j] = d[i+1][j-1] – d[i][j-1]
}
}
// Applying Formula
Sum =y [n]
For I = 1 to (n-1) by 1 do
{
Prod = 1
For j =0 to (i-1) by 1 do
Prod = prod * (p+j)
m = fact(i)
Sum = sum + (d[n-1][i] * prod) / m
}
Print “Ans is”, Sum
End Function
Example:
Enter the value of n (No. of pairs - 1):
7
Enter the last value of x
7
Enter the step size h
1
Enter the values of y
1 2 4 7 11 16 22 29
Enter the required no. of interpolated values of y:
6
Enter the 6 values of X for which values of y are required :
-0.4 0.8 3.5 5.0 6.3 7.7
The values of X and Y are: -0.400000 0.879999
The values of X and Y are: 0.800000 1.720000
The values of X and Y are: 3.500000 8.875000
The values of X and Y are: 5.000000 16.000000
The values of X and Y are: 6.300000 23.995003
The values of X and Y are: 7.700000 34.494999
Week 4
Assignment 8: Lagrange’s Interpolation
This is again an Nth
degree polynomial approximation formula to the function f(x), which is known at
discrete points xi, i = 0, 1, 2 . . . Nth
. The formula can be derived from the Vandermond’s determinant but a much
simpler way of deriving this is from Newton's divided difference formula. If f(x) is approximated with an Nth
degree
polynomial then the Nth
divided difference of f(x) constant and (N+1)th
divided difference is zero. That is
f [x0, x1, . . . xn, x] = 0
From the second property of divided difference we can write
f0
+
fn fx
= 0 + . . . +
(x0 - x1) . . . (x0 - xn)(x0 - x) (xn - x0) . . . (xn - xn-1)(xn - x) (x - x0) . . . (x - xn)
Or
(x - x1) . . . (x - xn) (x - x0) . . . (x - xn-1)
f(x) = f0 + . . . + fn
(x0 - x1) . . . (x0 - xn) (xn - x0) . . . (xn - xn-1)
n
(
n
)fi
| | x - xj
j = 0 (xi - xj)
i = 0 j 1
Pseudo Code:
void langrange()
{
//X[K]=Value of X for interpolation(f(x))
//l= No of interpolated data
for(k=0 to n)
{
for(i=0 to n+1)
{
a[i]=1;
for(j=0 to n+1)
{
if(i!=j)
a[i]=a[i]*(X[k]-x[j])/(x[i]-x[j]);
}
}
Y[k]=0;
for(i=0 to n+1)
Y[k]=Y[k]+a[i]*y[i];
print(The values of X and Y are : X[k],Y[k]);
}
}
Example:
Enter the no. of points: 4
Enter the values of x at which y is required: 1.1
Enter the values of x and y:
1 1
1.2 1.0954
1.3 1.1402
1.5 1.2247
At x = 1.100000, y = 1.048713
Assignment 10: Newton’s Divided Difference Method
Newton’s Divided Difference Formula eliminates the drawback of recalculation and re computation of
interpolation coefficients by using Newton’s general interpolation formula which uses divided differences .
Before going through the source code for Newton Divided Difference in C, here’s a brief explanation of what
divided differences are with the formula for divided differences.
If (x0, y0), (x1, y1), (x2, y2 …… e gi e poi ts, the the first di ided differe e for the argu e ts 0, x1 is
defined as: [x0, x1] = (y1-y0)/(x1-x0). And, similarly, [x1, x2] = (y2-y1)/(x2-x1), and after that [x2, x3] = (y3-y2)/(x3-x2), and
so on.
f(x) = f [x0] + (x - x0) f [x0, x1] + (x - x0) (x - x1) f [x0, x1, x2] + . . . + (x - x0) (x - x1) . . . (x - xk-1) f [x0, x1, . . ., xk].
The second divided difference is defined as: [x0, x1, x2] = ([x1, x2] – [x0, x1])/(x2-x0). This goes on in similar
fashio for the third, fourth …. a d th di ided differe es. Based o these for ulas, t o asi properties of Newton’s Divided Difference method can be outlined as given below:
The divided differences are symmetrical in their arguments i.e. independent of the order of the arguments.
The nth divided differences of a polynomial of the nth degree are constant.
Pseudo Code:
void newton_divided_difference()
{
//X[K]=Value of X for interpolation(f(x))
//l= No of interpolated data
for(k=0 to l)
{
for(j=0 to n+1)
d[0][j]=y[j];
for(i=1 to n+1)
{
for(j=0 to n+1-i)
d[i][j]=(d[i-1][j+1]-d[i-1][j])/(x[i+j]-x[j]);
}
Y[k]=y[0];
p=1;
for(i=0 to n)
{
p=p*(X[k]-x[i]);
Y[k]=Y[k]+p*d[i+1][0];
}
print(The values of X and Y are: X[k], Y[k]);
}
}
Example:
Enter the number of observations:
5
Enter the different values of x:
5 7 11 13 17
The corresponding values of y are:
150 392 1452 2366 5202
Enter the value of ‘k’ in f[k] you want to evaluate:
9
The value of f(9) = 810
Week 5
Assignment 11: Gauss Seidel Method
This iterative method starts from an approximation to the true solution and, if convergent, derives a
sequence of closer approximations, the cycle of computations being repeated till the required accuracy is
obtained. In general one should prefer a direct method for the solution of a linear system, but in the case of
matrices with a large number of zero elements, it will be advantageous to use iterative methods, which preserve
these elements.
For Example:
4x1 + x2 - x3 = 3
x1 = -1/4 x2 + 1/4 x3 + 3/4
2x1 + 7 x2 + x3 = 19 <=> x2 = -2/7 x1 - 1/7 x3 + 19/7
x1 - 3 x2 +12 x3 = 31 x3 = -1/12 x1 + 1/4 x2 + 31/12
Solution:
=
0 -1/4 1/4
x1
-2/7 0 -1/7 x2
-1/12 1/4 0 x3
The iteration formulas are
<=>
x1(k+1) =
-1/4 x2(k) + 1/4
x3(k)
+ 3/4
x2
(k+1) = -2/7 x1(k+1) - 1/7 x3
(k) + 19/7
x3
(k+1) = -1/12 x1(k+1) + 1/4 x2
(k+1) + 31/12
The difference between the Gauss-Seidel method is that here we use the coordinates x1(k),...,xi-1
(k) of x(k) already
known to compute its ith
coordinate xi(k). If we start from x1
(0) = x2(0) = x3
(0) = 0 and apply the iteration formulas,
we obtain
k x1(k) x2
(k) x3(k)
0 0 0 0
+
3/4
= G + 19/7
31/12
1 0,75 2,50 3,15
2 0,91 2,00 3,01
3 1,00 2,00 3,00
4 1,00 2,00 3,00
The exact solution is: x1 = 1, x2 = 2, x3 = 3.
Pseudo Code:
void diagdom()
{
// Declarations
for(i=0 to n)
{
m[i]=max(n,a[i]);
if(m[i]==n+1)
//Gauss-Siedel not applicable
}
for(i=0 to n)
{
k=0;
for(j=0 to n)
if(m[j]==i)
k++;
if(k!=1)
// Gauss-Siedel not applicable
}
// Gauss-Siedel applicable
// The next part is the conversion into a diagonally dominant matrix.
for(i=0 to n)
m[i]=max(n,b[i]);
for(i=0 to n)
{
for(j=i to n)
{
if(m[j]!=i)
continue;
else
{
t=m[j];
m[j]=m[i];
m[i]=t;
t2=c[j];
c[j]=c[i];
c[i]=t2;
for(k=0 to n)
{
t1[k]=a[i][k];
a[i][k]=a[j][k];
a[j][k]=t1[k];
}
}
}
}
// Solution Part :
for(i=0 to n)
x[i]=0.0;
for(k=0 to k)
{
for(i=0 to n)
{
s=0;
for(j=0 to n)
{
if(i==j)
continue;
else
s=s+(a[i][j]*x[j]);
}
x[i]=(c[i]-s)/a[i][i];
}
}
// Print the results.
}
Week 6
Assignment 12: Trapezoidal Rule
In mathematics, and more specifically in numerical analysis, the trapezoidal rule, also known as
the trapezoid rule or trapezium rule, is a technique for approximating the definite integral.
The trapezoidal rule works by approximating the region under the graph of the function f(x) as
a trapezoid and calculating its area. The trapezoidal rule is to find the exact value of a definite
integral using a numerical method. This rule is mainly based on the Newton -Cotes formula which
states that one can find the exact value of the integral as an nth order polynomial.
When n = 1 according to Trapezoidal rule, the area under the linear polynomial is stated as, xn
f(x) dx = h/2 ( f0 + f1 + . . . + fn ) x0
Pseudo Code:
void trapezoidal()
{
//h=step size
h=x[1]-x[0];
n=n-1;
for(i=0 to n)
{
if(k==0)
{
sum = sum + y[i];
k=1;
}
else
sum = sum + 2 * y[i];
}
sum = sum + y[i];
sum = sum * (h/2);
print(I =sum);
}
Example:
Enter lower & upper limit of x
0 6
Enter number of intervals (N)
6
Area = 2.02133
Assignment 13: Simpson’s 1/3 Rule.
This rule is obtained by putting n = 2, in the general eqn.(3.1) i.e. by replacing the curve f(x) by 2 arcs of
second degree polynomial or parabolas, or quadratic and all differences higher than 2 will become zero.
here ∆ 0 = y1 –y0 a d ∆2y0 = y2 -2y1 =y0)
Hence,
(This is the formula for 1st step from x0 to x2 of (2.h) step width, having 2
strips of each of width h)
Similarly for 2nd step consider points (x2,y2), (x3,y3) and (x4,y4).
(This is the formula for 2nd step from x2 to x4 of (2.h) step width, having 2
strips of each of width h)
For the last step,
(This is the formula for last step from xn-2 to xn of (2.h) step width, having 2
strips of each of width h)
Summing up, we obtain the Simpson’s 1/3rd Rule.
Pseudo Code:
void simp1by3(){
//h=step size
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1 to n) {
if(k==0) {
sum = sum + 4 * y[i];
k=1;
}
else {
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
print(I = sum);
}
Example:
Enter lower & upper limit of x : 0 6
Enter no. of intervals 6
Area = 1.9587
x2
y.dx = 2h [y0 + ∆y0 + (1/6) ∆2 y0 ] x0
x2
y.dx = h/3 (y0 + 4y1 +y2) x0
x4
y.dx = h/3 (y2 + 4y3 +y4)
x2
xn
y.dx = h/3 (yn-2 + 4yn-1 +yn)
xn-2
Week 7
Assignment 14: Simpson’s 3/8 Rule.
Simpson’s 3/8 rule approximates the function within the subinterval [xi , xi+3] using a quartic.
The Taylor series expansion is performed about xi+3/2 to obtain
f(x) = fi+3/ + − i+3/2)f0 + i+ / + − i+3/ f i+ / + 6 − i+3/2) 3 f 000 i+3/2 + 1 24 ( − i+3/2)
4 f ( iv)i+3/ + O − i+3/2) 5 .
Integrating this function in a similar manner to that used for the 1/3 rule yields
f(x) dx = 3h/8(f0 + 3f1 + 3f2 + 2f3 + 3f4 + 3f5 + . . . + 2f − + 3f − + 3f − + fn)
Pseudo Code:
void sim3by8()
{
//h=step size
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1 to n)
{
if(k==0 || l==0)
{
sum = sum + 3 * y[i];
if(k==1)
l=1;
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
l=0;
}
}
sum = sum + y[i];
sum = sum * (3*h/8);
print( I = sum);
}
Example:
Enter a, b = 0 6
Enter number of subintervals: 6
Value of integral is 1.357081
Assignment 15: Weddle’s Rule
Putting n = 6, in the general equation,
I = h[ + / ∆ + { - / } ∆ + { - / }∆ + -----------], we observe that all differences higher than the six degree will become zero, i. e. by replacing the curve f(x) by
six degree polynomial. We obtain,
x6
y.dx = (3h/10) [y0+5y1+y2+6y3+y4+5y5+y6 ] x0
(This is the formula for 1st
step from x0 to x6 of (6.h) step width, having 6 strips of each of width h )
The arc is divided into six strips of equal width h. Hence number of strips will have to be a multiple of six
for Weddle’s rule. This is seven point formula or six strip formula. [Here, step width = 6h]
Pseudo Code:
void weddle()
{
//h=step size
h=x[1]-x[0];
n=n-1;
sum = y[0] + y[n];
for(i=1 to n)
{
if((i%6==1) || (i%6==5))
sum = sum + 5* y[i];
else if((i%6==3))
sum=sum+6*y[i];
else
sum = sum + y[i];
}
sum = sum * (3*h/10);
print( I = sum);
}
Example:
Enter lower & upper limit of x 0 6
Enter number of steps(N): 12
Area = 1.945912
Week 8
Assignment 16: Bisection Method
In Engineering practice, it is necessary to find out the roots
of equations in the form f(x) = 0. If it is a quadratic / cubic /
biquadrate expressions, then the formulae are available to find
out the roots. But, when it is polynomial of higher degree or an
expression involving the transcendental functions or
combinations of all, such as, 1+ex, 1+sinx+4x, x tan x-ex, x2-ex etc.
For this, algebraic methods are not available, hence to find out
the roots, we have to try some other approximate methods.
These approximate methods are called as Numerical Methods.
Pseudo Code:
float res()
{
// Calculation value of f(x) for some value of x
while(i<n)
{
r = r + c[i] * pow(x,e[i]);
i++;
}
return r;
}
void bisec()
{
// Declarations and Inputs
while(1)
{
if(res(n,c,coeff,exp)*res(n,d,coeff,exp)>=0)
{
c++;
d++;
}
else
break;
}
switch(ch)
{
case 1 : // Finding root based on number of iterations.
for(i=0;i<it;i++) {
m=(c+d)/2.0;
mr = res(n,m,coeff,exp);
cr = res(n,c,coeff,exp);
dr = res(n,d,coeff,exp);
if((mr*dr)<0)
c=m;
else if((cr*mr)<0)
d=m;
else if(mr==0.000000)
break;
}
break;
case 2 : // Finding root based on accuracy of result.
for(i=0;i<500;i++) {
m=(c+d)/2.0;
mr = res(n,m,coeff,exp);
cr = res(n,c,coeff,exp);
dr = res(n,d,coeff,exp);
if((mr*dr)<0)
c=m;
else if((cr*mr)<0)
d=m;
else if(mr==0.000000)
break;
if((mr<0.000001) && (mr>-0.000001))
break;
}
break;
case 3 : // Finding root based on difference of the limits.
for(i=0;i<100;i++) {
m=(c+d)/2.0;
mr = res(n,m,coeff,exp);
cr = res(n,c,coeff,exp);
dr = res(n,d,coeff,exp);
if((mr*dr)<0)
c=m;
else if((cr*mr)<0)
d=m;
else if(mr==0.000000)
break;
if((d-c)<0.00001)
break;
}
break;
default: printf(" Please try to remain within the limits of this program and only choose among the options
provided to you. \n\n");
}
}
Example:
Enter number of terms 3
Exponent of term 1 - 3
Co-efficient of term 1 - 1
Exponent of term 2 – 1
Co-efficient of term 2 - -3
Exponent of term 3 – 0
Co-efficient of term 3 – 1.06
Choice – 1
Number of iterations – 15
Root – 0.370
Week 9
Assignment 17: Euler’s Equation.
Let dy/dx = y’ = f(x,y), with y = y0 at x = x0. Using the formula derived from the Taylor’s series as follows,
y(x+h) = y(x) + hy’(x) + (h2/2!).f (x)+-------------------
If the step length h is taken sufficiently small, the terms containing h2, h3 etc. in Taylor’s Series are still
smaller and hence can be neglected to give approximate solution. (Here, y’(x) is replaced by y’=f(x,y))
Y(x + h) = y(x) + h.f(x, y)
In the first step, y(x0 +h) = y(x0) + h.f(x0,y0), where x1= x0+h and y(x0) = y0 y(x0 + h) = y(x0) + h.f(x0,y0) can be written
as y(x1) = y(x0) + h.f(x0,y0) similarly, y2 = y2 + h.f(x1,y1) yn+1 = yn + h.f(xn + yn) Thus the successesive values of
y1,y2,y3,------at x1,x2,x3,------- are obtained from above equations.
Pseudo Code:
void euler()
{
// Declarations.
printf(“\n Enter starting value of x & y i. e. x0, y0”);
scanf(“%f%f”,&x0, &y0);
printf(“\nEnter the value of x at which y is required “);
scanf(“%f”, &xn);
printf(“\n Enter the step size “);
scanf(“%f”,&h);
do
{
printf(“\nThe value of y = %f at x = %f”, y0, x0);
y1 = y0 + h*slope(x0, y0);
x0 = x0 + h;
y0 = y1;
}while(x0<=xn);
}
Example:
Enter starting value of x & y i. e. x0, y0
0 1
Enter the value of x at which y is required
0.1
Enter the step size 0.02
x 0 0.02 0.04 0.06 0.08 0.1
y 1 1.02 1.039231 1.057748 1.075601 1.092832
Week 9
Assignment 18: Regular Falsi Method
- fx - x = 𝑓 -f - x In this method initial interval is chosen such that, f(x0)*f(x1)<0, where x0 and x1 are
two points in which root lies. Now equation of the chord joining the two points, [x0,f(x0)] and [x1,f(x1)] is,
Pseudo Code:
void regularfalsi()
{
// Declarations.
printf(“\n Enter accuracy”);
scanf(“%f”,&e);
do
{
Printf(“\n Enter interval x0,x1”);
Scanf(“%f%f”,&x0,&x1);
}while(f(x0)*f(x1)>0.0);
k=0;
do
{
x2 =x0 – ((f(x0)*(x1-x0))/(f(x1)-f(x0))); // f(x) = tan(x)+tanh(x)
printf(“\nI=%d x0=%f x1=%f x2=%f f(x2)=%f f(x0)=%f
f(x1)=%f”,k+1,x0,x1,x2,f(x2),f(x0),f(x1));
if (f(x0)*f(x2)<0.0)
x1=x2;
else
x0=x2;
k=k+1;
}while(fabs(f(x2))>e);
printf(“\n Root of the equation is %f”, x2);
printf(“\n Actual no. of iteration required to achieve desired accuracy, %d”, k);
getch();
}
Output:
Enter accuracy: 0.0001
Enter interval x0, x1: 1 2
Root of the equation is 1.796297
Actual no. of iterations required to achieve desired accuracy 5
Assignment 19: Newton Raphson Method
In this method the real root of the equation f(x) = 0 can be computed rapidly, when the derivative of f(x) can
be easily found and is a simple expression. When an approximate value of a real root of an equation is known,
a closer approximation to the root can be obtained by an iterative process, as explained below:
Let x0 be an approximate value of a root of the equation f(x) = 0.
Let x1 be the exact root closer to x0, so that x1 = x0 + h, where h is small.
Since x1 is the exact root of f(x) = 0, we have f(x1) = 0, i.e., f(x0 + h) = 0
i.e.,
} } \ x rSub { size 8{ } } \ + . . . = } { f 𝑥 + ℎ! f ′ 𝑥 + ℎ! 𝑓 , by Taylor’s theorem
Since h is small, h2 and higher powers of h may be omitted.
Hence 𝑓 𝑥 + hf ′ 𝑥 = , approximately ∴ ℎ = − 𝑓𝑓′ approximately
∴ 𝑥 = 𝑥 + ℎ = 𝑥 − 𝑓𝑓′ approximately.
The value of x1 thus obtained will be a closer approximation to the actual root of f(x) = 0 than x0.
Taking x1 as an approximate value of the root, a still better approximation x2 can be obtained by using the
formula
𝑥 = 𝑥 − 𝑓𝑓′
The iterative process is continued until we get the required accuracy, i.e., until |xn + 1 – xn| is less than a
prescribed small value.
The iterative formula
𝑥𝑛+ = 𝑥𝑛 − 𝑓 𝑛𝑓′ 𝑛 is called the Newton-Raphson formula.
Pseudo Code:
newtonraphson()
{
// Declarations.
printf(“\n Enter accuracy”);
scanf(“%f”,&e);
printf(“\n Enter initial guess x0”);
scanf(“%f”,&x[0]);
k=0;
do
{
x[k+1]=x[k]-(f(x[k])/(df(x[k])));
k=k+1;
}while(fabs(x[k]-x[k-1])>=e);
printf(“\n Root of the equation is %f”,x[k];
printf(“\n Actual no. of iterations required=%d”,k);
getch();
}
Example:
Enter accuracy 0.0001
Enter initial guess for the value of x0: 1.5
Computing Root….
Root of the equation is 1.7963219
Actual no. of iterations required to achieve desired accuracy 5