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

Fdm2orderode PDF

This document discusses solving second order ordinary differential equations using the finite difference method and the shooting method. It provides an overview of both methods, including replacing derivatives with finite differences in the finite difference method. It also gives pseudocode for solving ODEs using finite difference. Several examples are given to demonstrate applying the methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Fdm2orderode PDF

This document discusses solving second order ordinary differential equations using the finite difference method and the shooting method. It provides an overview of both methods, including replacing derivatives with finite differences in the finite difference method. It also gives pseudocode for solving ODEs using finite difference. Several examples are given to demonstrate applying the methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

20 K.

SUSHAN BAIRY

mprintf("y(%f) = %f \n",x(i),y(i));
end
//Calculating predicted and corrector method
for i=4:n
//Predicted value
y(i+1)=y(i)+h*(55*f(x(i),y(i))-59*f(x(i-1),y(i-1))..
+37*f(x(i-2),y(i-2))-9*f(x(i-3),y(i-3)))/24;
mprintf("Predicted Value y(%f)=%f\n",x(i+1),y(i+1));
//Corrected value
yc(1)=y(i+1)+h*(9*f(x(i+1),y(i+1))+19*f(x(i),y(i))..
-5*f(x(i-1),y(i-1))+f(x(i-2),y(i-2)))/24;
for j=2:20
yc(j)=y(i)+h*(9*f(x(i+1),yc(j-1))+19*f(x(i),y(i))..
-5*f(x(i-1),y(i-1))+f(x(i-2),y(i-2)))/24;
if abs(yc(j)-yc(j-1))<%eps then
mprintf("Corrected Value y(%f)=%f\n",x(i+1),yc(j));
y(i+1)=yc(j)
break
end
end
end

9. Finite Difference Method (Second order ODE)

The finite difference method for the solution of a two-point boundary value problem
consists in replacing the derivatives occurring in the differential equation by means of
their finite difference approximations and then solving the resulting system of equations
by a standard procedure.
Consider the linear second order differential equation
d2 y dy
2
+ P (x) + Q(x)y = R(x),
dx dx
with the boundary conditions y(x0 ) = y0 and y(xn ) = yn .
Let n = (xn − x0 )/h be the number of subintervals, where h is the step-size. Let
xi+1 = xi + h = x0 + ih, for i = 0, 1, 2, . . . , n. At the interior mesh points x1 , x2 , . . . , xn−1 ,
the differential equation to be approximated is given by,

y 00 (xi ) + P (xi )y 0 (xi ) + Q(xi )y(xi ) = R(xi ).


LAB MANUAL M306P 21

The centered-difference formula for y 00 (xi ) and y 0 (xi ) are given by,
y(xi+1 ) − y(xi−1 )
y 0 (xi ) = ,
2h
y(xi+1 ) − 2y(xi ) + y(xi−1 )
y 00 (xi ) = .
h2
Using these centered-difference formulas in the differential equation, we get
   
 2
 h 2 h
− 2 + h Q(x1 ) y(x1 ) + 1 − P (x1 ) y(x2 ) = h R(x1 ) − 1 + P (x1 ) y0
2 2
   
h h
1 + P (xi ) y(xi−1 ) − 2 + h2 Q(xi ) y(xi ) + 1 − P (xi ) y(xi+1 ) = h2 R(xi ),
 
2 2
for i = 2, 3, . . . , n − 1
   
h  2
 2 h
1 + P (xn ) y(xn−1 ) − 2 + h Q(xn ) y(xn ) = h R(xn ) − 1 − P (xn ) yn .
2 2
Thus, we have a tridiagonal system of equations which can be solved using Thomas
algorithm.

9.1. Exercises:
(1) Solve y 00 = 4(y − x), y(0) = 0, y(1) = 2 by finite difference method. Choose
h = 0.25.
(2) Solve y 00 = y 0 +2y +cos x, y(0) = −0.3, y(π/2) = −0.1 by finite difference method.
Choose h = π/16.
(3) Solve y 00 = − x2 y 0 + x22 y + sin (log
x2
x)
, y(1) = 1, y(2) = 2 by finite difference method.
Choose h = 0.1

9.2. Algorithm for solving ODE using finite difference method.


1: Start
2: Read the values of x0, xf, y0, yf, h
3: Define n ← (xf − x0)/h
4: for i=1 to n in steps of 1 do
5: x(i) ← x0 + (i) ∗ h
6: end for
7: for i=1 to n-2 in steps of 1 do
8: A(i,i)← 2 ∗ h2 ∗ q(x(i)) − 4
9: A(i,i+1)← 2 + h ∗ p(x(i))
10: A(i+1,i)← 2 − h ∗ p(x(i + 1))
11: end for
12: A(n-1,n-1)← 2 ∗ h ∗ h ∗ q(x(n − 1)) − 4
13: B(1)← 2 ∗ h2 ∗ r(x(1)) + (h ∗ p(x(1)) − 2) ∗ y0
22 K. SUSHAN BAIRY

14: B(n-1)← 2 ∗ h2 ∗ r(x(n − 1)) − (2 + h ∗ p(x(n − 1))) ∗ yf ;


15: for i=2 to n-2 in the steps of 1 do
16: B(i)← 2 ∗ h2 ∗ r(x(i))
17: end for
18: y← inverse(A)*B
19: Print y(x0) ← y0
20: for i=1 to n-1 in steps of 1 do
21: Print y(x(i)) ← y(i)
22: end for
23: Print y(xf ) ← yf
24: Stop

1: Define s1=p(x)
2: s1 = p(x)
3: Return s1

1: Define s2=q(x)
2: s2 = q(x)
3: Return s2

1: Define s3=r(x)
2: s3 = r(x)
3: Return s3

9.3. Program:
clc;
clear;
mprintf("SCILAB PROGRAM FOR FINITE DIFFERENCE APPROXIMATION..
FOR LINEAR ODE\n")
function [z]=P(x)
z=0;
endfunction
function [z]=Q(x)
z=-4;
LAB MANUAL M306P 23

endfunction
function [z]=R(x)
z=-4*x;
endfunction
a=input("Enter initial value of x:")
b=input("Enter final value of x:")
c=input("Enter initial value of y:")
d=input("Enter final value of y:")
h=input("Enter width in x direction h:")
n=(b-a)/h;
for i=1:n
x(i)=a+i*h;//disp(x(i));
end
for i=1:n-2
A(i,i)=2*h^2*Q(x(i))-4;
A(i,i+1)=2+h*P(x(i));
A(i+1,i)=2-h*P(x(i+1));
end
A(n-1,n-1)=2*h*h*Q(x(n-1))-4;
B(1)=2*h^2*R(x(1))+(h*P(x(1))-2)*c;
B(n-1)=2*h^2*R(x(n-1))-(2+h*P(x(n-1)))*d;
for i=2:n-2
B(i)=2*h^2*R(x(i));
end
y=inv(A)*B;
x=[a;x];
y=[c;y;d];
for i=1:n+1
mprintf("y(%3f)=%f\n",x(i),y(i));
end

10. Solution of Second Order ODE by Shooting Method

Shooting method is an iterative technique which transforms a boundary value problem


into an initial value problem.
d2 y 0
Consider the second order differential equation dx 2 = f (x, y, y ), subject to boundary

conditions y(x0 ) = y0 and y(xn ) = yn . To solve the BVP, we reduce the second order
dy
differential equation into two first order differential equations by setting dx = z. Then,

You might also like