Solving Ordinary Non Linear Differential Equations

Introduction

Let [a,b] be in R and y0 be a real value. Then we define function f: f:[a,b]×RR(t,y)f(t,y) We wish to solve the following Cauchy problem, for all: {y(t)=f(t,y)t[a,b]y(a)=y0 We have to find a continuous function y, derivable on [a,b] such that it yields: forallt[a,b],y(t)=f(t,y(t)) with y(a)=y0.

Function y(t)=t24 is a solution of the following problem: {y(t)=y(t) pour t[0,4]y(0)=0 Nevertheless, we can show that for any α>0 functions: {y(t)=0 for t[0,α]y(t)=(tα)24 si t>α are also solutions to this Cauchy problem... The solution is not unique.

Function f:[a,b]×RR is said lipschitz in y if there exists a real value L>0 called Lipschitz's constant such that for all t[a,b], y1 and y2 of R we have f(t,y1)f(t,y2)L.y1y2
[Cauchy-Lipschitz]. Let (C){y(t)=f(t,y)y(a)=y0 be a Cauchy problem. If f:[a,b]×RR satisfies the following assumptions
  • f is continuous on [a,b]×R
  • f is lipschitz on y
then the Cauchy problem has a unique solution of class C1.

Euler Algorithm (1768)

In general, we can't find the exact solution y of the Cauchy problem C. It's why we try to find an approximation y1 of y(t0+h)=y(t1) where h>0 is the step of the numerical method. We set ti=ti1+h for i=1,,n with t0=a and tn=b. The Euler algorithm try to approximate the exact solution of y(t0+h) at t0+h by using the tangent value of y(t) at (t0,y0). The tangent equation can be written by: yy0=y(t0)(xt0) Hence, for x=t0+h we have: y1=y0+f(t0,y0).h We then solve this new Cauchy problem: (C1){y(t)=f(t,y)y(t1)=y1 which allows us to approximate y(t2) by y2 at y(t1+h): y2=y1+h.f(t1,y1). Iteratively, we obtain the n values yi which approximate function y at each ti.

Exercise [Non Linear Equation of order 1]
Let us given the following Cauchy problem: {y(t)=1+(y(t))2,t[0,1],y(0)=0.

  • Solve this Cauchy problem with the scalar Euler algorithm. Draw the solution curve with Python. For this you have to define the following function:

  • 
    import numpy as np
    import matplotlib.pyplot as plt
    
    """ ODE function """
    def f(t,y):
        return 1+y*y
    
    """ Euler Algorithm"""
    def Euler(y0,a,b,nb):
        h=(b-a)/nb
        t=a
        X=[a]
        Y=[y0]
        y1=y0
        for i in range(1,nb):
            #TO DO
            Y.append(y2)
            X.append(t)
            y1=y2
        return X,Y
    
    y0= 0.0
    a = 0.0
    b = 1.0
    niter = 150
    x,y = Euler(y0,a,b,niter)
    plt.plot(x,y,'r', label=r"Euler method", linestyle='--')
    plt.legend(loc='upper left', bbox_to_anchor=(1.1, 0.95),fancybox=True, shadow=True)
    plt.show()
    
    
  • Solve this Cauchy problem with the scalar Runge-Kutta of order 2 algorithm (see Runge Kutta Algorithms ).

  • Solve this Cauchy problem with the scalar Runge-Kutta of order 4 algorithm.

  • Solve this Cauchy problem with the second order Adams-Moulton algorithm.

Exercise [Ricatti (1712) Non Linear Equation of order 1]
Let us given the following Cauchy problem: {y(t)=t2+(y(t))2,t[0,12],y(0)=0.

  • Solve this Cauchy problem with the scalar Euler and Runge-Kutta algorithm.

  • Draw the solution curves with Python.

  • Compare the approximted values of ynb with the reference solution
    y(12)= 0.041 791 146 154 681 863 220 76

Exercise [Numerical instability]
Let us given the following Cauchy problem: {y(t)=3y(t)4et,t[0,10],y(0)=1.

  1. Use the fourth-order Runge-Kutta method with step h=0.1 to solve this problem.
  2. Compare the result with the analytical solution y(t)=et.

Solving a second order differential Cauchy problem

Let us give (D){y=f(t,y,y)y(t0)=y0y(t0)=y0 where function f is of class C1. By setting V(t)=(y(t)y(t)) we obtain the following first order Cauchy problem: {dV(t)dt=(yy)=(yf(t,y,y))=F(t,V)V(t0)=(y0y0) Thus, we can solve this problem by using the Euler algorithm where the first iteration is given by: V1=V0+h.F(t0,V0)

Exercise [Non Linear Equation of order 2]
We want to solve this second order non linear differential equation: {ml2θ(t)=mglsin(θ(t))+c(t) pour t[0,T],θ(0)=0θ(0)=1. with torque c(t)=Asin(t), m=0.1kg, l=30cm, T=5s and A=0.1N.m.

  1. Solve this Cauchy problem with the Euler vector algorithm.

  2. 
    import numpy as np
    
    """ Euler Vector Algorithm"""
    
    def F(t,V):
        L=[V[1], # TO DO]
        return np.array(L)
    
    def NEuler(y0,a,b,nb):
        h=(b-a)/nb
        t=a
        X=[a]
        Y=[V0[0]]
        V1=V0
        for i in range(1,nb):
            #TO DO
            t=t+h
            Y.append(V2[0])
            X.append(t)
            V1=V2
        return X,Y
    
    
  3. Solve this Cauchy problem with the Runge-Kutta of order 2 algorithm.

  4. Solve this Cauchy problem with the Runge-Kutta of order 4 algorithm.

  5. Solve this Cauchy problem with the second order Adams-Moulton algorithm.

Exercise [Mass Spring]

Consider the mass–spring system where dry friction is present between the block and the horizontal surface. The frictional force has a constant magnitude μ.m.g (μ is the coefficient of friction) and always opposes the motion. We denote by y the measure from the position where the spring is unstretched.

We assume that k=3000N/m, μ=0.5, g=9.80665m/s², y0=0.1m and m=6.0 kg.
  1. Write the differential equation of the motion of the block
  2. If the block is released from rest at y=y0, verify by numerical integration that the next positive peak value of y is y04μ.m.gk. This relationship can be derived analytically.

Exercise [Iron block]

The magnetized iron block of mass m is attached to a spring of stiffness k and free length L. The block is at rest at x=L. When the electromagnet is turned on, it exerts a repulsive force F=c/x2 on the block.

We assume that c=5N·m², k=120 N/m, L=0.2m and m=1.0 kg.
  1. Write the differential equation of the motion of the mass
  2. Determine the amplitude and the period of the motion by numerical integration

Exercise [Magnus Effect applied to a ball]
On June 3, 1997, Roberto Carlos scored against France football team a free kick goal with an improbable effect. We want to simulate in this exercice what happened that day. For this, we set:

  • ρ=1.2 Kg.m3 : the air density
  • R=0.11 m the radius of the ball
  • m=0.430 Kg : the mass of the ball
  • M(t)=(x(t),y(t),z(t)): the location of the ball at t0
  • v0=36 m.s1 : the initial speed of the ball
  • v(t) : the speed of the ball at t0.
  • v(t) : the speed vector of the ball at t0.
  • ω=(0,0,ω0) : the angular velocity of the ball which is assumed to be constant during the trajectory
  • α : the angle between ω and v
  • β, γ : are the initial angles of the force applied to the ball
We assume that the forces applied to the ball are
  • its weight
  • a friction force: f, f=12CfπR2ρv2, where Cf0.45
  • a lift force F=12πρR3sin(α) ωv.
  1. Define the differential equation apply to the ball.

  2. Define the initial conditions.

  3. Solve this Cauchy problem with the Runge-Kutta of order 4 algorithm.

  4. Solve this Cauchy problem with the second order Adams-Moulton algorithm.

Runge-Kutta Algorithms

Runge-Kutta of order 2

We want to solve the following Cauchy problem: {y(t)=f(t,y)y(t0)=y0 for t[t0,tn] where f is of class C1. For this, we set k1=h.f(t0,y0)k2=h.f(t0+ah,y0+αk1) and y1=y0+R1k1+R2k2 Consequently, we have to calculate the unknows a,α,R1,R2 such that we minimize the following error: e1=y(t1)y1 To do so, we identify the Taylor expansion of y(t1) and y1. We assume that f is of class C2. As dydt=f(t,y)d2ydt2=f(t,y)t+f(t,y)yf(t,y) it yields, y(t1)=y(t0+h)y(t1)=y0+h.dy(t0)dt+h22d2y(t0)dt2+O(h3)y(t1)=y0+h.f(t0,y0)+h22[f(t0,y0)t+f(t0,y0)yf(t0,y0)]+O(h3) As φ(t,y,h)=R1k1+R2k2 is of class C2, we can apply the Taylor expansion at h=0. Then we have, φ(t,y,h)=φ(t,y,0)+hφ(t,y,0)h+h222φ(t,y,0)h2+O(h3)φ(t,y,h)=0+h[R1.f(t0,y0)]+h.[R2.f(t0,y0)+R2haf(t0,y0)t]+h[R2hαf(t0,y0)yf(t0,y0)]+O(h3) It comes y1=y0+(R1+R2)hf(t0,y0)+h2[aR2f(t0,y0)t+αR2f(t0,y0)yf(t0,y0)]+O(h3). Then, the difference y(t1)y1=(1R1R2)hf(t0,y0)+h2[(12aR2)f(t0,y0)t]+h2[(12αR2)f(t0,y0)yf(t0,y0)]+O(h3) is of order O(h3) if R1+R2=1 and aR2=αR2=12. It follows that:

  • if R2=0 then we obtain the previous Euler method
  • if R1=0,R2=1 and a=α=12 we obtain the modified Euler method where y1=y0+h.f(t0+h2,y0+h2f(t0,y0))
  • if R1=R2=12 and a=α=1 then we obtain the Euler-Cauchy method where y1=y0+12hf(t0,y0)+12h.f(t0+h,y0+hf(t0,y0)).

Runge-Kutta of order 4

The fourth-order Runge–Kutta method is obtained from the Taylor series along the samelines as the second-ordermethod. Since the derivation is rather long and not very instructive, we skip it. The final form of the integration formula again depends on the choice of the parameters; that is, there is no unique Runge–Kutta fourth-order formula. The most popular version, which is known simply as the Runge–Kutta method, entails the following sequence of operations: k1=h.f(t0,y0)k2=h.f(t0+h2,y0+k12)k3=h.f(t0+h2,y0+k22)k4=h.f(t0+h,y0+k3)y1=y0+16(k1+2k2+2k3+k4) The main drawback of this method is that it does not lend itself to an estimate of the truncation error. Therefore, we must guess the integration step size h, or determine it by trial and error. In contrast, the so-called adaptive methods can evaluate the truncation error in each integration step and adjust the value of h accordingly (but at a higher cost of computation). The adaptive method is introduced in the next Section.

Multi-step Algorithms

For all n, we set f(tn,yn)=fn. We call k-step method an algorithm of the kind: {yn=i=1kαiyni+hi=0kβifni for nky0,yk1 are given where the αi and βi are real values which are not dependant of h.

  • If β0=0 then we have an explicit method.
  • If β00 then we have an implicit method.
For example, yn=43yn113yn2+2h3fn is implicit and needs y0 and y1. The value yn is given by using a fixed-point methodology or by a prediction - correction methodology.

Adams-Moulton Algorithm

If we integrate the differential equation we have: y(tn)=y(tn1)+tn1tnf(t,y(t))dt In order to calculte this integral, we substitute f(t,y(t)) by an interpolation polynomial p(t) of degree k which satisfies p(tj)=f(tj,yj)=fj for j=nk,,n. For this, we need to take into account the k values ynk,,yn1 that were calculated previously and yn which is known. If we write this polynomial in the Newton'polynomial basis we obtain: yn=yn1+j=0k(i=0j1(ttni)f[tn,,tnj]). If the abscissa tj are uniform then it yields: yn=yn1+hki=0βifni. Coefficients βi have to be defined such that the formula are exacts for maximum degree polynomials. If k=2, for y(t)=1 then y(t)=0 and y(tn)=1=y(tn1)+h.(f(tn,yn).β0+f(tn1,yn1).β1+f(tn2,yn2).β2)It yields 1=1+h.[0.β0+0.β1+0.β2] for y(t)=ttn then y(t)=1and y(tn)=0=h+h.(β0+β1+β2) for y(t)=(ttn)2 then y(t)=2(ttn)and y(tn)=0=h2+h.(0.β02h.β14h.β2) From β0+β1+β2=12.β14.β2=1 we obtain that β0=β1=12β2=0 Consequently, the Adams-Moulton of order 2 can be written: yn=yn1+h2(fn+fn1) Order 3: yn=yn1+h12(5fn+8fn1fn2)

  • To calculate fn we need to know the value yn then we have implicit methods. To solve then, we can approximate yn by yn by using an explicit method and insert this value in the implicit schema (correction step).
  • Usually the implicit methods are more robust than the explicit ones.

Adams-Bashforth Algorithm
Order 2 : yn=yn1+h2(3fn1fn2) Order 3 : yn=yn1+h12(23fn116fn2+5fn3)

References

  1. Dennis, J.E., and Schnabel, R.B. 1983, Numerical Methods for Unconstrained Optimization and Nonlinear Equations; reprinted 1996 (Philadelphia: S.I.A.M.)