Numerical Solution of Initial

advertisement
Chapter 4
Numerical Solution of Initial-Value Problems
4.1 Introduction
A linear or nonlinear first order differential equation can always be solved numerically.
Consider the following differential equation
dy
= f(x, y)
dx
(4.1-1)
with initial condition x = x0, y = y0. The solution to the equation (4.1-1) is plotted in Figure
4.1-1.
y
y1
y0
Dy
h
x0
x1
x
dy
Figure 4.1-1. Numerical evaluation of
= f(x, y)
dx
The dependent variable y at x = x0 + h = x1 can be approximated by y = y0 + y where
y =h f(x0, y0)
The numerical solution y1 will approach the analytical solution as h approaches zero. The
process can be repeated until the final value of the independent variable x is obtained. This
procedure is the well-known Euler’s method to evaluate a first order differential equation
numerically. The value at the end of each interval or step size h is evaluated from the values
and slope at the beginning of the interval. This method only requires one slope evaluation
therefore it is a first order method. For a given step size, the accuracy of the solution can be
improved by evaluating the slope f(x, y) more than one. Each successive slope evaluation is
based on the improved value of the dependent variable from the previous slope. The slope
can be evaluated at any point between the interval h. However, it is normally evaluated at the
beginning, the midpoint, and/or the end of the interval.
4-1
4.2 Taylor Methods
Many of the numerical solution to initial-value problems have an underlying derivation from
Taylor’s Theorem. Expanding the unknown function using Taylor’s Theorem we obtain
y(x0 + h) = y(x0) + y’(x0)h +
1
1
y”(x0)h2 + y”’(x0)h3 + 
2!
3!
If the Taylor series is truncated after the first derivative term, we obtain Euler’s method
y(x0 + h) = y(x0) + y’(x0)h
1
y”()h2 for which x0    x0 + h. The term
2!
local error refers to the error at the given step if the previous results are all exact. The global
error is the true error or the error accumulated due to all steps from the initial conditions.
The local error for Euler’s method is equal to
The second order Taylor series for yn+1 is given as
yn+1 = yn + y’(xn)h +
In general y’(xn) =
1
y”(xn)h2
2!
dy
(xn, yn) = f(xn, yn), therefore
dx
yn+1 = yn + f(xn, yn)h +
where f’(xn, yn) =
1
f’(xn, yn)h2
2!
df
f dx
f
f dy
f dy
=
+
=
+
dx
x dx
x
y dx
y dx
Example 4.2-1 _____________________________________________________
Solve the following ordinary differential equation (ODE) using Taylor’s method of order 2
with x = 0.2
dy
= y  x2 + 1,
dx
for 0  x  2,
with y(0) = 0.5
Solution
df
= y’  2x = y  x2 + 1  2x
dx
yn+1 = yn + (yn  xn2 + 1)h +
1
( yn  xn2  2xn + 1)h2
2
4-2
In terms of f = y  x2 + 1, yn+1 = yn + fnh +
1
( fn  2xn)h2
2
Table 4.2-1 lists the Matlab program with numerical and analytical values of y for 0.2  x 
2. The exact or analytical values of y are given by y = (x + 1)2  0.5et.
Table 2.4-2 Matlab program for
dy
= y  x2 + 1------------dx
%
% Example 4.2-1, Taylor's method of order 2
%
x=0;y=0.5;h=0.2;
for i=1:10
f=y-x*x+1;
y=y+f*h+.5*(f-2*x)*h*h;
x=x+h;
ye=(x+1)^2-.5*exp(x);
fprintf('x = %10.7f, y = %10.7f, y(exact) = %10.7f\n',x,y,ye)
end
x=
x=
x=
x=
x=
x=
x=
x=
x=
x=
0.2000000, y =
0.4000000, y =
0.6000000, y =
0.8000000, y =
1.0000000, y =
1.2000000, y =
1.4000000, y =
1.6000000, y =
1.8000000, y =
2.0000000, y =
0.8300000, y(exact) =
1.2158000, y(exact) =
1.6520760, y(exact) =
2.1323327, y(exact) =
2.6486459, y(exact) =
3.1913480, y(exact) =
3.7486446, y(exact) =
4.3061464, y(exact) =
4.8462986, y(exact) =
5.3476843, y(exact) =
0.8292986
1.2140877
1.6489406
2.1272295
2.6408591
3.1799415
3.7324000
4.2834838
4.8151763
5.3054720
4-3
Download