3、/2)*K2);K4=f(Xn+h,Yn+h*K3);Yn+1=Yn+h*(K1+2K2+2K3+K4)*(1/6)一、Runge-Kutta的算法和流程圖在龍格-庫塔法中,四階龍格-庫塔法的局部截斷誤差約為0(h5),被廣泛應(yīng)用于解微分方程的初值問題。其算法公式為:流程圖:(1)、四階龍格-庫塔方法流程圖:(2)、實例求解流程圖:一、Runge-Kutta的Matlab實現(xiàn)function[x,y]=runge_kutta1(ufunc,y0,h,a,b)%參數(shù)表順序依次是微分方程組的函數(shù)名稱,初始值向量,步長,時間起點,時間終點(參數(shù)形式參考了ode45函數(shù))n=floo
4、r((b-a)/h);%求步數(shù)x(1)=a;%時間起點y(:,1)=y0;%賦初值,可以是向量,但是要注意維數(shù)forii=1:nx(ii+1)=x(ii)+h;k1=ufunc(x(ii),y(:,ii));k2=ufunc(x(ii)+h/2,y(:,ii)+h*k1/2);k3=ufunc(x(ii)+h/2,y(:,ii)+h*k2/2);k4=ufunc(x(ii)+h,y(:,ii)+h*k3);y(:,ii+1)=y(:,ii)+h*(k1+2*k2+2*k3+k4)/6;%按照龍格庫塔方法進(jìn)行數(shù)值求解end一、Runge-Kutta的算例實現(xiàn)例:求解常微分方程d
5、(y)/d(x)=-2*y+2*x*x+2*x,0≤x≤0.5,y(0)=1.編輯m文件Untitled3:fun=inline('-2*y+2*x*x+2*x');[x,y]=ode45(fun,[0,0.5],1)>>Untitled3x=00.01250.02500.03750.05000.06250.07500.08750.10000.11250.12500.13750.15000.16250.17500.18750.20000.21250.22500.23750.25000.26250.27500.28750.30000.31250.32500.33750.3500
6、0.36250.37500.38750.40000.41250.42500.43750.45000.46250.47500.48750.5000y=1.00000.97550.95190.92910.90730.88640.86630.84710.82870.81120.79440.77850.76330.74890.73530.72240.71030.69890.68830.67830.66900.66050.65260.64540.63880.63290.62770.62310.61910.61570.61300.61090.60930.60840.60800.60830
7、.60910.61040.61240.61480.6179