赞
踩
目录
在常微分方程算法之欧拉法中我们提到,通过数值计算求解连续问题的基本思想是采用离散化方法将连续问题转化为离散问题,进而采用迭代的方式求解差分格式的离散方程。在从离散方程到差分方程的过程中,我们采用了将高阶项忽略的近似方法,称为局部截断误差。而对于一阶常微分方程来说,如果其精确解在求解域I上存在k+1阶连续导数,则利用泰勒级数展开可得:
如果忽略式(1)中的高阶项,可得:
如果令式(2)中k=1,即可获得欧拉法,所以本质上欧拉法是一阶泰勒级数方法的数值计算,称之为一阶方法。如果令k=2,并忽略高阶项,可得二阶泰勒级数数值计算公式:
由梯形法和改进欧拉法可知,两者均属于二阶方法。故阶数越高,计算精度越高,也即可以通过控制局部截断误差来提升算法精度。
对改进欧拉法差分格式进行推导变形:
由此可见,改进欧拉法本质上与泰勒级数二阶展开(公式3)只差一项。所以,泰勒级数展开中各阶导数可以用函数在一些点上值的线性组合近似地表示。对于欧拉法,,其中,局部截断误差为;对于改进欧拉法,,其中,,局部截断误差为。将二阶泰勒级数数值格式写成一般格式:
对进行二元函数泰勒展开:
则公式(4)可以写作:
将公式(5)与公式(3)比较可知,只要满足,两式则对等:
所以将满足上述参数条件的一族公式(4)称为二阶龙格-库塔公式。当参数满足时,二阶龙格-库塔法就是改进欧拉法。
同二阶推导类似,三阶泰勒级数数值格式写成一般格式:
对于上式,只要满足:
所以将满足上述参数条件的一族公式称为三阶龙格-库塔公式,可以取一组简单的解,得到一组简单的三阶龙格-库塔公式。
数值计算中通常采用四阶龙格-库塔公式,推导过程与前述类似,四阶泰勒级数数值格式写成一般格式:
对于上式,只要满足:
可以取下列满足条件的一组解,得到最常用的四阶龙格-库塔公式:
与欧拉法、梯形法、改进欧拉法一样,求相同的初值问题
步长h=0.02。已知精确解为:
代码如下:
- #include <stdlib.h>
- #include <stdio.h>
- #include <cmath>
-
- int main(int argc, char* argv[])
- {
- int i,N;
- double a,b,h,x0,x1,y0,y1,yexact,err;
- double k1,k2,k3,k4;
- double f(double x, double y);
- double exact(double x);
-
- a=0.0;
- b=0.1;
- N=5;
- h=(b-a)/N;
-
- i=1;
- x0=a;
- y0=1.0;
-
- do
- {
- x1=x0+h;
- k1=h*f(x0,y0);
- k2=h*f(x0+0.5*h,y0+0.5*k1);
- k3=h*f(x0+0.5*h,y0+0.5*k2);
- k4=h*f(x1,y0+k3);
- y1=y0+(k1+2*k2+2*k3+k4)/6.0;
- yexact=exact(x1);
- err=fabs(y1-yexact);
- printf("x=%.2f, y=%f, exact=%f, err=%.10f.\n",x1,y1,yexact,err);
- i++;
- x0=x1;
- y0=y1;
- }while(i<=N);
-
- return 0;
- }
-
- double f(double x, double y)
- {
- return -0.9*y/(1.0+2*x);
- }
-
- double exact(double x)
- {
- return pow((1.0+2*x),-0.45);
N=5时,运行结果如下(并与欧拉法、梯形法、改进欧拉法对比):
- ++++++++++++++++++++++四阶龙格-库塔法+++++++++++++++++++++++
- x=0.0200, y=0.982506, exact=0.982506, err=0.0000000003.
- x=0.0400, y=0.965960, exact=0.965960, err=0.0000000006.
- x=0.0600, y=0.950281, exact=0.950281, err=0.0000000008.
- x=0.0800, y=0.935393, exact=0.935393, err=0.0000000010.
- x=0.1000, y=0.921231, exact=0.921231, err=0.0000000011.
- +++++++++++++++++++++++++预估-校正法++++++++++++++++++++++++++++
- x[1]=0.0200, y[1]=0.982324, exact=0.982506, err=0.000182.
- x[2]=0.0400, y[2]=0.965616, exact=0.965960, err=0.000344.
- x[3]=0.0600, y[3]=0.949791, exact=0.950281, err=0.000490.
- x[4]=0.0800, y[4]=0.934772, exact=0.935393, err=0.000621.
- x[5]=0.1000, y[5]=0.920492, exact=0.921231, err=0.000739.
- +++++++++++++++++++++++++++梯形法++++++++++++++++++++++++++++++
- x[1]=0.0200, y[1]=0.982498, exact=0.982506, err=0.000008.
- x[2]=0.0400, y[2]=0.965946, exact=0.965960, err=0.000015.
- x[3]=0.0600, y[3]=0.950260, exact=0.950281, err=0.000021.
- x[4]=0.0800, y[4]=0.935367, exact=0.935393, err=0.000026.
- x[5]=0.1000, y[5]=0.921201, exact=0.921231, err=0.000030.
- ++++++++++++++++++++++++++++欧拉法+++++++++++++++++++++++++++++
- x[1]=0.0200, y[1]=0.982000, exact=0.982506, err=0.000506.
- x[2]=0.0400, y[2]=0.965004, exact=0.965960, err=0.000957.
- x[3]=0.0600, y[3]=0.948920, exact=0.950281, err=0.001360.
- x[4]=0.0800, y[4]=0.933670, exact=0.935393, err=0.001723.
- x[5]=0.1000, y[5]=0.919182, exact=0.921231, err=0.002049.
N=10时,运行结果如下(并与欧拉法、梯形法、改进欧拉法对比):
- ++++++++++++++++++++++++四阶龙格-库塔法+++++++++++++++++++++++
- x=0.0100, y=0.991128, exact=0.991128, err=0.0000000000.
- x=0.0200, y=0.982506, exact=0.982506, err=0.0000000000.
- x=0.0300, y=0.974120, exact=0.974120, err=0.0000000000.
- x=0.0400, y=0.965960, exact=0.965960, err=0.0000000000.
- x=0.0500, y=0.958017, exact=0.958017, err=0.0000000000.
- x=0.0600, y=0.950281, exact=0.950281, err=0.0000000001.
- x=0.0700, y=0.942742, exact=0.942742, err=0.0000000001.
- x=0.0800, y=0.935393, exact=0.935393, err=0.0000000001.
- x=0.0900, y=0.928225, exact=0.928225, err=0.0000000001.
- x=0.1000, y=0.921231, exact=0.921231, err=0.0000000001.
- +++++++++++++++++++++++++预估-校正法+++++++++++++++++++++++++++
- x[1]=0.0100, y[1]=0.991081, exact=0.991128, err=0.000047.
- x[2]=0.0200, y[2]=0.982413, exact=0.982506, err=0.000092.
- x[3]=0.0300, y[3]=0.973985, exact=0.974120, err=0.000135.
- x[4]=0.0400, y[4]=0.965786, exact=0.965960, err=0.000175.
- x[5]=0.0500, y[5]=0.957805, exact=0.958017, err=0.000213.
- x[6]=0.0600, y[6]=0.950032, exact=0.950281, err=0.000248.
- x[7]=0.0700, y[7]=0.942459, exact=0.942742, err=0.000283.
- x[8]=0.0800, y[8]=0.935078, exact=0.935393, err=0.000315.
- x[9]=0.0900, y[9]=0.927879, exact=0.928225, err=0.000346.
- x[10]=0.1000, y[10]=0.920856, exact=0.921231, err=0.000375.
- ++++++++++++++++++++++++++++梯形法+++++++++++++++++++++++++++++
- x[1]=0.0100, y[1]=0.991127, exact=0.991128, err=0.000001.
- x[2]=0.0200, y[2]=0.982504, exact=0.982506, err=0.000002.
- x[3]=0.0300, y[3]=0.974117, exact=0.974120, err=0.000003.
- x[4]=0.0400, y[4]=0.965957, exact=0.965960, err=0.000004.
- x[5]=0.0500, y[5]=0.958013, exact=0.958017, err=0.000004.
- x[6]=0.0600, y[6]=0.950276, exact=0.950281, err=0.000005.
- x[7]=0.0700, y[7]=0.942736, exact=0.942742, err=0.000006.
- x[8]=0.0800, y[8]=0.935386, exact=0.935393, err=0.000006.
- x[9]=0.0900, y[9]=0.928218, exact=0.928225, err=0.000007.
- x[10]=0.1000, y[10]=0.921223, exact=0.921231, err=0.000008.
- ++++++++++++++++++++++++++++欧拉法+++++++++++++++++++++++++++++
- x[1]=0.0100, y[1]=0.991000, exact=0.991128, err=0.000128.
- x[2]=0.0200, y[2]=0.982256, exact=0.982506, err=0.000250.
- x[3]=0.0300, y[3]=0.973756, exact=0.974120, err=0.000364.
- x[4]=0.0400, y[4]=0.965488, exact=0.965960, err=0.000473.
- x[5]=0.0500, y[5]=0.957442, exact=0.958017, err=0.000575.
- x[6]=0.0600, y[6]=0.949609, exact=0.950281, err=0.000672.
- x[7]=0.0700, y[7]=0.941978, exact=0.942742, err=0.000764.
- x[8]=0.0800, y[8]=0.934541, exact=0.935393, err=0.000851.
- x[9]=0.0900, y[9]=0.927290, exact=0.928225, err=0.000934.
- x[10]=0.1000, y[10]=0.920218, exact=0.921231, err=0.001013.
可见,相同计算条件下,四阶龙格-库塔法具有更高的计算精度,近乎与解析解相等。但采用四阶龙格-库塔法在每一步计算中都需要计算4次
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。