Application Center - Maplesoft

App Preview:

Classroom Tips and Techniques: Numeric Solution of a Two-Point BVP

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Image 

Classroom Tips and Techniques:
Numeric Solution of a Two-Point BVP
 

Robert J. Lopez
Emeritus Professor of Mathematics and Maple Fellow
Maplesoft
 

Introduction 

 

A recent query from a colleague in engineering posed a nonlinear two-point boundary value problem that could only be solved numerically.  In this month's article, we'll explore the solution of a similar problem, highlighting the way Maple can be used to obtain the required solution. 

 

Initializations 

 

> Typesetting:-mrow(Typesetting:-mi(

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

> Typesetting:-mrow(Typesetting:-mi(
 

Statement of the BVP 

 

Consider the nonlinear ODE 

 

> Typesetting:-mrow(Typesetting:-mi(

Typesetting:-mprintslash([`:=`(q, `+`(diff(y(x), `$`(x, 2)), `*`(`^`(y(x), 2), `*`(diff(y(x), x))), cos(y(x))) = x)], [`+`(diff(diff(y(x), x), x), `*`(`^`(y(x), 2), `*`(diff(y(x), x))), cos(y(x))) = x...
 


and the related nonlinear boundary conditions
 

 

> Typesetting:-mrow(Typesetting:-mi(

 

Typesetting:-mprintslash([`:=`(b1, `+`(`*`(`+`(1, y(0)), `*`((D(y))(0))), `-`(`*`(3, `*`(y(0))))) = 0)], [`+`(`*`(`+`(1, y(0)), `*`((D(y))(0))), `-`(`*`(3, `*`(y(0))))) = 0])
Typesetting:-mprintslash([`:=`(b2, `+`(`*`(`+`(1, y(1)), `*`((D(y))(1))), `-`(`*`(3, `*`(y(1)))), `-`(`*`(`^`(y(1), 2))), `/`(1, 2)) = 0)], [`+`(`*`(`+`(1, y(1)), `*`((D(y))(1))), `-`(`*`(3, `*`(y(1))...
 


that pose a two-point boundary value problem on the interval Typesetting:-mrow(Typesetting:-mn(
 

 

Using dsolve to Obtain a Solution 

 

A naive use of Maple's dsolve command did not provide a solution to the problem originally posed, running at great length and then crashing.  However, inclusion of the approxsoln parameter very quickly led to the desired solution.  This parameter provides an initial guess for an iterative Rayleigh-Ritz solution process that converges very quickly to a solution.  In the original engineering problem there were two possible solutions, only one of which was physically meaningful. 

 

If we apply that same strategy to the problem posed in the previous section, assuming that perhaps a solution might be close to Typesetting:-mrow(Typesetting:-mi(, we obtain the graph shown in Figure 1. 

 

> Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 1   Solution of nonlinear two-point BVP starting with initial guess Typesetting:-mrow(Typesetting:-mi( 


In principle, it should be possible to solve this BVP via a shooting method, using the two boundary conditions as the governing equations that determine Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(.  In the sequel, we apply this idea to the solution process, and also come to grips with another disturbing facet of the problem we selected, namely, finding all possible solutions.
 

 

A Solution from First Principles 

 

We begin by defining a procedure Typesetting:-mrow(Typesetting:-mi( that, given values for Typesetting:-mrow(Typesetting:-mi(, returns the list Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(  This procedure implements a shooting method, starting from the initial values of Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(, numerically integrating a solution for the resulting initial value problem, and returning the endpoint values. 

 

> h := proc(a,b)
local F;
if type(a, numeric) or type(b, numeric) then
F := dsolve({q, y(0)=a,D(y)(0)=b}, y(x), numeric);
[op([2,2],F(1)),op([3,2],F(1))];
else 'h'(a,b);
end if;
end proc:
 


In terms of Typesetting:-mrow(Typesetting:-mi(and Typesetting:-mrow(Typesetting:-mi(, the boundary conditions can be expressed via the expressions
 

 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

 

Typesetting:-mprintslash([`:=`(f, `+`(`*`(`+`(1, a), `*`(b)), `-`(`*`(3, `*`(a)))))], [`+`(`*`(`+`(1, a), `*`(b)), `-`(`*`(3, `*`(a))))])
Typesetting:-mprintslash([`:=`(g, `+`(`*`(`+`(1, h(a, b)[1]), `*`(h(a, b)[2])), `-`(`*`(3, `*`(h(a, b)[1]))), .5, `-`(`*`(`^`(h(a, b)[1], 2)))))], [`+`(`*`(`+`(1, h(a, b)[1]), `*`(h(a, b)[2])), `-`(`*...
 


It is now possible to graph the functions Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(, resulting in Figure 2.
 

 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 2   Graph of the equations for Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(, as determined by the boundary conditions of the BVP 


Figure 2 suggests that there are actually four solutions of the given BVP.
 

 

Unfortunately, the fsolve command in Maple 11 does not permit systems of procedure calls, as would be required by our definitions of Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( through the procedure Typesetting:-mrow(Typesetting:-mi(  Fortunately, this shortcoming is corrected in the next version of Maple.  Meanwhile, we need to solve a pair of nonlinear algebraic equations whose terms are calculated via the solution of an initial value problem. 

 

We propose three methods for this.  First, we implement Broyden's method as a generalization of the Secant algorithm for solving a single equation.  Second, we apply a minimization technique to Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi(, a sum of squares.  Finally, because of the special nature of the first boundary condition, we can eliminate Typesetting:-mrow(Typesetting:-mi( from the second equation, and apply the Secant method to the single resulting equation. 

 

In anticipation of all three iterative techniques, we define the following initial points, each taken from Figure 2. 

 

> Typesetting:-mrow(Typesetting:-mi(

Typesetting:-mprintslash([`:=`(P1, P2, P3, P4, Vector[column]([[-3.6], [4.2]]), Vector[column]([[-2], [6]]), Vector[column]([[-.8], [-10]]), Vector[column]([[.1], [.4]]))], [Vector[column](%id = 20111...
 

Broyden's Method 

 

Newton's method for solving the system of equations Typesetting:-mrow(Typesetting:-mi(is captured by the fixed-point scheme 

 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(  

 

where Typesetting:-mrow(Typesetting:-mi( is the Jacobian matrix for F.  Since this requires computation of derivatives, it would be tedious to implement a method for repeatedly computing Typesetting:-mrow(Typesetting:-mi(.  As an alternative, we consider Broyden's method wherein Typesetting:-mrow(Typesetting:-mi( is only computed once.  Thereafter, an approximation to Typesetting:-mrow(Typesetting:-mi( is computed, an approximation that converges to Typesetting:-mrow(Typesetting:-mi(where Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi( is a solution to the system Typesetting:-mrow(Typesetting:-mi(  

 

After obtaining Typesetting:-mrow(Typesetting:-mi( either analytically or approximately by numeric differentiation, Broyden's method thereafter replaces Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(with 

 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(  

 

as an approximation to Typesetting:-mrow(Typesetting:-mi( 

 

Our implementation of Broyden's method takes the vector Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi( as impute, along with an initial point, a stepsize Typesetting:-mrow(Typesetting:-mi( used for numerically computing Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(, and the integer Typesetting:-mrow(Typesetting:-mi( used to define the computational tolerance Typesetting:-mrow(Typesetting:-msup(Typesetting:-mn(.  Our code is simplistic, in that it does not type-checking, and does not make provision for generalization to other systems.  It is provided as a sketch to show how such a computation might be implemented. 

 

> Broyden := proc(F,X0,d,N)
local X, F0, F1, f, g, fa, ga, fb, gb, A0, A1, x0, x1, tol, k, L, Z;
x0 := X0;
X := <a,b>:
f := F[1];
g := F[2];
Z := Equate(X,x0);
F0 := eval(F,Equate(X,x0)):
fa := (eval(f,Equate(X,x0+<d,0>)) - F0[1])/d:
ga := (eval(g,Equate(X,x0+<d,0>)) - F0[2])/d:
fb := (eval(f,Equate(X,x0+<0,d>)) - F0[1])/d:
gb := (eval(g,Equate(X,x0+<0,d>)) - F0[2])/d:
A0 := Matrix([[fa,fb],[ga,gb]]):
x1 := x0 - A0^(-1).F0:
Digits := 30:
tol := 10^(-N):
for k from 1 to 10 do
F1 := eval(F, Equate(X,x1));
L := x1 - x0;
A1 := A0 + (F1 - F0 - A0.L).L^%T/(L.L);
Z := A1^(-1).F1;
if sqrt(Z.Z)<tol then break;
else x0 := x1; x1 := x1-Z; A0 := A1; F0 := F1;
end if;
end do:
Digits := 10:
x1;
end proc:
 


Using our Broyden procedure, we compute solutions for Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(.
 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 


With pairs Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi( determined, we can solve the corresponding four initial value problems, and graph their solutions.  The following commands generate the solutions and graphs,
 

 

> Typesetting:-mrow(Typesetting:-mo(
 


and the graphs themselves appear in Figure 3.
 

 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 3   Graphs of four possible solutions generated as solutions of initial value problems from pairs Typesetting:-mrow(Typesetting:-mi( computed by Broyden's method 


The four solutions found in Figure 3 can be obtained directly with Maple's dsolve command, provided an appropriate approximate solution is provided.  If we list the four approximate solutions
 

 

> Typesetting:-mrow(Typesetting:-mi(

Typesetting:-mprintslash([`:=`(L, [`+`(x, `-`(4)), `+`(`*`(3, `*`(x)), `-`(2)), `+`(`-`(`*`(10, `*`(x)))), `*`(x, `*`(`+`(1, `-`(x))))])], [[`+`(x, `-`(4)), `+`(`*`(3, `*`(x)), `-`(2)), `+`(`-`(`*`(10...
 


then the following loop computes the corresponding
dsolve solutions. 

 

> Typesetting:-mrow(Typesetting:-mo(
 


Figure 4 displays these solutions.
 

 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 4   Solutions obtained by dsolve using the approxsoln parameter 


The graphs in Figures 3 and 4 are essentially the same.
 

 

Minimizing a Sum of Squares 

 

A pair Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi( that minimizes the sum of squares 

 

> Typesetting:-mrow(Typesetting:-mi(

Typesetting:-mprintslash([`:=`(SS, `+`(`*`(`^`(`+`(`*`(`+`(1, a), `*`(b)), `-`(`*`(3, `*`(a)))), 2)), `*`(`^`(`+`(`*`(`+`(1, h(a, b)[1]), `*`(h(a, b)[2])), `-`(`*`(3, `*`(h(a, b)[1]))), .5, `-`(`*`(`^...
 


is a solution to the equations Typesetting:-mrow(Typesetting:-mi(.  We can compute such minimizing pairs with the
NLPSolve command in Maple's Optimization package.  This command provides the nonlinear simplex (Nelder-Mead) method, which does not require computation of derivatives.  The computed pairs Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi( and the values of Typesetting:-mrow(Typesetting:-mi( at the minima are given by the following loop. 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

Typesetting:-mprintslash([`:=`(SS1, [0.424383076000000013e-15, [a = -3.53593901734596372, b = 4.18299373881071544]])], [[0.424383076000000013e-15, [a = -3.53593901734596372, b = 4.18299373881071544]]]...
Typesetting:-mprintslash([`:=`(SS2, [0.278130335809999996e-15, [a = -2.00223622924222688, b = 5.99330629759563838]])], [[0.278130335809999996e-15, [a = -2.00223622924222688, b = 5.99330629759563838]]]...
Typesetting:-mprintslash([`:=`(SS3, [0.252635688370000019e-15, [a = -.773424077481638550, b = -10.2405948453594214]])], [[0.252635688370000019e-15, [a = -.773424077481638550, b = -10.2405948453594214]...
Typesetting:-mprintslash([`:=`(SS4, [0.766794320149520000e-15, [a = .115558396306133294, b = .310763844991456040]])], [[0.766794320149520000e-15, [a = .115558396306133294, b = .310763844991456040]]])
 


The first number returned in each solution is the value of Typesetting:-mrow(Typesetting:-mi( at the computed extremum.
 

 

To facilitate a comparison with the values computed by Broyden's method, we express the nonlinear simplex solutions as the vectors 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mprintslash([`:=`(V4, Vector[column]([[.115558396306133294], [.310763844991456040]]))], [Vector[column](%id = 273442176)])
 


The Euclidean norms of the differences between the two solutions are given by
 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

0.762090333232966255e-8
0.105803072908730576e-7
0.417331013238361704e-7
0.117736025877342888e-7
 


The agreement is substantial.
 

 

Secant Method 

 

If the first boundary condition is solve for Typesetting:-mrow(Typesetting:-mi(, we obtain 

 

> Typesetting:-mrow(Typesetting:-mi(
 

Typesetting:-mprintslash([`:=`(B, `+`(`/`(`*`(3, `*`(a)), `*`(`+`(1, a)))))], [`+`(`/`(`*`(3, `*`(a)), `*`(`+`(1, a))))])
 


with which Typesetting:-mrow(Typesetting:-mi( can be eliminated from the second boundary condition.  Thus, we have the single equation
 

 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

`+`(`*`(`+`(1, h(a, `+`(`/`(`*`(3, `*`(a)), `*`(`+`(1, a)))))[1]), `*`(h(a, `+`(`/`(`*`(3, `*`(a)), `*`(`+`(1, a)))))[2])), `-`(`*`(3, `*`(h(a, `+`(`/`(`*`(3, `*`(a)), `*`(`+`(1, a)))))[1]))), .5, `-`...
 


to solve.  Figure 5 provides a graph of the left-hand side of this equation.
 

 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 5   Graph of Typesetting:-mrow(Typesetting:-mi( 


There are four intercepts for the graph in Figure 5, consistent with our earlier findings on the number of solutions of the nonlinear BVP.  The equation Typesetting:-mrow(Typesetting:-mi( can be solved with the Secant method, which we have coded as the following procedure.  Our procedure includes a calculation of Typesetting:-mrow(Typesetting:-mi(, and returns both Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(.
 

 

> Secant := proc(X0,N)
local y0,k,x0,x1,x2,y1,X,Y,tol;
Digits := 30:
tol := 10^(-N):
x0 := X0;
y0 := eval(Ga,a=x0):
x1 := 1.05*x0:
unassign('X','Y');
for k from 1 to 10 do
y1 := eval(Ga,a=x1);
x2 := x1 - y1 * (x1 - x0)/(y1 - y0);
if abs(x2-x1)<tol then X:=evalf[N](x2); Y:=evalf[N](eval(B,a=x2));break:
else x0:=x1; y0:=y1; x1:=x2; y1:=eval(B,a=x1);
end if;
end do:
Digits := 10:
<X,Y>;
end proc:
 


An application of the Secant method leads to the following four solutions.
 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mprintslash([`:=`(W4, Vector[column]([[.11555840240217400632], [.31076383491891882363]]))], [Vector[column](%id = 274595412)])
 


As we did earlier, we compare these solutions with those computed by Broyden's method.
 

 

> Typesetting:-mrow(Typesetting:-mi(

 

 

 

0.
0.
0.
0.
 

The solutions appear to be identical. 

 


Legal Notice: The copyright for this application is owned by the author(s). Neither Maplesoft nor the author are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.
 

Image