5. Ordinary Differential Equation

求解問題

Write a program to solve the following ordinary differential equation by

  • basic Euler method
  • improved Euler method
  • four-order Runge-Kutta method

and calculate y(1.5) with stepsize=0.1, 0.1/2, 0.1/4, 0.1/8

Compare it with analytic solution (in figure)

主程序

program main
    use ODE
    implicit none
    real :: x0=0.0,y0=3.0,a=0.0,b=1.5,h=0.1
    integer :: n,i,j
    character(len=512) :: filename


    print *,'The analytic solution of y(1.5)= ',analytic_solution(1.5)
    print *

    print *,'Basic Euler Method'
    print '(5x,a,f3.1,3x,a,f3.1,3x,a,f3.1,3x,a,f3.1)','Set initial  x0= ',x0,'y0= ',y0,'a= ',a,'b= ',b
    do j=0,3
        h=0.1/2**j
        print '(5x,a,f6.4)','Set h= ',h
        call init(x0,y0,a,b,h)
        n=size(x)-1
        call Basic_Euler
        print '(8x,a,f10.8)','y(1.5)= ',y(n)
        write(filename, *) j
        filename='BEM_'//Trim(AdjustL(filename))//'.txt'
        open(101,file=filename)
        write(101,'(f3.1,f10.8)') (x(i),y(i),i=0,n)
        close(101)
        deallocate(x,y)
    end do
    print *

    print *,'Improved Euler Method'
    print '(5x,a,f3.1,3x,a,f3.1,3x,a,f3.1,3x,a,f3.1)','Set initial  x0= ',x0,'y0= ',y0,'a= ',a,'b= ',b
    do j=0,3
        h=0.1/2**j
        print '(5x,a,f6.4)','Set h= ',h
        call init(x0,y0,a,b,h)
        n=size(x)-1
        call Improved_Euler
        print '(8x,a,f10.8)','y(1.5)= ',y(n)
        write(filename, *) j
        filename='IEM_'//Trim(AdjustL(filename))//'.txt'
        open(101,file=filename)
        write(101,'(f3.1,f10.8)') (x(i),y(i),i=0,n)
        close(101)
        deallocate(x,y)
    end do
    print *

    print *,'Four Order Runge-Kutta Method'
    print '(5x,a,f3.1,3x,a,f3.1,3x,a,f3.1,3x,a,f3.1)','Set initial  x0= ',x0,'y0= ',y0,'a= ',a,'b= ',b
    do j=0,3
        h=0.1/2**j
        print '(5x,a,f6.4)','Set h= ',h
        call init(x0,y0,a,b,h)
        n=size(x)-1
        call Four_Order_Runge_Kutta
        print '(8x,a,f10.8)','y(1.5)= ',y(n)
        write(filename, *) j
        filename='RKM_'//Trim(AdjustL(filename))//'.txt'
        open(101,file=filename)
        write(101,'(f3.1,f10.8)') (x(i),y(i),i=0,n)
        close(101)
        deallocate(x,y)
    end do
    print *

end program main

求解ODE方程的關鍵方法寫在ODE模塊中:

module ODE
    implicit none
    private
    public :: x,y,init,Basic_Euler,Improved_Euler,Four_Order_Runge_Kutta,analytic_solution

    real,allocatable :: x(:),y(:)
    real :: x0,y0,a,b,h
    integer :: n,i

contains

    function f(x,y)
        implicit none
        real :: f,x,y
        f=-x*x*y*y
    end function f

    function analytic_solution(x) result(f)
        implicit none
        real :: f,x
        f=3.0/(1+x*x*x)
    end function

    subroutine init(x0_,y0_,a_,b_,h_)
        implicit none
        real :: x0_,y0_,a_,b_,h_
        x0=x0_
        y0=y0_
        a=a_
        b=b_
        h=h_
        n=int((b-a)/h)
        allocate(x(0:n),y(0:n))
        x=(/ (a+i*h,i=0,n) /)
        y=0
        y(0)=y0
    end subroutine init

    subroutine Basic_Euler()
        implicit none
        do i=1,n
            y(i)=y(i-1)+h*f(x(i-1),y(i-1))
        end do
    end subroutine Basic_Euler

    subroutine Improved_Euler()
        implicit none
        real :: y_
        do i=1,n
            y_=y(i-1)+h*f(x(i-1),y(i-1))
            y(i)=y(i-1)+h/2*(f(x(i-1),y(i-1))+f(x(i),y_))
        end do
    end subroutine Improved_Euler

    subroutine Four_Order_Runge_Kutta()
        implicit none
        real :: k1,k2,k3,k4
        do i=1,n
            k1=f(x(i-1),y(i-1))
            k2=f(x(i-1)+h/2,y(i-1)+h/2*k1)
            k3=f(x(i-1)+h/2,y(i-1)+h/2*k2)
            k4=f(x(i-1)+h,y(i-1)+h*k3)
            y(i)=y(i-1)+h/6*(k1+2*k2+2*k3+k4)
        end do
    end subroutine Four_Order_Runge_Kutta
end module ODE

其中init方法是用來初始化:

    subroutine init(x0_,y0_,a_,b_,h_)
        implicit none
        real :: x0_,y0_,a_,b_,h_
        x0=x0_
        y0=y0_
        a=a_
        b=b_
        h=h_
        n=int((b-a)/h)
        allocate(x(0:n),y(0:n))
        x=(/ (a+i*h,i=0,n) /)
        y=0
        y(0)=y0
    end subroutine init

analytic_solution是數值解:

    function analytic_solution(x) result(f)
        implicit none
        real :: f,x
        f=3.0/(1+x*x*x)
    end function

Basic Euler Method

流程圖:


原理:
![][3]
[3]: http://latex.codecogs.com/gif.latex?\begin{cases}%20y(x_{n+1})=y(x_n)+hf(x_n,y(x_n))+O(h^2)%20\%20y(x_0)=y_0%20\end{cases}

代碼:

    subroutine Basic_Euler()
        implicit none
        do i=1,n
            y(i)=y(i-1)+h*f(x(i-1),y(i-1))
        end do
    end subroutine Basic_Euler

輸出結果:


Basic Euler 方法

圖例:


不同h每步迭代結果

Improved Euler Method

流程圖:


原理:
![][4]
[4]: http://latex.codecogs.com/gif.latex?\begin{cases}%20\bar%20y_{n+1}=y_n+hf(x_n,y_n)%20\%20y(x_{n+1})=y(x_n)+\dfrac{h}{2}[f(x_n,y(x_n))+f(x_{n+1},\bar%20y_{n+1})]+O(h^2)%20\%20y(x_0)=y_0%20\end{cases}

代碼:

    subroutine Improved_Euler()
        implicit none
        real :: y_
        do i=1,n
            y_=y(i-1)+h*f(x(i-1),y(i-1))
            y(i)=y(i-1)+h/2*(f(x(i-1),y(i-1))+f(x(i),y_))
        end do
    end subroutine Improved_Euler

輸出結果:


Improved Euler 方法

圖例:


不同h每步迭代結果

Four Order Runge-Kutta Method

流程圖:


原理:
![][5]
[5]: http://latex.codecogs.com/gif.latex?\begin{cases}%20y_{n+1}=y_n+\dfrac{h}{6}(K_1+2K_2+2K_3+K_4)%20\%20K_1=f(x_n,y_n)%20\%20K_2=f(x_n+\dfrac{h}{2},y_n+\dfrac{h}{2}K_1)%20\%20K_3=f(x_n+\dfrac{h}{2},y_n+\dfrac{h}{2}K_2)%20\%20K_4=f(x_n+h,y_n+hK_3)%20\end{cases}

代碼:

    subroutine Four_Order_Runge_Kutta()
        implicit none
        real :: k1,k2,k3,k4
        do i=1,n
            k1=f(x(i-1),y(i-1))
            k2=f(x(i-1)+h/2,y(i-1)+h/2*k1)
            k3=f(x(i-1)+h/2,y(i-1)+h/2*k2)
            k4=f(x(i-1)+h,y(i-1)+h*k3)
            y(i)=y(i-1)+h/6*(k1+2*k2+2*k3+k4)
        end do
    end subroutine Four_Order_Runge_Kutta

輸出結果:


4 Order Runge-Kutta 方法

圖例:


不同h每步迭代結果

三種方法結果對比

h=0.1時
h=0.1/8時

可以看出,當h較大時,三種方法的差別還是很大的,當h逐漸減小時,三種方法的結果已基本相同。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,778評論 0 33
  • 轉至元數據結尾創建: 董瀟偉,最新修改于: 十二月 23, 2016 轉至元數據起始第一章:isa和Class一....
    40c0490e5268閱讀 1,789評論 0 9
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,992評論 19 139
  • 午飯過后,D小姐照常跟著同事們在辦公桌上聊會兒天,那是一天之中她感覺最輕松的時刻。可以聽著這些長輩們怎么說世...
    美的歲月閱讀 231評論 0 0
  • 定義 定義模型與表之間的映射,使用 define 方法. Sequelize 會自動增加 createdAt 和 ...
    kayorl閱讀 7,986評論 2 4