Fdm2orderode PDF
Fdm2orderode PDF
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
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,
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
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
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,