<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE article  PUBLIC "-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN" "http://dtd.nlm.nih.gov/publishing/3.0/journalpublishing3.dtd"><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="3.0" xml:lang="en" article-type="research article"><front><journal-meta><journal-id journal-id-type="publisher-id">JAMP</journal-id><journal-title-group><journal-title>Journal of Applied Mathematics and Physics</journal-title></journal-title-group><issn pub-type="epub">2327-4352</issn><publisher><publisher-name>Scientific Research Publishing</publisher-name></publisher></journal-meta><article-meta><article-id pub-id-type="doi">10.4236/jamp.2022.103059</article-id><article-id pub-id-type="publisher-id">JAMP-116069</article-id><article-categories><subj-group subj-group-type="heading"><subject>Articles</subject></subj-group><subj-group subj-group-type="Discipline-v2"><subject>Physics&amp;Mathematics</subject></subj-group></article-categories><title-group><article-title>
 
 
  A Comparison of Different Numerical Schemes to Solve Nonlinear First Order ODE
 
</article-title></title-group><contrib-group><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Mahjoub</surname><given-names>A. Elamin</given-names></name><xref ref-type="aff" rid="aff1"><sup>1</sup></xref></contrib><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>Sami</surname><given-names>H. Altoum</given-names></name><xref ref-type="aff" rid="aff2"><sup>2</sup></xref><xref ref-type="corresp" rid="cor1"><sup>*</sup></xref></contrib></contrib-group><aff id="aff2"><addr-line>Academy of Engineering Sciences, Umm Al-Qura University, Mecca, Saudi Arabia</addr-line></aff><aff id="aff1"><addr-line>Department of Mathematics, University College of Umluj, Tabuk University, Tabuk, Saudi Arabia</addr-line></aff><pub-date pub-type="epub"><day>02</day><month>03</month><year>2022</year></pub-date><volume>10</volume><issue>03</issue><fpage>865</fpage><lpage>876</lpage><history><date date-type="received"><day>18,</day>	<month>January</month>	<year>2022</year></date><date date-type="rev-recd"><day>19,</day>	<month>March</month>	<year>2022</year>	</date><date date-type="accepted"><day>21,</day>	<month>March</month>	<year>2022</year></date></history><permissions><copyright-statement>&#169; Copyright  2014 by authors and Scientific Research Publishing Inc. </copyright-statement><copyright-year>2014</copyright-year><license><license-p>This work is licensed under the Creative Commons Attribution International License (CC BY). http://creativecommons.org/licenses/by/4.0/</license-p></license></permissions><abstract><p>
 
 
  In this paper, the nonlinear first order ordinary differential equation will be considered. Three simplest numerical stencils are presented to solve this equation. We deduce that the numerical method of Trapezoidal is a good technique, which helped us to find an approximation of the exact solution with small error.
 
</p></abstract><kwd-group><kwd>Ordinary Differential Equation</kwd><kwd> Euler Method</kwd><kwd> Numerical Approximation</kwd></kwd-group></article-meta></front><body><sec id="s1"><title>1. Introduction</title><p>Consider the nonlinear first order ordinary differential equation</p><p>D ( y ) ( x ) = f ( x , y ( x ) ) . (1)</p><p>Ordinary differential equations occur in many scientific disciplines, including physics, chemistry, biology, and economics. In mathematics, an ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions. Ordinary differential equations (ODEs) arise in many contexts of mathematics and social and natural sciences. Mathematical descriptions of change use differentials and derivatives. Various differentials, derivatives, and functions become related via equations, such that a differential equation is a result that describes dynamically changing phenomena, evolution, and variation. Some ODEs can be solved explicitly in terms of known functions and integrals. When that is not possible, the equation for computing the Taylor series of the solutions may be useful. For applied problems, numerical methods for ordinary differential equations can supply an approximation of the solution. In addition, some methods in numerical partial differential equations convert the partial differential equation into an ordinary differential equation, which must then be solved. Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic explicit method for numerical integration of ordinary differential equations and is the simplest Runge-Kutta method [<xref ref-type="bibr" rid="scirp.116069-ref1">1</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref2">2</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref3">3</xref>]. The backward Euler method (or implicit Euler method) is one of the most basic numerical methods for the solution of ordinary differential equations, the backward Euler method has order one. This means that the local truncation error (defined as the error made in one step) is O ( h 2 ) . The error at a specific time t is O ( h ) . It is similar to the (standard) Euler method, but differs in that it is an implicit method. This differs from the (forward) Euler method in that the latter uses f ( t k , y k ) in place of f ( t k + 1 , y k + 1 ) . The backward Euler method is an implicit method: the new approximation y k + 1 appears on both sides of the equation, and thus the method needs to solve an algebraic equation for the unknown y k + 1 . For non-stiff problems, this can be done with fixed-point iteration [<xref ref-type="bibr" rid="scirp.116069-ref4">4</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref5">5</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref6">6</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref7">7</xref>]. This paper is organized as follows: In Chapter 2 we presented three simplest numerical stencils. In Chapter 3 we introduce a numerical formulation of ODE and apply examples.</p></sec><sec id="s2"><title>2. Three Simplest Numerical Stencils</title><sec id="s2_1"><title>2.1. Forward Euler Stencil</title><p>The purpose of this paper is to derive the three simplest numerical stencils to solve the first order equation</p><p>D ( y ) ( x ) = f ( x , y ( x ) ) . (2)</p><p>In this expression, f is assumed to be a known function of the independent variable x and the function that we are trying to solve for y ( x ) . The simplest numerical stencils to solve this equation will give us an approximation to y at some point x = X + h given some knowledge of y at x = X . All of these stencils are based on the Taylor series approximation for y ( x ) about x = X to linear order:</p><p>y ( x ) = y ( X ) + D ( y ) ( X ) ( x − X ) + O ( ( x − X ) 2 ) . (3)</p><p>Let us define h as the difference between x and X, and then get rid of x in the above expression:</p><p>h = x − X</p><p>y ( h + X ) = y ( X ) + D ( y ) ( X ) h + O ( h 2 ) . (4)</p><p>Now, we can remove the first derivative of y by making use of the differential equation:</p><p>D ( y ) ( X ) = f ( X , y ( X ) )</p><p>y ( X + h ) = y ( X ) + f ( X , y ( X ) ) h + O ( h 2 ) (5)</p><p>The forward Euler algorithm involves discarding the terms of order h<sup>2</sup> and higher in this expression and hence obtaining an approximation to y ( X + h ) in terms of y ( X ) . We can accomplish this by converting the above series into a polynomial using the convert/polynom command. Also, we can take X = x i as the i<sup>th</sup> point on an evenly spaced lattice x i = x 0 + i h , where h is the lattice spacing and i is an integer; it then follows that “ X + h = x i + 1 ”. Furthermore, we label the numeric approximation of y ( x ) at x = x i a “ y i and approx; y ( x i ) ”. Implementing these steps and notational changes:</p><p>y ( h + X ) = y ( X ) + f ( X , y ( X ) ) h</p><p>[ y ( h + X ) = y i + 1 , y ( X ) = y i , X = x i ]</p><p>y i + 1 = y i + f ( x i , y i ) h (6)</p><p>The last expression is the forward Euler stencil for solving ode. It is called an explicit algorithm because the quantity we wish to calculate y i + 1 is given explicitly in terms of y i .</p></sec><sec id="s2_2"><title>2.2. Backward Euler Stencil</title><p>The backward Euler stencil is obtained in a similar fashion, except we identify h, y i , and y i + 1 differently:</p><p>h = X − x</p><p>y ( − h + X ) = y ( X ) − ( D ( y ) ) ( X ) h + O ( ( − h ) 2 )</p><p>y ( − h + X ) = y ( X ) − f ( X , y ( X ) ) h + O ( ( − h ) 2 )</p><p>y ( − h + X ) = y ( X ) − f ( X , y ( X ) ) h</p><p>y ( − h + X ) = y i , y ( X ) = y i + 1 , X = x i + 1</p><p>y i = y i + 1 − f ( x i + 1 , y i + 1 ) h . (7)</p><p>This stencil is explicit because it will not in general be possible to algebraically isolate what we want to calculate, i.e. y i + 1 = ( x i + 1 ) , except for very specific forms of f. We can rearrange the stencils to better demonstrate their geometric meaning:</p><p>f ( x i , y i ) = − y i + y i + 1 h</p><p>f ( x i + 1 , y i + 1 ) = ( − y i + y i + 1 ) / h . (8)</p><p>These forms illustrate that the forward Euler stencil (first equation) is a simple approximation of the first derivative of y ( x ) in the interval x ∈ [ x i , x i + 1 ] using ode evaluated at the left hand side of the interval. Conversely, the backward Euler stencil uses the differential equation to evaluate the derivative at the righthand side of the interval [<xref ref-type="bibr" rid="scirp.116069-ref8">8</xref>] [<xref ref-type="bibr" rid="scirp.116069-ref9">9</xref>].</p></sec><sec id="s2_3"><title>2.3. Trapezoidal Method</title><p>There is another stencil we can derive that is the average of these two approaches:</p><p>1 2 f ( x i , y i ) + 1 2 f ( x i + 1 , y i + 1 ) = − y i + y i + 1 h . (9)</p><p>This is called the trapezoidal method, and it uses ode evaluated at both ends of the interval to approximate y ′ ( x ) . Like the backward Euler stencil, it represents an implicit scheme.</p></sec></sec><sec id="s3"><title>3. Numerical Formulation</title><p>As an example, we can look at what these stencils look like for the special case of f ( x , y ) = λ y , where λ is a constant:</p><p>λ   y i = − y i + y i + 1 h     Forward Euler</p><p>λ   y i + 1 = − y i + y i + 1 h     Backward Euler</p><p>λ   y i + 1 2   λ   y i + 1 = − y i + y i + 1 h     Trapezoidal .</p><p>For this special case, we see that it is possible to isolate y i + 1 for each stencil. Let’s do this using a loop:</p><p>y i + 1 = y i ( h λ + 1 )     Forward Euler</p><p>y i + 1 = − y i h λ − 1     Backward Euler</p><p>y i + 1 = − y i ( h λ + 2 ) h λ − 2     Trapezoidal .</p><p>Now, lets go back to the general case by undefined f:</p><p>f ( x i , y i ) = ( − y i + y i + 1 ) / h</p><p>f ( x i + 1 , y i + 1 ) = ( − y i + y i + 1 ) / h</p><p>( 1 / 2 ) f ( x i , y i ) + ( 1 / 2 ) f ( x i + 1 , y i + 1 ) = ( − y i + y i + 1 ) / h .</p><p>We now want to determine what the error is in each stencil. To do this, we need to rewrite each of them in the standard form</p><p>y i + 1 − y i − h Φ ( x i , x i + 1 , y i , y i + 1 ) = 0</p><p>therefore,</p><p>y i + 1 − y i − f ( x i , y i ) h = 0</p><p>y i + 1 − f ( x i + 1 , y i + 1 ) h − y i = 0</p><p>y i + 1 − h ( ( 1 / 2 ) ∗ f ( x i , y i ) + ( 1 / 2 ) f ( x i + 1 , y i + 1 ) ) − y i = 0.</p><p>Note that these relations define our numeric stencils in these sense that they give exact relations between the approximations y i and y i + 1 . But if we replace the approximations with the exact values of y ( x ) they are supposed to represent, the above will represent only approximate equalities. That is, if we make the changes x i → x , x i + 1 → x + h , y i → y ( x ) , y i + 1 → y ( x + h ) , the left hand sides of the above equations will only be approximately equal to zero. We call the magnitude of the discrepancy the “one step error” or “local error” in the stencil. Hence, the one step error in the various stencils will be:</p><p>y i = y ( x ) , y i + 1 = y ( x + h ) , x i = x , x i + 1 = x + h</p><p>Error Forward Euler = y ( x + h ) − y ( x ) − f ( x , y ( x ) ) h</p><p>Error Backward Euler = y ( x + h ) − y ( x ) − f ( x , y ( x ) ) h</p><p>Error Trapezoidal = y ( x + h ) − f ( x + h , y ( x + h ) ) h − y ( x )</p><p>To estimate the magnitudes of these errors, we expand each of the above expression as a power series about h = 0 (since we are implicitly assuming h is a small quantity):</p><p>Error Forward Euler = ( − f ( x , y ( x ) ) + D ( y ) ( x ) ) h + 1 / 2 ( D ( 2 ) ) ( y ) ( x ) h 2     + 1 / 6 ( D ( 3 ) ) ( y ) ( x ) h 3 + O ( h 4 )</p><p>Error Backward Euler = ( − f ( x , y ( x ) ) + D ( y ) ( x ) ) h + ( α 1 ) h 2 + α 2 − α 3 + O ( h 4 )</p><p>where,</p><p>α 1 = 1 / 2 ( D ( 2 ) ) ( y ) ( x ) − D 1 ( f ) ( x , y ( x ) ) − D 2 ( f ) ( x , y ( x ) ) D ( y ) ( x )</p><p>α 2 = 1 / 6 ( D ( 3 ) ) ( y ) ( x ) − 1 / 2 ( D ( y ) ( x ) ) 2 ( D 2,2 ) ( f ) ( x , y ( x ) )     − 1 / 2 D 2 ( f ) ( x , y ( x ) ) ( D ( 2 ) ) ( y ) ( x )</p><p>α 3 = ( D 1 , 2 ) ( f ) ( x , y ( x ) ) D ( y ) ( x ) − 1 / 2 ( D 1 , 1 ) ( f ) ( x , y ( ( x ) ) ) h 3</p><p>Error Trapezoidal = ( β 1 ) h + ( β 2 ) h 2 + ( β 3 − β 4 ) h 3 + O ( h 4 )</p><p>where,</p><p>β 1 = − f ( x , y ( x ) ) + D ( y ) ( x )</p><p>β 2 = 1 / 2   ( D ( 2 ) ) ( y ) ( x ) − 1 / 2 D 1 ( f ) ( x , y ( x ) ) − 1 / 2 D 2 ( f ) ( x , y ( x ) ) D ( y ) ( x )</p><p>β 3 = 1 / 6 ( D ( 3 ) ) ( y ) ( x ) − 1 / 4 ( D ( y ) ( x ) ) 2 ( D 2,2 ) ( f ) ( x , y ( x ) )</p><p>β 4 = 1 / 4 D 2 ( f ) ( x , y ( x ) ) ( D ( 2 ) ) ( y ) ( x ) − 1 / 2 ( D 1,2 ) ( f ) ( x , y ( x ) ) D ( y ) ( x )     − 1 / 4 ( D 1,1 ) ( f ) ( x , y ( x ) ) .</p><p>Notice that first, second and third derivatives of y appear explicitly in these expressions. The first derivative terms can be removed by making use of ode. The second derivative can also be removed by examining the derivative of ode:</p><p>( D ( 2 ) ) ( y ) ( x ) = D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) d d x y ( x )</p><p>and</p><p>d d x y ( x ) = f ( x , y ( x ) )</p><p>( D ( 2 ) ) ( y ) ( x ) = D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) .</p><p>Similarly, the third derivative can be removed</p><p>( D ( 3 ) ) ( y ) ( x ) = ( D 1,1 ) ( f ) ( x , y ( x ) ) + ( D 1,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) + ( ( D 1,2 ) ( f ) ( x , y ( x ) ) + ( D 2,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) ) f ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) ( D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) ) .</p><p>We now substitute our formulae for the derivatives of y into the error expressions:</p><p>Error Forward Euler = γ 1 O ( h 2 ) + ( γ 2 + γ 3 + γ 4 ) O ( h 3 ) + O ( h 4 ) (10)</p><p>where,</p><p>γ 1 = ( 1 / 2 D 1 ( f ) ( x , y ( x ) ) + 1 / 2 D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) )</p><p>γ 2 = 1 / 6 ( D 1,1 ) ( f ) ( x , y ( x ) ) + 1 / 6 ( D 1,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) )</p><p>γ 3 = 1 / 6 ( ( D 1,2 ) ( f ) ( x , y ( x ) ) + ( D 2,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) ) f ( x , y ( x ) )</p><p>γ 4 = 1 / 6 D 2 ( f ) ( x , y ( x ) ) ( D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) )</p><p>Error Backward Euler = γ 5 ( h 2 ) + ( γ 6 + γ 7 − γ 8 − γ 9 ) h 3 + O ( h 4 ) (11)</p><p>where,</p><p>γ 5 = − ( 1 / 2 D 1 ( f ) ( x , y ( x ) ) − 1 / 2 D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) )</p><p>γ 6 = − 1 / 3 ( D 1,1 ) ( f ) ( x , y ( x ) ) − 5 / 6 ( D 1,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) )</p><p>γ 7 = 1 / 6 ( ( D 1,2 ) ( f ) ( x , y ( x ) ) + ( D 2,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) ) f ( x , y ( x ) )</p><p>γ 8 = 1 / 3 D 2 ( f ) ( x , y ( x ) ) ( D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) )</p><p>γ 9 = 1 / 2 ( f ( x , y ( x ) ) ) 2 ( D 2,2 ) ( f ) ( x , y ( x ) )</p><p>Error Trapezoidal = ( γ 10 + γ 11 − γ 12 − γ 13 ) h 3 + O ( h 4 ) (12)</p><p>where,</p><p>γ 10 = − 1 / 12 ( D 1,1 ) ( f ) ( x , y ( x ) ) − 1 / 3 ( D 1,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) )</p><p>γ 11 = 1 / 6 ( ( D 1,2 ) ( f ) ( x , y ( x ) ) + ( D 2,2 ) ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) ) f ( x , y ( x ) )</p><p>γ 12 = 1 / 12 D 2 ( f ) ( x , y ( x ) ) ( D 1 ( f ) ( x , y ( x ) ) + D 2 ( f ) ( x , y ( x ) ) f ( x , y ( x ) ) )</p><p>γ 13 = 1 / 4 ( f ( x , y ( x ) ) ) 2 ( D 2,2 ) ( f ) ( x , y ( x ) ) .</p><p>We see that the local error for the forward and backward schemes is O ( h 2 ) . Furthermore, the leading order terms for those stencils are the negative of the other one. Since the trapezoidal scheme is the average of the forward and backward algorithms, this explains why the leading order error for the trapezoidal scheme is O ( h 3 ) . In other words, the trapezoidal scheme is more accurate than the other two. Note that if all we are after is the magnitude of the leading order terms in the errors, we can just expand the above in a low order series:</p><p>Error Forward Euler = O ( h 2 ) (13)</p><p>Error Backward Euler = O ( h 2 ) (14)</p><p>Error Trapezoidal = O ( h 3 ) . (15)</p><p>We now turn our attention to actual numerical algorithms employing these stencils. Since the forward Euler scheme is explicit, it is particularly easy to develop some code for it that works for arbitrary functions f. It is useful to rewrite the stencil with y i + 1 isolated on the LHS:</p><p>y i + 1 = y i + f ( x i , y i ) h</p><p>Now we can transcribe this as a mapping that takes the step size and the old values of y i and x i and returns the new value y i + 1 y new : = ( h , x old , y old ) → y old + f ( x old , y old ) h We could have equivalently accomplished this by using the unapply command, which converts expression into mappings without having to re-write them manually: y new : = ( h , y 2 , y 3 ) → y 3 + f ( y 2 , y 3 ) h Here is a procedure that calculates the numerical solution of ode in the interval x ∈ [ 0,1 ] once the form of f ( x , y ) has been fixed. Its arguments are initial data in the form y ( 0 ) = y 0 and the number of steps N. The output is a list of points, which we compare in the plot to the output generated by numeric for the same problem.</p><p>Example: Solve the equation f : = ( x , y ) → − y 2 + 2 x</p><p>D ( y ) ( x ) = − ( y ( x ) ) 2 + 2 x ,   y ( 0 ) : = 2   ,   N : = 15</p><p>Solution:</p><p>data : = [ 0,2 ] , [ 0.06666666667,1.733333333 ] , [ 0.1333333333,1.541925926 ] , [ 0.2000000000,1.401201333 ] , [ 0.2666666667,1.296976988 ] , [ 0.3333333334,1.220389256 ] , [ 0.4000000001,1.165543705 ] , [ 0.4666666668,1.128310896 ] ,</p><p>[ 0.5333333335,1.105660753 ] , [ 0.6000000002,1.095272817 ] , [ 0.6666666669,1.095297981 ] , [ 0.7333333336,1.104208359 ] , [ 0.8000000003,1.120701063 ] , [ 0.8666666670,1.143636338 ] , [ 0.9333333337,1.171998289 ] , [ 1.000000000,1.204870734 ]</p><p>Of course <xref ref-type="fig" rid="fig1">Figure 1</xref> it is really comparing the results of two numeric approximations to the true solution of the problem. It is also useful to compare numeric answers to analytic results (if available). For the above choice of f ( x , y ) there is an analytic solution in terms of Airy functions:</p><p>Analytical     solution : = 2 1 3 ( − ξ 1 ξ 2 + A i r y B i ( 1, 2 3 x ) ) − ξ 3 ξ 4 + A i r y B i ( 2 3 x ) (16)</p><p>where</p><p>ξ 1 = − A i r y A i ( 1 , 2 1 3 x ) ( ( 4 ) 3 5 / 6 π − 3 Γ ( 2 / 3 ) 2 3 2 / 3 2 1 / 3 ) / ( ( 3 ) 3 1 / 6 Γ ( 2 / 3 ) 2 2 1 / 3 + 4 π 3 1 / 3 )</p><p>ξ 2 = 3   3 6 ( Γ ( 2 / 3 ) ) 2 2 3 + 4 π 3 3</p><p>ξ 3 = ( ( 4 ) 3 5 / 6 π − 3 ( Γ ( 2 / 3 ) ) 2 3 2 / 3 2 3 ) A i r y A i ( 2 3 x )</p><p>ξ 4 = 3   3 6 ( Γ ( 2 / 3 ) ) 2 2 3 + 4 π 3 3 .</p><p>Here is some code that generates a movie comparing how the numerical solution converges to the analytic solution as the number of cells is increased:</p><p>We would also like to examine the performance of the backward and trapezoidal stencils see <xref ref-type="fig" rid="fig2">Figure 2</xref>, so let’s make a simple choice of f ( x , y ) that admits an analytic solution allows each stencil to be solve for y i + 1 explicitly:</p><p>f : = ( x , y ) → λ y ,     y ( 0 ) : = y 0</p><p>D ( y ) ( x ) = λ y ( x )</p><p>Analytic     Solution : = y ( 0 ) e λ x</p><p>Forward     Euler : y i + 1 = h λ   y i + y i</p><p>Backward     Euler : y i + 1 = y i − h λ + 1</p><p>Trapezoidal : y i + 1 = y i + 1 / 2 h λ y i 1 − 1 / 2 h λ .</p><p>As for the above code, it will be useful to realize each stencil as a mapping with arguments h , λ and y i . We can do this using a loop and the unapply command:</p><p>stencil 1 : = ( h , λ , y 3 ) → h λ   y 3 + y 3</p><p>stencil 2 : = ( h , λ , y 3 ) → y 3 − h λ + 1</p><p>stencil 3 : = ( h , λ , y 3 ) → y 3 + 1 / 2 h λ y 3 1 − 1 / 2 h λ .</p><p>Conversely, we could have accomplished the same thing in one line by using the map command, which applies the same mapping onto each element of a list (in this case we are applying operations onto each element of Stencils to generate a new list stencil):</p><p>stencil : = ( h , λ , y 3 ) → ( h λ y 3 ) + y 3 ,</p><p>( h , λ , y 3 ) → y 3 − h λ + 1 ,</p><p>( h , λ , y 3 ) → y 3 + 1 / 2 h λ y 3 1 − 1 / 2 h λ .</p><p>It is interesting to note that even though each of the stencils give different expressions for y i + 1 , they actually agree with one another to order ( h 2 ). To see this, let’s perform some Taylor series expansions:</p><p>Forward   Euler   y i + 1 = h λ y i + y i</p><p>Backward   Euler   y i + 1 = y i + h λ y i + O ( h 2 )</p><p>Trapezoidal   y i + 1 = y i + λ y i h + O ( h 2 ) .</p><p>Now, here is a procedure Euler solution that calculates a numeric solution for y ( x ) for x ∈ [ 0,1 ] using N cells and assuming y ( 0 ) = y 0 and a given value of λ . The value of choice dictates which stencil is used: 1 for forward Euler, 2 for backward Euler, and 3 for trapezoidal. Here is a plot of the numeric output for each stencil compared to the actually solution (because we are going to plot 4 curves on our graph). It seems as if the trapezoidal method performs much better than the other two. N : = 15 , y 0 : = 2 , λ : = − 4 .</p><p>We can quantify exactly how well the various stencils are doing by comparing the numeric and actual values for y at some fixed value of x, say x = 1 . We call this the global error in the numeric solution at x = 1 , which we denote by ε In this procedure, note that the [ N + 1 ] [ 2 ] suffix after Euler solution ( y 0 , N , λ ) has the effect of picking out the last element of the list (which is itself a list of 2 quantities), and then picking out the second element of that sub-list (which is the numerical answer for y ( 1 ) . We now generate a log-log plot of the global errors for each stencil as a function of N.</p><p>For N ≥ 100 , the error curves look linear on this log-log plot. This implies that above some threshold number of steps, the errors obey an approximate power law ε ≃ a N b where a and b are constants. We can determine the values of these parameters by fitting a power law to our error data using statistics. <xref ref-type="fig" rid="fig3">Figure 3</xref> and <xref ref-type="fig" rid="fig4">Figure 4</xref> illustrate four solutions Forward Euler, Backward Euler, Trapezoidal and analytic solution and their errors.</p><p>(PowerFit). We first need to regenerate our data for N ≥ 100 , the range for which we believe a power law ought to be valid. Then, we use the Statistics (PowerFit) command to perform the fit. Note that this function requires the input to be a matrix, which is why we use the convert Matrix structure. The raw output</p><p>of the fitting algorithms is shown for each stencil, as well as the value of b in the power-law ε ≃ a N b .</p><p>Forward Euler : = [ 0.284665679953286 N 0.995851778662443 , ( − 1.25643984007394 − 0.995851778662443 ) ] ,</p><p>b = − 0.995851778662443 .</p><p>Backward Euler : = [ 0.301341961047440 N 1.00397729890028 , ( − 1.19950957587335 − 1.00397729890028 ) ] ,</p><p>b = − 1.00397729890028 .</p><p>Trapezoidal : = [ 0.196184023674760 N 2.00065878415391 , ( − 1.62870216393926 − 2.00065878415391 ) ] ,</p><p>b = − 2.00065878415391 .</p><p>We see that for asymptotically large numbers of steps N ≫ 1 , the global errors are O ( N − 1 ) = O ( h ) for the forward and backward Euler stencils, and O ( N − 2 ) = O ( h 2 ) for the trapezoidal method. This confirms the general expectation that for a stencil with one step error O ( h p ) , we expect the global error to obey</p><p>global   error ∼ ( number   of   steps ) &#215; ( one-step   error   for   each   step ) = N &#215; O ( h p ) = O ( h − 1 ) &#215; O ( h p ) = O ( h p − 1 ) .</p><p>Recall that we showed in (13, 14, 15) above that p = 2 for forward and backward Euler, and p = 3 for the trapezoidal algorithm.</p></sec><sec id="s4"><title>4. Conclusion</title><p>In this paper, we have investigated the solution of ODEs using three approaches. The first one, is the Forward Euler method, the second is Backward Euler method and the third one is a numerical method based on Trapezoidal method. In general, we find the numerical method of Trapezoidal method, is a good technique that helped us to find an approximation of the exact solution with small error. In the last, we note the Trapezoidal method.</p></sec><sec id="s5"><title>Acknowledgements</title><p>The author would like to thank the reviewers for their valuable comments which have improved the paper.</p></sec><sec id="s6"><title>Conflicts of Interest</title><p>The authors declare that they have no competing interests.</p></sec><sec id="s7"><title>Cite this paper</title><p>Elamin, M.A. and Altoum, S.H. (2022) A Comparison of Different Numerical Schemes to Solve Nonlinear First Order ODE. Journal of Applied Mathematics and Physics, 10, 865-876. https://doi.org/10.4236/jamp.2022.103059</p></sec></body><back><ref-list><title>References</title><ref id="scirp.116069-ref1"><label>1</label><mixed-citation publication-type="other" xlink:type="simple">Altoum, S.H. (2018) Solution of Second Order Ordinary Differential Equation Associated with Toeplitz and Stiffness Matrices. American Journal of Applied Sciences, 15, 416-422. https://doi.org/10.3844/ajassp.2018.416.422</mixed-citation></ref><ref id="scirp.116069-ref2"><label>2</label><mixed-citation publication-type="other" xlink:type="simple">Altoum, S.H. (2018) Analytical and Three Numerical Approach to Solve Second Order ODEs. International Journal of Advanced Scientific and Technical Research, 4, 51-67. https://doi.org/10.26808/rs.st.i8v4.06</mixed-citation></ref><ref id="scirp.116069-ref3"><label>3</label><mixed-citation publication-type="other" xlink:type="simple">Williams, D. (1991) Probability with Martingales. Cambridge University Press, Cambridge, UK. https://doi.org/10.1017/CBO9780511813658</mixed-citation></ref><ref id="scirp.116069-ref4"><label>4</label><mixed-citation publication-type="other" xlink:type="simple">Baker, I.N. and Rippon, P.J. (1983) Convergence of Infinite Exponentials. Annales Academiac Scentiarium Fennicae, Series AI Mathematika, 8, 179-186. https://doi.org/10.5186/aasfm.1983.0805</mixed-citation></ref><ref id="scirp.116069-ref5"><label>5</label><mixed-citation publication-type="other" xlink:type="simple">Feynman, R.P. (1986) Surely You’re Joking, Mr. Feynman. Bantam Books, New York, USA, 71-72.</mixed-citation></ref><ref id="scirp.116069-ref6"><label>6</label><mixed-citation publication-type="other" xlink:type="simple">Kuo, H.-H. (1996) White Noise Distribution Theory. CRC Press, Boca Raton.</mixed-citation></ref><ref id="scirp.116069-ref7"><label>7</label><mixed-citation publication-type="other" xlink:type="simple">Rudin, W. (1976) Principles of Mathematical Analysis. McGraw Hill, USA.</mixed-citation></ref><ref id="scirp.116069-ref8"><label>8</label><mixed-citation publication-type="journal" xlink:type="simple"><name name-style="western"><surname>Euler</surname><given-names> L. </given-names></name>,<etal>et al</etal>. (<year>1778</year>)<article-title>De formulis exponentialibus replicatus</article-title><source> Acta Academiac Petropolitenae</source><volume> 1</volume>,<fpage> 38</fpage>-<lpage>60</lpage>.<pub-id pub-id-type="doi"></pub-id></mixed-citation></ref><ref id="scirp.116069-ref9"><label>9</label><mixed-citation publication-type="other" xlink:type="simple">Folland, G.B. (1999) Real Analysis: Modern Techniques and Their Applications. 2nd Edition, Wiley, Hoboken, USA.</mixed-citation></ref></ref-list></back></article>