You are on page 1of 14

1

Chapter 1
NUMERICAL METHOD
Numerical method is an approach for solving complex mathematical problems using basic
arithmetic operation i.e. addition, subtraction, multiplication, division. Thus the numerical
method is the computational techniques which involves only a set of basic arithmetic operation
for solving any complex mathematical problem
Its major concern is to provide efficient and computer oriented solution of any physical problem

Application of numerical method


 Data analysis :numerical linear algebra is important for data analysis
 In the study of planets and stars
 Simulating living cells for medicine and biology
 Circuit simulation and analysis
 Numerical interpolation and extrapolation
 Solving equations and system of equations that describe physical system
 Numerical integration and differentiation
 In almost all Software tools that are used in science and engineering
 Structural analysis in civil engineering
Importance of computer programming in numerical method
 Fast calculation
 Less prone to errors
 Single program can be used to solve number of same class of problem
Equations and roots of equations
 Problems in science and engineering which are expressed in the combination of
dependent and independent variables
 A wide variety of problems in science and engineering can be formulated into equations
of the form f(x)=0,where x and f(x) may be real, complex or vector equations
 The roots of the equations are the values of x which satisfy the given equations
 The roots of equation are also known as zeros of the equations since the function
becomes zero at these values
Types of equations
1. Algebraic equations
2. Transcendental equations
3. Differential equations
4. Integral equations
Algebraic equations
Equations which involves only algebraic expression is known as algebraic equations.
It can be expressed in the form of
fnyn +fn-1yn-1 +fn-2yn-2+…………………. f0y0=0
where fi is an ith order polynomial in x an yi can be any constant or any order of y
eg,3x+5y-21,x3-xy-3y2=0
a) Linear equations:
An algebraic equations of degree one
b) Polynomial equation:
It is a simple class of algebraic equations which are represented as follows
anxn+an-1xn-1+……a1x+a0=0
It is nth degree polynomial which has n roots and roots may be
i) Real and different
Prepared by Ajay Lama
2

ii) Real and repeated


iii) Complex
 Complex root always appear in pair
 If n is odd there must be at least one real root and remaining roots may be real or
complex
Transcendental equations
An equations containing trigonometric, exponential and logarithmic as well as algebraic terms
are called transcendental equation.
Eg, 2sinx-x=0, exsinx-x/2=0 etc

Errors
i) Inherent errors:
 Errors which are already present in the statement of a problem before its solution are
called inherent errors.
 Arises either due to the given data being approximate or due to limitation of
mathematical tables, calculations
 Can be minimized by taking better data or by using precision computing aids
ii) Round off errors:
Round off errors occurs when a fixed number of digits are used to represent exact numbers.
Such as we represent the value of pi by 3.1416. There are two ways of rounding off a
number
a) Chopping
In chopping extra digits are dropped. Suppose we want to store a number 23.45678 in
computer with a fixed word length of four digits, then the number 23.45678 will be
stored as 23.45 and the digits 678 will be dropped

b) Symmetric rounding
in symmetric round off, the last retained significant digit is round up by 1 if the first
discarded digit is larger than or equal to 5,otherwise the last retained digit is unchanged
for example the number 23.45678 would become 23.46

iii) Truncation errors:


 Caused by using approximate results or on replacing an infinite process by finite one.
 ex=1+x/1!+x2/2!+x3/3!+x4/4!+… is replaced by ex=1+x/1!+x2/2!+x3/3! Which result
truncation errors
 series 6+6/10+6/100+6/1000+6/10000+6/100000+……..=6.6666666666666
it we take 6.66666 truncation errors
iv) Absolute errors and relative errors and percentage errors
 If X is true value and X’ is approximate value then
Absolute error(Ea)=|X-X’| and
Relative errors(Er)=|(X-X’)/X|
Percentage errors =|(X-X’)/X|*100%
 Relative errors and percentage errors are unit less where as absolute errors has unit
Taylor’s series
Different form of Taylor’s series are
f(x)=f(a)+f’(a)(x-a)/1!+f’’(a)(x-a)2/2!+f’’’(x)(x-a)3/3!+……
f(a+h)=f(a)+f’(a)h/1!+f’’(a)h2/2!+f’’’(a)h3/3!+…… (x-a=h)
f(x+h)=f(x)+f’(x)h/1!+f’’(x)h2/2!+f’’’(x)h3/3 ………….
Prepared by Ajay Lama
3

CENTRAL DIFFERENCE INTERPOLATION FORMULA


Stirling’s formula is given by
xi yi ∆yi ∆2yi ∆3yi ∆4yi ∆5yi ∆6yi
x0-3h y-3
∆y-3
x0-2h y-2 ∆2y-3
∆y-2 ∆3y-3
x0-h y-1 ∆2y-2 ∆4y-3
∆y-1 ∆ y-2
3
∆5y-3
x0 y0 ∆2y-1 ∆4y-2 ∆6y-3
∆y0 ∆ y-1
3
∆ y-2
5

x0+h y1 ∆ y0
2
∆ y-1
4

∆y1 ∆3y0
x0+2h y2 ∆2y1
∆y2
x0+3h y3
yp=y0+p + ∆2y-1 + + ∆4y-2 +
+….
bessel’s formula is given by
xi yi ∆yi ∆2yi ∆3yi ∆4yi ∆5yi ∆6yi ∆7yi
x0-3h y-3
∆y-3
x0-2h y-2 ∆2y-3
∆y-2 ∆3y-3
x0-h y-1 ∆ y-2
2
∆4y-3
∆y-1 ∆3y-2 ∆5y-3
x0 y0 ∆ y-1
2
∆ y-2
4
∆6y-3
∆y0 ∆3y-1 ∆5y-2 ∆7y-3
x0+h y1 ∆ y02
∆ y-1
4
∆ y-2
6

∆y1 ∆ y0
3
∆ y-1
5

x0+2h y2 ∆2y1 ∆4y0


∆y2 ∆3y1
x0+3h y3 ∆2y2
∆y3
x0+4h Y4
yp=y0+p∆y0 + + ∆3y-1

+ ( )+ ∆5y-2

+ ( )+ ∆7y-3 …..……..

Prepared by Ajay Lama


4

Q) employ stirling’s formula to compute y(12.2) from the following table


x 10 11 12 13 14
y 0.23967 0.28060 0.31788 0.35209 0.38368
Taking origin x0=12,p=(x-x0)/h=(12.2-12)/1=0.2
Central difference table is given as
xi yi 1st difference 2nd difference 3rd difference 4rth difference
10 0.23967
0.04093
11 0.28060 -0.00365
0.03728 0.00053
12 (x0 ) 0.31788 (y0 ) -0.00312 0.00007
0.03421(∆y0) -0.0006
13 0.35209 -0.00252 (∆2y0)
0.03159
14 0.38368
We have ,yp=y0+p + ∆2y-1 + + ∆4y-2
yp=0.31788 +0.2 + -
0.00312)+ + (0.00007)
=0.32497
Q)Apply bessel’s formula to obtain y(25) given
y(20)=2854,y(24)=3162,y(28)=3544,y(32)=3992
Taking x0=24 ,p=(x-x0)/h=(25-24)/4=1/4=.25
Central difference table is given as
xi yi 1st difference 2nd difference 3rd difference
20 2854
308
24(x0 ) 3162(y0 ) 74
382(∆y0) -8
28 3544 66(∆ y0)
2

448
32 3992
We have
yp=y0+p∆y0 + + ∆3y-1

yp=3162+0.25*382+ + (-8)
=3250.875
Numerical differentiation formula
a)two point formula
f’(x)= or y’i=(yi+1-yi) /h (forward difference formula)
f’(x)= or y’i=(yi-yi-1)/h (backward difference formula)
f’(x)= or y’i=(yi+1-yi-1) /2h (central difference formula)
b)three point formula
f’(x)= ( forward difference formula)

Prepared by Ajay Lama


5

f’(x)= ( backward difference formula)


f’(x)= (central difference formula)
c)second order differential formula
f’’(x)= or y’’i=(yi+1-2yi+ yi-1 )/h2 (central)
f’’(x)= (forward)
f’’(x)= (backward)
Shooting method (solution of boundary value problem)
In this method, the given boundary value problem is first converted into an equivalent initial
value problem and then solved by using any method (RK-1,RK-2.RK-4)
Consider the equation y’’=g(x,y,y’)……..(1), with y(a)=A,y(b)=B
Let , equation 1 becomes
z’= g(x,y,z) and y’=z
in order to slove this set as an initial value problem, we need two condition at x=a but we have
only y(a)=A so let y’(a)=z(a)=M1 (M1 represent slope at x=a)
now we have y’=z=f(x,y,z)and z’=g(x,y,z) with y(a)=A and y’(a)=z(a)=M1
These can be solve for y and z using any method using step of h
now let estimated value of y(b) be B1, if B1=B then we obtained required solution if B1≠B then
we obtain the solution with another guess say z(a)=M2 and new estimated value be B2

If B2 is also not equal to B, then process may be continued until we obtain the correct estimate of
y(b) however the procedure can be accelerated by using and improved guess for z(a) M3 as given
by following formula

Or M3=M2+ (B-B2)
Here M1,M2,B1,B2,B all are known so we can easily calculate M3 with this z(a)=M3 we can
obtain the solution of y
Summary: step1 let z(a)=M1 and find y(b) (let y(b)=B1)
Step2 let z(a)=M2 and find y(b) (let y(b)=B2)
Step3 calulate z(a)=M3=M2+ (B-B2) and find y(b) with z(a)=M3
Prepared by Ajay Lama
6

Q) Using shooting method, solve the equation y’’=6x with y(1)=2 and y(2)=9 by taking step
size 0.5 and with the help of modified eulers method
Sol n, Let y’=z then y’’=z’ =6x
Now y’=f(x,y,z)=z and z’=g(x,y,z)=6x

Step1: x0=1,y0=2,z0=M1=2(guess)
Solution for y (m=y’=f(x,y,x)=z) Solution for z (l=z’=g(x,y,z)=6x)
m1=f(x0,y0,z0)=2 l1=g(x0,y0,z0)=6
m2=f(x0+h,y0+m1*h,z0+l1*h)=5 l2=g(x0+h,y0+m1*h,z0+l1*h)=9
m=(m1+m2)/2=3.5 l=(l1+l2)/2=7.5
y1=y(1.5)=y0+m*h=3.75 z1=z(1.5)=z0+l*h=5.75
m1=f(x1,y1,z1)=5.75 l1=g(x1,y1,z1)=9
m2=f(x1+h,y1+m1*h,z1+l1*h)=10.25 l2=g(x1+h,y1+m1*h,z1+l1*h)=12
m=(m1+m2)/2=8 l=(l1+l2)/2=10.5
y2=y(2)=y1+m*h=7.75 z2=z(2)=z1+l*h
Therefore B1=7.75(<9)

Step2: x0=1,y0=2,z0=M2=4(guess)
Solution for y (m=y’=f(x,y,x)=z) Solution for z (l=z’=g(x,y,z)=6x)
m1=f(x0,y0,z0)=4 l1=g(x0,y0,z0)=6
m2=f(x0+h,y0+m1*h,z0+l1*h)=7 l2=g(x0+h,y0+m1*h,z0+l1*h)=9
m=(m1+m2)/2=5.5 l=(l1+l2)/2=7.5
y1=y(1.5)=y0+m*h=4.75 z1=z(1.5)=z0+l*h=7.75
m1=f(x1,y1,z1)=7.75 l1=g(x1,y1,z1)=9
m2=f(x1+h,y1+m1*h,z1+l1*h)=12.25 l2=g(x1+h,y1+m1*h,z1+l1*h)=12
m=(m1+m2)/2=10 l=(l1+l2)/2=10.5
y2=y(2)=y1+m*h=9.75 z2=z(2)=z1+l*h
Therefore B2=9.75(>9)

Step3: Now M1=2, B1=7.75 M2=4,B2=9.75 and B=9


M3=M1+ (B-B1)=3.75
x0=1,y0=2,z0=3.25
Solution for y (m=y’=f(x,y,x)=z) Solution for z (l=z’=g(x,y,z)=6x)
m1=f(x0,y0,z0)=3.75 l1=g(x0,y0,z0)=6
m2=f(x0+h,y0+m1*h,z0+l1*h)=6.25 l2=g(x0+h,y0+m1*h,z0+l1*h)=9
m=(m1+m2)/2=4.75 l=(l1+l2)/2=7.5
y1=y(1.5)=y0+m*h=4.375 z1=z(1.5)=z0+l*h=7
m1=f(x1,y1,z1)=7 l1=g(x1,y1,z1)=9
m2=f(x1+h,y1+m1*h,z1+l1*h)=11.5 l2=g(x1+h,y1+m1*h,z1+l1*h)=12
m=(m1+m2)/2=9.25 l=(l1+l2)/2=10.5
y2=y(2)=y1+m*h=9 z2=z(2)=z1+l*h
Therefore y(1)=2,y(1.5)=4.375 and y(2)=9
Algorithm
Basic Gauss elimination method
Step1: Input number of equation (n) and augmented matrix ai,j
Step2: from i=1 to n-1 updating i by 1
i) from j=i+1 to n updating j by 1

Prepared by Ajay Lama


7

a) factor = aj,i/ ai,i


b) from k=1 to n+1 updating k by 1 do
aj,k = aj,k -factor* ai,k
step3: xn = an,n+1/ an,n
step4: from i=n-1 to 1 updating i by -1
i)sum=0
ii)from j=i+1 to n updating j by 1 do
sum=sum+ai,j*xj
iii) xi =( ai,n+1-sum)/ai,i
step5: print result xi
Basic Gauss elimination method with partial pivoting
Step1: Input number of equation (n) and augmented matrix ai,j
Step2: from i=1 to n-1 updating i by 1
i) from j=i+1 to n updating j by 1
a)m=i
b)if | aj,i |>| am,i | assign m=j
ii)if (m≠i) swap am,k and ai,k where k=1 to n
iii)from j=i+1 to n updating j by 1
a) factor = aj,i/ ai,i
b) from k=1 to n+1 updating k by 1 do
aj,k = aj,k -factor* ai,k
step3: xn = an,n+1/ an,n
step4: from i=n-1 to 1 updating i by -1
i)sum=0
ii)from j=i+1 to n updating j by 1 do
sum=sum+ai,j*xj
iii) xi =( ai,n+1-sum)/ai,i
step5: print result xi
( note:underline ->extra from basic gauss elimination method)

Gauss Jordan method for solution of system of linear equation


Step1: Input number of equation (n) and augmented matrix ai,j
Step2: from i=1 to n updating i by 1
i)pivot = ai,i
ii)from k=1 to n+1 updating k by 1
ai,k = ai,k /pivot
i) from j=1 to n updating j by 1
a) factor = aj,i
b) if(i≠j) from k=1 to n+1 updating k by 1
aj,k = aj,k -factor* ai,k
step3: print result xi= ai,n+1
Prepared by Ajay Lama
8

Gauss Jordan method for inverse of a matrix


Step1: Input order of matrix (n) and element of matrix ai,j
Step2:from i=1 to n updating i by 1
a)from j=n+1 to 2n updating j by 1
if(j=i+n) assign ai,j =1 else ai,j =0
Step3: from i=1 to n updating i by 1
i)pivot = ai,i
ii)from k=1 to 2n updating k by 1
ai,k = ai,k /pivot
i) from j=1 to n updating j by 1
a) factor = aj,i
b) if(i≠j) from k=1 to 2n updating k by 1
aj,k = aj,k -factor* ai,k
step4: print result xi= ai,n+1 (underline->extra from solution of equation)
jacobi method
Step1: Input number of equation (n) and augmented matrix ai,j and error e
Step2:from i=1 to n updating i by 1
xi = ai,n+1 / ai,I
step3:from i=1 to n updating i by 1
a)sum=0
b)from j=1 to n updating j by 1
if(i≠j) sum=sum+ ai,j *xj
c) yi =( aj,n+1-sum)/ ai,i
d)errori=| xi -yi |
step 4:from i=1 to n updating i by 1
xi = yi
step 5:find maximum value of errori and assign it to errormax
step6: if(errormax>e) goto step 3 else print xi
seidel method
Step1: Input number of equation (n) and augmented matrix ai,j and error e
Step2:from i=1 to n updating i by 1
xi = ai,n+1 / ai,i
step3:from i=1 to n updating i by 1
a)sum=0
b)from j=1 to n updating j by 1
if(i≠j) sum=sum+ ai,j *xj
c) yi =( aj,n+1-sum)/ ai,I
d)errori=| xi -yi |
e)xi = yi
step 5:find maximum value of errori and assign it to errormax
step6: if(errormax>e) goto step 3 else print xi
Prepared by Ajay Lama
9

lagrange interpolation
step1:Input order of polynomial (n) // n+1 data create n order polynomial
step2:Input all data (xi,yi) ,i=0 to n
step3:Input value of x at which functional value is unknown (xun)
step4:calculate f(xun)=
where Li(xun)=
step5:print f(xun)
Least square method
Step1:Input number of data (n)
Step 2:Input all data (xi,yi),x2
Step 3:Calculate ,
Step 4:calculate a=
b=
step 5: print y=a+bx

shooting method(with modified eulers method)


step1:Define function y’=f(x,y,z)=z and y’’=g(x,y,z)
step 2:Input x0,y0,xn,yn(=B) and h
step3:calculate n=(xn-x0)/h
step3: z0=M1(guess)
a) from i=0,1,2,3..n-1
m1=f(xi,yi,zi) and l1=g(xi,yi,zi)
m2=f(xi+h,yi+m1h,zi+l1h) and l2=g(xi+h,yi+m1h,zi+l1h)
m=(m1+m2)/2 and l=(l1+l2)/2
yi+1=yi+mh,xi+1=xi+h,zi+1=zi+lh
b) B1=y(xn)
step4: z0=M2(guess)
a) from i=0,1,2,3..n-1
m1=f(xi,yi,zi) and l1=g(xi,yi,zi)
m2=f(xi+h,yi+m1h,zi+l1h) and l2=g(xi+h,yi+m1h,zi+l1h)
m=(m1+m2)/2 and l=(l1+l2)/2
yi+1=yi+mh,xi+1=xi+h,zi+1=zi+lh
b) B2=y(xn)
step5: z0=M2+(M2-M1)(B-B2)/(B2-B1)
a) from i=0,1,2,3..n-1
m1=f(xi,yi,zi) and l1=g(xi,yi,zi)
m2=f(xi+h,yi+m1h,zi+l1h) and l2=g(xi+h,yi+m1h,zi+l1h)
m=(m1+m2)/2 and l=(l1+l2)/2
yi+1=yi+mh,xi+1=xi+h,zi+1=zi+lh
step 6: print solution xi,yi
Prepared by Ajay Lama
10

Pseudo code
1.Bisetion Method
Input f(x),e
Repeat
Input x1 and x2
till f(x1)*f(x2)>0
Repeat
x0=(x1+x2)/2
if f(x1)*f(x0)<0 assign x2=x0
else assign x1=x0
till |x2-x1|>e (Or till |f(x0)|>e )// write any one in your pseudo code
print root=x0
2.Regula Falsi method:
Input f(x),e
L1: Input x1 and x2
If(f(x1)*f(x2)>0)goto L1
Repeat
x0={f(x2)*x1-f(x1)*x2}/{f(x2)-f(x1)}
if f(x1)*f(x0)<0 assign x2=x0
else assign x1=x0
till |x2-x1|>e (Or till |f(x0)|>e) //write any one in your pseudo code
print root=x0
3.Secant Method
Input: f(x), e1,x1 and x2 //e1 is error
L1: Set x3= {f(x2)*x1-f(x1)*x2}/{f(x2)-f(x1)}
Assign x1=x2 and x2=x3
If( | f(x3)| >e1) goto L1 (or |x2−x1| >e1) //write any one in your pseudo code
Display roor= x3
4.Newton Raphson
Define f(x) and f’(x)
Input e and x0
Repeat
x1=x0-f(x0)/f’(x0)
error=|x1-x0|
assign x0=x1
till error>e (Or till |f(x0)|>e) //write any one in your pseudo code
5.Newton Raphson(two equation method)
Define f(x,y) ,g(x,y) ,f1(x,y),f2(x,y),g1(x,y),g2(x,y) and error(e)
Input initial guesses (x0,y0)
Repeat D=f1(x0,y0)*g2(x0,y0)-g1(x0,y0)*f2(x0,y0)//D= , Dx= Dy=
Dx=f(x0,y0)*g2(x0,y0)-g(x0,y0)*f2(x0,y0)
Dy=f1(x0,y0)*g(x0,y0)-g1(x0,y0)*f(x0,y0)
x1=x0-Dx/D
y1=y0-Dy/D
error_x=|x1-x0|
error_y=|y1-y0|
Prepared by Ajay Lama
11

assign x0=x1 and y0=y1

till error_x>e or error_y>e (Or |f(x0,y0)|>e or| g(x0,y0)|>e)


print result x0 and y0
6.Gauss Elimination method
input number of equation n and augmented matrix aij
Formation of upper triangular matrix
For i=1 to n-1 updating i by 1 repeat
For j=i+1 to n updating j by 1 repeat
factor =aji/aii
For k=1 to n+1 updating k by 1 repeat
ajk=ajk-factor*aik
end for k
end for j
end for i
xn=an,n/an,n+1
backward substitution method
for i=n-1 to 1 updation i by -1 repeat
sum=0
for j=i+1 to n updating j by 1 repeat
sum=sum+aij*xj
end for j
xi=(ai,n+1-sum)/aii
end for i
print result xi
7.Gauss jordon method
input number of equation n and augmented matrix aij
For i=1 to n updating i by 1 repeat
Pivot=aii
for k=1 to n+1 updating k by 1 repeat
aik=aik/pivot //normalization of pivot row
end for k
for j=1 to n updating j by 1 repeat
factor =aji
If i≠j
For k=1 to n+1 updating k by 1 repeat
ajk=ajk-factor*aik
end for k
end for j
end for i
print result xi

8. Jacobi method
Define error e
Input number of equation (n) and augmented matrix aij
for i=1,2,3..n
set xi=ai,n+1/aii
Prepared by Ajay Lama
12

l1: for i=1,2,3…n


sum=0
for j=1,2,3..n
if(i≠j) sum=sum+aij*xj
end for j
yi=(ai,n+1-sum)/aii
errori=|yi-xi|
end for i
for i=1,2,3…n
xi=yi //difference between seidel and Jacobi is the place of assignment
errormax=error1 // maximum error among error vector
for i=2,3,..n
if(errormax>errori) set errormax=errori
goto l1 till errormax>e
print result xi
9.Gauss seidel
Define error e
Input number of equation (n) and augmented matrix aij
for i=1,2,3..n
set xi=ai,n+1/aii
l1: i=1,2,3…n
sum=0
for j=1,2,3..n
if(i≠j) sum=sum+aij*xj
end for j
yi=(ai,n+1-sum)/aii
errori=|yi-xi|
xi=yi //difference between seidel and jacobi is the place of assignment
end for i
errormax=error1 // maximum error among error vector
for i=2,3,..n
if(errormax>errori) set errormax=errori
goto l1 till errormax>e
print result xi

10. Power method


Input order of square matrix n and error e
Input all element of square matrix aij
Input initial guesses of eigen vector xi
L1:for i=1,2..n
sum=0
for j=1,2,3..n
sum=sum+aij*xj
yi=sum
end for i
eigenvalue=y1
Prepared by Ajay Lama
13

for i=2,3..n
if (|eigenvalue|<|yi|) set eigenvalue=yi
emax=0
for i=1,2,3…n
if(|xi-yi/|eigenvalue||>emax
emax= |xi-yi/|eigenvalue||
xi=yi/|eigenvalue|
end for i
if emax>e go to L1
print eigenvalue and eigen vector xi

11.Lagrange Interpolation
Input E
Input n and all data (xi,yi)
Input point at which value is unknown (xx)
Sum=0
For i=0 to n updating i by 1 repeat
Mul=1
For j=0 to n updating j by 1 repeat
If i≠j , mul=mul*(xx-xi)/(xi-xj)
End for j
Sum=sum+yi*mul
End for i
Print result=sum
12 .least square method
input number of data (n)
input all data (xi,yi)
for i=1to n updating i by 1 repeat
sumx=sumx+xi
sumy=sumy+yi
sumxy=sumxy+xi*yi
sumx2=sumx2+xi*xi
end for i
a=
b=
print result y=a+bx

13.composite simpson 1/3 method


define f(x)
input lower limit(a),upper limit(b),n // n must be even
h=(b-a)/n
for i=1 to n-1 repeat following
if (i modulo2 =0)
sum=sum+2*f(a+i*h)
else
sum=sum+4*f(a+i*h)
end for i
Prepared by Ajay Lama
14

sum=sum+f(a)+f(b)
I=sum*h/3
print integration =I

14.composite simpson 3/8 method


define f(x)
input lower limit(a),upper limit(b),n // n must be multiple of 3
h=(b-a)/n
for i=1 to n-1 repeat following
if (i modulo 3 =0)
sum=sum+2*f(a+i*h)
else
sum=sum+3*f(a+i*h)
end for i
sum=sum+f(a)+f(b)
I=sum*h/3
Print integration =I
15.RK-4 method for solution of ODE 1st order
Define f(x,y) // where f(x,y)=dy/dx
Input step size h,x0,y0,xn
h=(xn-x0)/h
From i=0,1,2…n-1
m1=f(xi,yi)
m2=f(xi +h/2, yi +m1*h/2)
m3=f(xi +h/2, yi +m2*h/2)
m4=f(xi +h, yi +m3*h)
m=(m1+2m2+2m3+m4)/6
xi+1= xi +h , yi+1= yi +m*h
print xi,yi

16.RK-4 method for solution of ODE 2nd order

Define f(x,y,z)=z and g(x,y,z) // where f(x,y,z)=dy/dx=z, and g(x,y,z)=d2y/dx2


Input step size h,x0,y0,z0,xn
Calculate n=(xn-x0)/h
From i=0,1,2…n-1
m1=f(xi,yi,zi) and l1= g(xi, yi, zi)
m2=f(xi +h/2, yi +m1*h/2, zi +l1*h/2) and l2=g(xi +h/2, yi +m1*h/2, zi +l1*h/2)
m3=f(xi +h/2, yi +m2*h/2,zi +l2*h/2) and l3=g(xi +h/2, yi +m2*h/2, zi +l2*h/2)
m4=f(xi +h, yi +m3*h,z0+l3*h) and l4=g(xi +h, yi +m3*h, zi +l3*h)
m=(m1+2m2+2m3+m4)/6 and l= (l1+2l2+2l3+l4)/6
xi+1= xi +h , yi+1= yi +m*h and zi+1= zi +l*h
print xi,yi

Prepared by Ajay Lama

You might also like