§1Euler’sMethod欧拉公式的改进:隐式欧拉法/*implicitEulermethod*/向后差商近似导数hxyxyxy)()()(011x0x1))(,()(1101xyxfhyxy+)1,...,0(),(111=+=+++niyxfhyyiiii由于未知数yi+1同时出现在等式的两边,不能直接得到,故称为隐式/*implicit*/欧拉公式,而前者称为显式/*explicit*/欧拉公式。一般先用显式计算一个初值,再迭代求解。隐式欧拉法的局部截断误差:11)(++=iiiyxyR)()(322hOxyih+=即隐式欧拉公式具有1阶精度。Hey!Isn’ttheleadingtermofthelocaltruncationerrorofEuler’smethod?Seemsthatwecanmakeagooduseofit…)(22ihxy§1Euler’sMethod梯形公式/*trapezoidformula*/—显、隐式两种算法的平均)1,...,0()],(),([2111=++=+++niyxfyxfhyyiiiiii注:的确有局部截断误差,即梯形公式具有2阶精度,比欧拉方法有了进步。但注意到该公式是隐式公式,计算时不得不用到迭代法,其迭代收敛性与欧拉公式相似。)()(311hOyxyRiii==++中点欧拉公式/*midpointformula*/中心差商近似导数hxyxyxy2)()()(021x0x2x1))(,(2)()(1102xyxfhxyxy+1,...,1),(211=+=+niyxfhyyiiii假设,则可以导出即中点公式具有2阶精度。)(),(11iiiixyyxyy==)()(311hOyxyRiii==++需要2个初值y0和y1来启动递推过程,这样的算法称为双步法/*double-stepmethod*/,而前面的三种算法都是单步法/*single-stepmethod*/。方法§1Euler’sMethod显式欧拉隐式欧拉梯形公式中点公式简单精度低稳定性最好精度低,计算量大精度提高计算量大精度提高,显式多一个初值,可能影响精度Can’tyougivemeaformulawithalltheadvantagesyetwithoutanyofthedisadvantages?Doyouthinkitpossible?Well,callmegreedy…OK,let’smakeitpossible.改进欧拉法/*modifiedEuler’smethod*/Step1:先用显式欧拉公式作预测,算出),(1iiiiyxfhyy+=+Step2:再将代入隐式梯形公式的右边作校正,得到1+iy)],(),([2111+++++=iiiiiiyxfyxfhyy注:此法亦称为预测-校正法/*predictor-correctormethod*/。可以证明该算法具有2阶精度,同时可以看到它是个单步递推格式,比隐式公式的迭代求解过程简单。后面将看到,它的稳定性高于显式欧拉法。)1,...,0(),(,),(211=+++=++niyxfhyxfyxfhyyiiiiiiii§1Euler’sMethod§2龙格-库塔法/*Runge-KuttaMethod*/建立高精度的单步递推格式。单步递推法的基本思想是从(xi,yi)点出发,以某一斜率沿直线达到(xi+1,yi+1)点。欧拉法及其各种变形所能达到的最高精度为2阶。考察改进的欧拉法,可以将其改写为:),(),(2121121211hKyhxfKyxfKKKhyyiiiiii++==++=+斜率一定取K1K2的平均值吗?步长一定是一个h吗?§2Runge-KuttaMethod首先希望能确定系数1、2、p,使得到的算法格式有2阶精度,即在的前提假设下,使得)(iixyy=)()(311hOyxyRiii==++Step1:将K2在(xi,yi)点作Taylor展开)(),(),(),(),(2112hOyxfphKyxphfyxfphKyphxfKiiyiixiiii+++=++=)()()(2hOxyphxyii++=将改进欧拉法推广为:),(),(][12122111phKyphxfKyxfKKKhyyiiiiii++==++=+),(),(),(),(),(),()(yxfyxfyxfdxdyyxfyxfyxfdxdxyyxyx+=+==Step2:将K2代入第1式,得到)()()()()]()()([)(322212211hOxyphxyhyhOxyphxyxyhyyiiiiiiii++++=++++=+§2Runge-KuttaMethodStep3:将yi+1与y(xi+1)在xi点的泰勒展开作比较)()()()(322211hOxyphxyhyyiiii++++=+)()(2)()()(321hOxyhxyhxyxyiiii+++=+要求,则必须有:)()(311hOyxyRiii==++21,1221==+p这里有个未知数,个方程。32存在无穷多个解。所有满足上式的格式统称为2阶龙格-库塔格式。21,121===p注意到,就是改进的欧拉法。Q:为获得更高的精度,应该如何进一步推广?其中i(i=1,…,m),i(i=2,…,m)和ij(i=2,…,m;j=1,…,i1)均为待定系数,确定这些系数的步骤与前面相似。§2Runge-KuttaMethod)...,(......),(),(),(]...[1122112321313312122122111++++++=+++=++==++++=mmmmmmimiiiiiimmiihKhKhKyhxfKhKhKyhxfKhKyhxfKyxfKKKKhyy最常用为四级4阶经典龙格-库塔法/*ClassicalRunge-KuttaMethod*/:),(),(),(),()22(34222312221432161hKyhxfKKyxfKKyxfKyxfKKKKKyyiihihihihiiihii++=++=++==++++=+§2Runge-KuttaMethod注:龙格-库塔法的主要运算在于计算Ki的值,即计算f的值。Butcher于1965年给出了计算量与可达到的最高精度阶数的关系:753可达到的最高精度642每步须算Ki的个数)(2hO)(3hO)(4hO)(5hO)(6hO)(4hO)(2nhO8n由于龙格-库塔法的导出基于泰勒展开,故精度主要受解函数的光滑性影响。对于光滑性不太好的解,最好采用低阶算法而将步长h取小。HW:p.202#1,2§2Runge-KuttaMethodLab15.Runge-Kutta(OrderFour)UseRunge-Kuttamethodoforderfourtoapproximatethesolutionoftheinitial-valueproblem,,and(1)YouaresupposedtowriteafunctionvoidRK4(double(*f)(),doublea,doubleb,doubley0,intn,FILE*outfile)toapproximatethesolutionofProblem(1)withy’=f,xin[a,b],andtheinitialvalueofybeingy0.Outputtheapproximatingvaluesofyonthen+1equallyspacedgridpointsfromatobtooutfile.InputThereisnoinputfile.Instead,youmusthandinyourfunctionina*.hfile.Theruleofnamingthe*.hfileisthesameasthatofnamingthe*.cor*.cppfiles.Output(representsaspace)Foreachtestcase,youaresupposedtoprintn+1lines,andeachlineisinthefollowingformat:fprintf(outfile,“%8.4f%12.8e\n”,x,y);),(yxfy=],[bax0)(yay=§2Runge-KuttaMethodSampleJudgeProgram#includestdio.h#includemath.h#include98115001_15.hdoublef0(doublex,doubley){return(yx*x+1.0);}voidmain(){FILE*outfile=fopen(out.txt,w);intn;doublea,b,y0;a=0.0;b=2.0;y0=0.50;n=10;RK4(f0,a,b,y0,n,outfile);fprintf(outfile,\n);fclose(outfile);}SampleOutput(representsaspace)0.00005.00000000e0010.20008.29293333e0010.40001.21407621e+0000.60001.64892202e+0000.80002.12720268e+0001.00002.64082269e+0001.20003.17989417e+0001.40003.73234007e+0001.60004.28340950e+0001.80004.81508569e+0002.00005.30536300e+000§3收敛性与稳定性/*ConvergencyandStability*/收敛性/*Convergency*/定义若某算法对于任意固定的x=xi=x0+ih,当h0(同时i)时有yiy(xi),则称该算法是收敛的。例:就初值问题考察欧拉显式格式的收敛性。==0)0(yyyy解:该问题的精确解为xeyxy0)(=欧拉公式为iiiiyhyhyy)1(1+=+=+0)1(yhyii+=对任意固定的x=xi=ih,有iixhhxihyhyy])1[()1(/10/0+=+=ehhh=+/10)1(lim)(0ixxyeyi=§3ConvergencyandStability稳定性/*Stability*/例:考察初值问题在区间[0,0.5]上的解。分别用欧拉显、隐式格式和改进的欧拉格式计算数值解。==1)0()(30)(yxyxy0.00.10.20.30.40.5精确解改进欧拉法欧拉隐式欧拉显式节点xixey30=1.00002.00004.00008.00001.60001013.20001011.00002.50001016.25001021.56251023.90631039.76561041.00002.50006.25001.56261013.90631019.76561011.00004.97871022.47881031.23411046.14421063.0590107Whatiswrong??!AnEngineercomplains:Maththeoremsaresounstablethatasmallperturbationontheconditionswillcauseacrashontheconclusions!§3ConvergencyandStability定义若某算法在计算过程中任一步产生的误差在以后的计算中都逐步衰减,则称该算法是绝对稳定的/*absolutelystable*/。一般分析时为简