Noting thatthe second integrand function in P5.6.3 oscillates like crazy with higher frequency and larger amplitude as y gets closer to zero 0, set the lower bound of the integration int
Trang 1250 NUMERICAL DIFFERENTIATION/ INTEGRATION
(ii) Add the block of statements in P5.5(c) into the routines “ sns()” and “adap_smpsn()” to make them cope with the cases
smp-ofNaN(Not-a-Number) andInf(Infinity)
(iii) Supplement the program “nm5p06a.m” so that the various routinesare applied for computing the integrals (P5.6.1) and (P5.6.3), where
the parameters like the number of segments (N = 200), the errortolerance (tol= 1e-4), and the number of grid points (MGL = 20)are supposed to be used as they are in the program Noting thatthe second integrand function in (P5.6.3) oscillates like crazy with
higher frequency and larger amplitude as y gets closer to zero (0),
set the lower bound of the integration interval toa2 = 0.001
(iv) Run the supplemented program and fill in Table P5.6 with the
absolute errors of the results
%nm5p06a
warning off MATLAB:divideByZero
fp56a = inline(’sin(x)./x’,’x’); fp56a2 = inline(’sin(1./y)./y’,’y’);
IT = pi/2; % True value of the integral
a = 0; b = 100; N = 200; tol = 1e-4; MGL = 20; a1 = 0; b1 = 1; a2 = 0.001; b2 = 1; format short e
Trang 2(b) To apply the routines like “smpsns()”, “adapt_smpsn()”, “quad()”, and
“Gauss_Hermite()” for evaluating the integral (P5.6.2), do the following
(i) Note that the integration interval [0,∞) can be changed into afinite interval as below
∞0
e −x2dx =
1 0
e −x2dx+
∞1
e −x2dx
= 10
e −x2dx+
1 0
e −1/y2
y2 dy (P5.6.4)
(ii) Compose the incomplete routine “Gauss_Hermite” like “Gauss_ Legendre”, which performs the Gauss–Hermite integration intro-duced in Section 5.9.2
(iii) Supplement the program “nm5p06b.m” so that the various routinesare applied for computing the integrals (P5.6.2) and (P5.6.4), where
the parameters like the number of segments (N = 200), the errortolerance (tol= 1e-4) and the number of grid points (MGH = 2)are supposed to be used as they are in the program Note that theintegration interval is not (−∞, ∞) like that of Eq (5.9.12), but [0,∞) and so you should cut the result of “Gauss_Hermite()” byhalf to get the right answer for the integral (P5.6.2)
(iv) Run the supplemented program and fill in Table P5.6 with the
absolute errors of the results
(c) Based on the results listed in Table P5.6, answer the following questions: (i) Among the routines “smpsns()”, “adapt_smpsn()”, “quad()”,and “Gauss()”, choose the best two ones for (P5.6.1) and (P5.6.2),respectively
(ii) The routine “Gauss – Legendre()” works (badly, perfectly) evenwith as many as 20 grid points for (P5.6.1), while the routine
Trang 3252 NUMERICAL DIFFERENTIATION/ INTEGRATION
“Gauss_Hermite()” works (perfectly, badly) just with two gridpoints for (P5.6.2) It is because the integrand function of (P5.6.1)
is (far from, just like) a polynomial, while (P5.6.2) matches
Eq (5.9.11) and the part of it excluding e −x2 is (just like, farfrom) a polynomial
function I = Gauss_Hermite(f,N,varargin) [t,w]=???????(N);
ft = feval(f,t,varargin{:});
I = w*ft’;
(iii) Run the following program “nm5p06c.m” to see the shapes of theintegrand functions of (P5.6.1) and (P5.6.2) and the second inte-gral of (P5.6.3) You can zoom in/out the graphs by clicking theTools/Zoom in menu and then clicking any point on the graphswith the left/right mouse button in the MATLAB graphic win-dow Which one is oscillating furiously? Which one is oscillatingmoderately? Which one is just changing abruptly?
(iv) The adaptive integration routines like “adapt smpsn()” and
“quad()” work (badly, fine) for (P5.6.1), but (fine, badly) for(P5.6.2) From this fact, we might conjecture that the adaptiveintegration routines may be (ineffective, effective) for the integrandfunctions which have many oscillations, while they may be(effective, ineffective) for the integrand functions which haveabruptly changing slope To support this conjecture, run thefollowing program “nm5p06d”, which uses the “quad()” routinefor the integrals
b1
sin x
x dx with b = 100, 1000, 10000 (P5.6.5a)
1sin(1/y)
y dy with a = 0.001, 0.0001, 0.00001, (P5.6.5b)
Trang 4disp(’Change of lower limit of the integration interval’)
a2 = [1e-3 1e-4 1e-5 1e-6 0]; b2 = 1; tol = 1e-4;
(cf) This problem warns us that it may be not good to use only one routine for a computational work and suggests us to use more than one method for cross check.
5.7 Gauss–Hermite Integration Method
Consider the following integral:
∞0
10−5 Fill in Table P5.7 with the error results
Table P5.7 The Results of Applying Various Numerical Integration Methods
Simpson (N = 700)
adaptive (tol = 10 −5) Gauss (tolquad= 10 −5) (tolquadl= 10 −5)
(P5.7.1) |error| 1.0001e-3 1.0000e-3
flops 4930 5457 1484 11837 52590 (with quad8)
Trang 5254 NUMERICAL DIFFERENTIATION/ INTEGRATION
5.8 Gauss–Laguerre Integration Method
(a) As in Section 5.9.1, Section 5.9.2, and Problem 5.6(b), compose the
MATLAB routines: “Laguerp()”, which generates the Laguerre nomial (5.9.18); “Gausslgp()”, which finds the grid point t i’s and the
poly-coefficient w N,i’s for Gauss–Laguerre integration formula (5.9.16); and
“Gauss_Laguerre(f,N)”, which uses these two routines to carry outthe Gauss–Laguerre integration method
(b) Consider the following integral:
e −t dt = −e −t∞
0 = 1 ( P5.8.1) Noting that, since this integral matches Eq (5.9.17) with f (t)=
t, Gauss–Laguerre method is the right choice, apply the routine
“Gauss_Laguerre(f,N)” (manufactured in (a)) withN= 2 as well asthe routines “smpsns()”, “adapt_smpsn()”, “quad()”, and “quadl()”for evaluating the integral and fill in Table P5.7 with the error results.Which turns out to be the best? Is the performance of “quad()”improved by lowering the error tolerance?
(cf) This illustrates that the routine “ adapt_smpsn() ” sometimes outperforms the MATLAB built-in routine “ quad() ” with fewer computations On the other hand, Table P5.7 shows that it is most desirable to apply the Gauss quadrature schemes only if one of them is applicable to the integration problem.
endre()” (Section 5.9.1) or “Gauss_Laguerre()” (Problem 5.8) (with
N= 15) to compute the above integrals and fill in Table P5.9 with therelative errors Use the upper/lower bounds of the integration interval inTable P5.9 if they are specified in the table
(b) Based on the results listed in Table P5.9, answer the following questions
or circle the right answer
Trang 6PROBLEMS 255
(i) From the fact that the Gauss–Legendre integration scheme worked
best only for (1), it is implied that the scheme is (recommendable,not recommendable) for the case where the integrand function isfar from being approximated by a polynomial
(ii) From the fact that the Gauss–Laguerre integration scheme worked
best only for (9), it is implied that the scheme is (recommendable,not recommendable) for the case where the integrand function
excluding the multiplying term e −xis far from being approximated
by a polynomial
(iii) Note the following:
ž The integrals (3) and (4) can be converted into each other by a
variable substitution of x = u−1, dx = −u−2du The integrals(5) and (6) have the same relationship
ž The integrals (7) and (8) can be converted into each other by a
variable substitution of u = e −x , dx = −u−1du.From the results for (3)–(8), it can be conjectured that the numerical integra-
tion may work (better, worse) if the integration interval is changed from [1,∞)into (0,1] through the substitution of variable like
(9), b= 10 7.8276e-04 2.9237e-07 7.8276e-04
5.10 The BER (Bit Error Rate) Curve of Communication with Multidimensional
Trang 7256 NUMERICAL DIFFERENTIATION/ INTEGRATION
where b is the number of bits, M = 2b is the number of orthogonal
wave-forms, SNR is the signal-to-noise-ratio, and Q( ·) is the error function
We want to plot the BER curves for SNR= 0:10[dB] and b = 1:4.
(a) Consider the following program “nm5p10.m”, whose objective is to
compute the values of P e,b (SNR,b) for SNR = 0:10[dB] and b = 1:4
by using the routine “Gauss_Hermite()” (Problem 5.6) and also byusing the MATLAB built-in routine “quad()” and to plot them versusSNR[dB]= 10 log10SNR Complete the incomplete part which com-putes the integral in (P5.10.1) over [−1000, 1000] and run the program
to obtain the BER curves like Fig P5.10
(b) Of the two routines, which one is faster and which one presents us with
more reliable values of the integral in (P5.10.1)?
1 0
Figure P5.10 The BER (bit error rate) curves for multidimensional (orthogonal) signaling.
%nm5p10.m: plots the probability of bit error versus SNRbdB
fs =’Q(-sqrt(2)*x - sqrt(b*SNR)).^(2^b - 1)’;
Q = inline(’erfc(x/sqrt(2))/2’,’x’);
f = inline(fs,’x’,’SNR’,’b’);
fex2 = inline([fs ’.*exp(-x.*x)’],’x’,’SNR’,’b’);
SNRdB = 0:10; tol = 1e-4; % SNR[dB] and tolerance used for ’quad’
Trang 8PROBLEMS 257
5.11 Length of Curve/Arc: Superb Harmony of Numerical Derivative/Integral.
The graph of a function y = f (x) of a variable x is generally a curve and its length over the interval [a, b] on the x-axis can be described by a line
integral as
I =
b a
dl =
b a
dx2+ dy2 =
b a
For example, the length of the half-circumference of a circle with the radius
of unit length can be obtained from this line integral with
gram Run the program with the step size h = 0.001, 0.0001, and 0.00001
in the numerical derivative and fill in Table P5.11 with the errors of theresults, noting that the true value of the half-circumference of a unit circle
is π
%nm5p11
a = -1; b = 1; % the lower/upper bounds of the integration interval
N = 1000 % the number of segments for the Simpson method
tol = 1e-6 % the error tolerance
M = 20 % the number of grid points for Gauss – Legendre integration
IT = pi; h = 1e-3 % true integral and step size for numerical derivative flength = inline(’sqrt(1 + dfp511(x,h).^2)’,’x’,’h’);%integrand P5.11.1)
Trang 9258 NUMERICAL DIFFERENTIATION/ INTEGRATION
Table P5.11 Results of Applying Various Numerical Integration Methods for (P5.11.1,2)/(P5.12.1,2)
Step-size h Simpson Adaptive quad quadl Gauss
5.12 Surface Area of Revolutionary 3-D (Cubic) Object
The upper/lower surface area of a 3-D structure formed by one revolution of
a graph (curve) of a function y = f (x) around the x-axis over the interval [a, b] can be described by the following integral:
I = 2π
b a
y dl = 2π
b a
f (x)
1+ (f(x))2dx ( P5.12.1)
For example, the surface area of a sphere with the radius of unit length can
be obtained from this equation with
y = f (x) =1− x2, a = −1, b= 1 ( P5.12.2)
Starting from the program “nm5p11.m”, make a program “nm5p12.m” thatuses the numerical integration routines “smpsns()” (with the number ofsegments N = 1000), “adapt_smpsn()”, “quad()”, “quadl()” (with theerror tolerance tol = 10−6) and “Gauss_Legendre()” (with the number
of grid points M = 20) to evaluate the integral (P5.12.1,2) with the firstderivative approximated by Eq (5.1.8), where the parameters like the num-ber of segments (N), the error tolerance (tol), and the number of grid points(M) are supposed to be as they are in the program Run the program with
the step size h = 0.001, 0.0001, and 0.00001 in the numerical derivative
and fill in Table P5.11 with the errors of the results, noting that the true
value of the surface area of a unit sphere is 4π
5.13 Volume of Revolutionary 3-D (Cubic) Object
The volume of a 3-D structure formed by one revolution of a graph (curve)
of a function y = f (x) around the x-axis over the interval [a, b] can be
described by the following integral:
I = π
b
Trang 10PROBLEMS 259
For example, the volume of a sphere with the radius of unit length (Fig.P5.13) can be obtained from this equation with Eq (P5.12.2) Starting fromthe program “nm5p11.m”, make a program “nm5p13.m” that uses the numer-ical integration routines “smpsns()” (with the number of segments N =
100), “adapt_smpsn()”, “quad()”, “quadl()” (with the error tolerance
tol = 10−6), and “Gauss_Legendre()” (with the number of grid points
M = 2) to evaluate the integral (P5.13.1) Run the program and fill inTable P5.11 with the errors of the results, noting that the volume of a
−y cos xπ
0 dy=
2 0
2y dy = y22
0 = 4
( P5.14.1)
Use the routine “int2s()” (Section 5.10) with M = N = 20, M = N =
50 and M = N = 100and the MATLAB built-in routine “dblquad()”
to compute this double integral Fill in Table P5.14.1 with the resultsand the times measured by using the commandstic/tocto be takenfor carrying out each computation Based on the results listed inTable P5.14.1, can we say that the numerical error becomes smaller
as we increase the numbers (M,N) of segments along the x-axis and
y-axis for the routine “int2s()”?
Trang 11260 NUMERICAL DIFFERENTIATION/ INTEGRATION
(b) Consider the following double integral:
I =
1 0
1 0
1
1− xy dx dy=
π2
Noting that the integrand function is singular at (x, y) = (1, 1), use
the routine “int2s()” and the MATLAB built-in routine “dblquad()”with the upper limit (d) of the integration interval along the y-axis d
= 0.999, d = 0.9999,d = 0.99999 and d = 0.999999 to compute thisdouble integral Fill in Tables P5.14.2 and P5.14.3 with the results andthe times measured by using the commands tic/tocto be taken forcarrying out each computation
Table P5.14.1 Results of Running ‘‘int2s()’’ and ‘‘dblquad()’’ for (P5.14.1)
int2s(), int2s(), int2s(),
(i) Can we say that the numerical error becomes smaller as we set the
upper limit (d) of the integration interval along the y-axis closer to
the true limit 1?
Trang 12PROBLEMS 261
(ii) Can we say that the numerical error becomes smaller as we increase
the numbers (M,N) of segments along the x-axis and y-axis for the
routine “int2s()”? If this is contrary to the case of (a), can youblame the weird shape of the integrand function in Eq (P5.14.2)for such a mess-up?
(cf ) Note that the computation times to be listed in Tables P5.14.1 to P5.14.3 may vary with the speed of CPU as well as the computational jobs which are concurrently processed by the CPU Therefore, the time measured by the
‘ tic/toc ’ commands cannot be an exact estimate of the computational load taken by each routine.
(a) Use any integration method that you like best to evaluate the integral
I1 =
1 0
f (x)0
1 dy dx=
10
x0
1 dy dx ( P5.15.2)
You may get puzzled with some problem when applying the routine
“int2s()” if you define the integrand function as
>>fp515b = inline(’1’,’x’,’y’);
It is because this function, being called inside the routine
“smpsns_fxy()”, yields just a scalar output even for the vector-valuedinput argument There are two remedies for this problem One is todefine the integrand function in such a way that it can generate theoutput of the same dimension as the input
Trang 13262 NUMERICAL DIFFERENTIATION/ INTEGRATION
function INTf = smpsns_fxy(f,x,c,d,N)
Trang 14In this chapter, we look into several methods of obtaining the numerical tions to ordinary differential equations (ODEs) in which all dependent variables
solu-(x) depend on a single independent variable (t) First, the initial value problems
(IVPs) will be handled with several methods including Runge–Kutta method andpredictor–corrector methods in Sections 6.1 to 6.5 The final section (Section 6.6)will introduce the shooting method and the finite difference method for solvingthe two-point boundary value problem (BVP) ODEs are called an IVP if the
values x(t0) of dependent variables are given at the initial point t0 of the
inde-pendent variable, while they are called a BVP if the values x(t0) / x(t f )are given
at the initial/final points t0 and t f
When talking about the numerical solutions to ODEs, everyone starts with theEuler’s method, since it is easy to understand and simple to program Even thoughits low accuracy keeps it from being widely used for solving ODEs, it gives us a
Applied Numerical Methods Using MATLAB, by Yang, Cao, Chung, and Morris
Copyr ight 2005 John Wiley & Sons, I nc., ISBN 0-471-69833-4
263
Trang 15264 ORDINARY DIFFERENTIAL EQUATIONS
clue to the basic concept of numerical solution for a differential equation simplyand clearly Let’s consider a first-order differential equation:
y(t) + a y(t) = r with y(0) = y0 ( 6.1.1)
It has the following form of analytical solution:
trans-Then, how do we translate the differential equation into a form that can ily be handled by computer? First of all, we have to replace the derivative
eas-y(t) = dy/dt in the differential equation by a numerical derivative (introduced in Chapter 5), where the step-size h is determined based on the accuracy require-
ments and the computation time constraints Euler’s method approximates thederivative in Eq (6.1.1) with Eq (5.1.2) as
y(t + h) − y(t)
h + a y(t) = r
y(t + h) = (1 − ah)y(t) + hr with y(0) = y0 (6.1.3)
and solves this difference equation step-by-step with increasing t by h each time from t = 0
y(h) = (1 − ah)y(0) + hr = (1 − ah)y0 + hr
y( 2h) = (1 − ah)y(h) + hr = (1 − ah)2y0+ (1 − ah)hr + hr
To be specific, let the parameters and the initial value of Eq (6.1.1) be a= 1,
r = 1, and y0= 0 Then, the analytical solution (6.1.2) becomes
Trang 16for itr = 1:3 %with various step size h = 1/8,1/4,1/2
klast = klasts(itr); h = hs(itr); y(1)=y0;
for k = 1:klast
y(k + 1) = (1 - a*h)*y(k) +h*r; %Eq.(6.1.3):
plot([k - 1 k]*h,[y(k) y(k+1)],’b’, k*h,y(k+1),’ro’)
if k < 4, pause; end
end
end
and the numerical solution (6.1.4) with the step-size h = 0.5 and h = 0.25 are
as listed in Table 6.1 and depicted in Fig 6.1 We make a MATLAB program
“nm610.m”, which uses Euler’s method for the differential equation (6.1.1), ally solving the difference equation (6.1.3) and plots the graphs of the numericalsolutions in Fig 6.1 The graphs seem to tell us that a small step-size helpsreduce the error so as to make the numerical solution closer to the (true) ana-lytical solution But, as will be investigated thoroughly in Section 6.2, it is onlypartially true In fact, a too small step-size not only makes the computation time
actu-longer (proportional as 1/ h), but also results in rather larger errors due to the
accumulated round-off effect This is why we should look for other methods todecrease the errors rather than simply reduce the step-size
Euler’s method can also be applied for solving a first-order vector differentialequation
Trang 17266 ORDINARY DIFFERENTIAL EQUATIONS
Figure 6.1 Examples of numerical solution obtained by using the Euler’s method.
and is cast into the MATLAB routine “ode_Euler()”
function [t,y] = ode_Euler(f,tspan,y0,N)
%Euler’s method to solve vector differential equation y’(t) = f(t,y(t))
% for tspan = [t0,tf] and with the initial value y0 and N time steps
if nargin<4 | N <= 0, N = 100; end
if nargin<3, y0 = 0; end
h = (tspan(2) - tspan(1))/N; %stepsize
t = tspan(1)+[0:N]’*h; %time vector
y(1,:) = y0(:)’; %always make the initial value a row vector
for k = 1:N
y(k + 1,:) = y(k,:) +h*feval(f,t(k),y(k,:)); %Eq.(6.1.7)
end
Another method of solving a first-order vector differential equation like Eq (6.1.6)comes from integrating both sides of the equation
y(t) = f(t, y), y(t)|tk+1
tk = y(t k+1) − y(t k )=
tk+1tk
f(t, y) dt
y(t k+1) = y(t k )+ tk+1
tk
f(t, y) dt with y(t0)= y0 (6.2.1)
If we assume that the value of the (derivative) function f(t,y) is constant
as f(t k ,y(t k )) within one time step [t k ,t k+1), this becomes Eq (6.1.7) (with h=
t k+1− t k), amounting to Euler’s method If we use the trapezoidal rule (5.5.3), itbecomes
yk+1= yk+h
2{f(t k , y k ) + f(t k+1, y k+1)} ( 6.2.2)
Trang 18RUNGE–KUTTA METHOD 267
function [t,y] = ode_Heun(f,tspan,y0,N)
%Heun method to solve vector differential equation y’(t) = f(t,y(t))
% for tspan = [t0,tf] and with the initial value y0 and N time steps
if nargin<4 | N <= 0, N = 100; end
if nargin<3, y0 = 0; end
h = (tspan(2) - tspan(1))/N; %stepsize
t = tspan(1)+[0:N]’*h; %time vector
y(1,:) = y0(:)’; %always make the initial value a row vector
for k = 1:N
fk = feval(f,t(k),y(k,:)); y(k+1,:) = y(k,:)+h*fk; %Eq.(6.2.3) y(k+1,:) = y(k,:) +h/2*(fk +feval(f,t(k+1),y(k+1,:))); %Eq.(6.2.4) end
But, the right-hand side (RHS) of this equation has yk+1, which is unknown at
t k To resolve this problem, we replace the yk+1 on the RHS by the followingapproximation:
yk+1∼= yk + hf(t k , y k ) ( 6.2.3)
so that it becomes
yk+1= yk+h
2{f(t k , y k ) + f(t k+1, y k + hf(t k , y k ))} ( 6.2.4)
This is Heun’s method, which is implemented in the MATLAB routine
“ode_Heun()” It is a kind of predictor-and-corrector method in that it predicts
the value of yk+1 by Eq (6.2.3) at t k and then corrects the predicted value by
Eq (6.2.4) at t k+1 The truncation error of Heun’s method is O(h2)(proportional
to h2) as shown in Eq (5.6.1), while the error of Euler’s method is O(h).
Although Heun’s method is a little better than the Euler’s method, it is still notaccurate enough for most real-world problems The fourth-order Runge–Kutta
(RK4) method having a truncation error of O(h4)is one of the most widely usedmethods for solving differential equations Its algorithm is described below
Trang 19268 ORDINARY DIFFERENTIAL EQUATIONS
function [t,y] = ode_RK4(f,tspan,y0,N,varargin)
%Runge-Kutta method to solve vector differential eqn y’(t) = f(t,y(t))
% for tspan = [t0,tf] and with the initial value y0 and N time steps
if nargin < 4 | N <= 0, N = 100; end
if nargin < 3, y0 = 0; end
y(1,:) = y0(:)’; %make it a row vector
h = (tspan(2) - tspan(1))/N; t = tspan(1)+[0:N]’*h;
for k = 1:N
f1 = h*feval(f,t(k),y(k,:),varargin{:}); f1 = f1(:)’; %(6.3.2a)
f2 = h*feval(f,t(k) + h/2,y(k,:) + f1/2,varargin{:}); f2 = f2(:)’;%(6.3.2b) f3 = h*feval(f,t(k) + h/2,y(k,:) + f2/2,varargin{:}); f3 = f3(:)’;%(6.3.2c) f4 = h*feval(f,t(k) + h,y(k,:) + f3,varargin{:}); f4 = f4(:)’; %(6.3.2d) y(k + 1,:) = y(k,:) + (f1 + 2*(f2 + f3) + f4)/6; %Eq.(6.3.1)
plot(t,yt,’k’, t1,ye,’b:’, t1,yh,’b:’, t1,yr,’r:’)
plot(t1,ye,’bo’, t1,yh,’b+’, t1,yr,’r*’)
N = 1e3; %to estimate the time for N iterations
tic, [t1,ye] = ode_Euler(df61,tspan,y0,N); time_Euler = toc
tic, [t1,yh] = ode_Heun(df61,tspan,y0,N); time_Heun = toc
tic, [t1,yr] = ode_RK4(df61,tspan,y0,N); time_RK4 = toc
Equation (6.3.1) is the core of RK4 method, which may be obtained by stituting Simpson’s rule (5.5.4)
the average of the successive function values (f k + f k )/2 Accordingly, the
RK4 method has a truncation error of O(h4)as Eq (5.6.2) and thus is expected
to work better than the previous two methods
The fourth-order Runge–Kutta (RK4) method is cast into the MATLAB tine “ode_RK4()” The program “nm630.m” uses this routine to solve Eq (6.1.1)
rou-with the step size h = (t f − t0)/N = 2/4 = 0.5 and plots the numerical result
together with the (true) analytical solution Comparison of this result with those ofEuler’s method (“ode_Euler()”) and Heun’s method (“ode_Heun()”) is given inFig 6.2, which shows that the RK4 method is better than Heun’s method, whileEuler’s method is the worst in terms of accuracy with the same step-size But,
Trang 20PREDICTOR–CORRECTOR METHOD 269
+
++
y (t ) = 1 – e –at the (true) analytical solution
h = 0.5
Figure 6.2 Numerical solutions for a first-order differential equation.
in terms of computational load, the order is reversed, because Euler’s method,Heun’s method, and the RK4 method need 1, 2, and 4 function evaluations (calls)per iteration, respectively
(cf) Note that a function call takes much more time than a multiplication and thus the number of function calls should be a criterion in estimating and comparing compu- tational time.
The MATLAB built-in routines “ode23()” and “ode45()” implement theRunge–Kutta method with an adaptive step-size adjustment, which uses a
large/small step-size depending on whether f (t) is smooth or rough In
Section 6.4.3, we will try applying these routines together with our routines tosolve a differential equation for practice rather than for comparison
6.4.1 Adams– Bashforth– Moulton Method
The Adams–Bashforth–Moulton (ABM) method consists of two steps The first
step is to approximate f(t,y) by the (Lagrange) polynomial of degree 4 matching
the four points
{(t k−3, f k−3), (t k−2, f k−2), (t k−1, f k−1), (t k , f k )}and substitute the polynomial into the integral form (6.2.1) of differential equation
to get a predicted estimate of yk+1
pk+1= yk+
h0
l3(t) dt = yk+ h
24(−9fk−3+ 37fk−2− 59fk−1+ 55fk )
( 6.4.1a)
Trang 21270 ORDINARY DIFFERENTIAL EQUATIONS
The second step is to repeat the same work with the updated four points
iL = polyint(L(m,:)); %indefinite integral of polynomial
cAP(m) = polyval(iL,1)-polyval(iL,0); %definite integral over [0,1] end
cAP %Predictor coefficients
[l,L] = lagranp([-2 -1 0 1],[0 0 0 0]); %only coefficient polynomial L for m = 1:4
iL = polyint(L(m,:)); %indefinite integral of polynomial
cAC(m) = polyval(iL,1) - polyval(iL,0); %definite integral over [0,1] end
cAC %Corrector coefficients
( 2)
k +h44!f
( 3)
k +h55!f
( 2)
k+1+ h44!f
( 3)
k+1− h55!f
( 2)
k+1−h44!f
( 3)
k+1+h55!f
( 4)
k+1− · · · (6.4.2b)and replace the first, second, and third derivatives by their difference approxi-mations
Trang 22From these equations and under the assumption that fk ( 4)+1∼= f( 4)
k ∼= K, we canwrite the predictor/corrector errors as
Trang 23272 ORDINARY DIFFERENTIAL EQUATIONS
We still cannot use these formulas to estimate the predictor/corrector errors, since
K is unknown But, from the difference between these two formulas
pk+1→ pk+1+251
270(c k− pk )⇒ mk+1 (6.4.7a)
ck+1→ ck+1− 19
270(c k+1− pk+1)⇒ yk+1 (6.4.7b)These modification formulas are expected to reward our efforts that we havemade to derive them
The Adams–Bashforth–Moulton (ABM) method with the modification las can be described by Eqs (6.4.1a), (6.4.1b), and (6.4.7a), (6.4.7b) summarizedbelow and is cast into the MATLAB routine “ode_ABM()” This scheme needsonly two function evaluations (calls) per iteration, while having a truncation
formu-error of O(h5)and thus is expected to work better than the methods discussed sofar It is implemented by the MATLAB built-in routine “ode113()” with manyadditional sophisticated techniques
Adams–Bashforth–Moulton method with modification formulas
Predictor: pk+1= yk+ h
24(−9fk−3+ 37fk−2− 59fk−1+ 55fk ) (6.4.8a)Modifier: mk+1= pk+1+251
Trang 24PREDICTOR–CORRECTOR METHOD 273
function [t,y] = ode_ABM(f,tspan,y0,N,KC,varargin)
%Adams-Bashforth-Moulton method to solve vector d.e y’(t) = f(t,y(t))
% for tspan = [t0,tf] and with the initial value y0 and N time steps
% using the modifier based on the error estimate depending on KC = 1/0
if nargin < 5, KC = 1; end %with modifier by default
if nargin < 4 | N < = 0, N = 100; end %default maximum number of iterations y0 = y0(:)’; %make it a row vector
h = (tspan(2) - tspan(1))/N; %step size
p = p1; c = c1; %update the predicted/corrected values
F = [F(2:4,:); feval(f,t(k + 1),y(k + 1,:),varargin{:})];
End
function [t,y] = ode_Ham(f,tspan,y0,N,KC,varargin)
% Hamming method to solve vector d.e y’(t) = f(t,y(t))
% for tspan = [t0,tf] and with the initial value y0 and N time steps
% using the modifier based on the error estimate depending on KC = 1/0
if nargin < 5, KC = 1; end %with modifier by default
if nargin < 4 | N <= 0, N = 100; end %default maximum number of iterations
if nargin < 3, y0 = 0; end %default initial value
y0 = y0(:)’; end %make it a row vector
h = (tspan(2)-tspan(1))/N; %step size
p = p1; c = c1; %update the predicted/corrected values
F = [F(2:3,:); feval(f,t(k + 1),y(k + 1,:),varargin{:})];
end
Trang 25274 ORDINARY DIFFERENTIAL EQUATIONS
Hamming method with modification formulas
Predictor: pk+1= yk−3+4h
3 (2fk−2− fk−1+ 2fk ) (6.4.9a)Modifier: mk+1= pk+1+112
This scheme also needs only two function evaluations (calls) per iteration,
while having the error of O(h5) and so is comparable with the ABM methoddiscussed in the previous section
The major factors to be considered in evaluating/comparing different cal methods are the accuracy of the numerical solution and its computationtime In this section, we will compare the routines “ode_RK4()”, “ode_ABM()”,
numeri-“ode_Ham()”, “ode23()”, “ode45()”, and “ode113()” by trying them out onthe same differential equations, hopefully to make some conjectures about theirperformances It is important to note that the evaluation/comparison of numer-ical methods is not so simple because their performances may depend on thecharacteristic of the problem at hand It should also be noted that there are otherfactors to be considered, such as stability, versatility, proof against run-timeerror, and so on These points are being considered in most of the MATLABbuilt-in routines
The first thing we are going to do is to validate the effectiveness of the ifiers (Eqs (6.4.8b,d) and (6.4.9b,d)) in the ABM (Adams–Bashforth–Moulton)method and the Hamming method For this job, we write and run the program
mod-“nm643_1.m” to get the results depicted in Fig 6.3 for the differential equation
y(t) = −y(t) + 1 with y(0)= 0 ( 6.4.10)
which was given at the beginning of this chapter Fig 6.3 shows us an interestingfact that, although the ABM method and the Hamming method, even withoutmodifiers, are theoretically expected to have better accuracy than the RK4 (fourth-order Runge–Kutta) method, they turn out to work better than RK4 only withmodifiers Of course, it is not always the case, as illustrated in Fig 6.4, which