資源簡介
MATLAB四階龍格庫塔法 求解微分方程數值解
部分源碼
clear;clc;close all
h=0.2;
t=0:h:3;
x(1)=1;
%使用Runge-Kutta方法,計算微分方程的數值解

代碼片段和文件信息
clear;clc;close?all
h=0.2;?
t=0:h:3;
x(1)=1;
%使用Runge-Kutta方法,計算微分方程的數值解
for?n=1:(length(t)-1)
????K1=-t(n)*x(n);
????K2=-(t(n)+h/2)*(x(n)+K1*h/2);
????K3=-(t(n)+h/2)*(x(n)+K2*h/2);
????K4=-(t(n)+h)*(x(n)+K3*h);
????x(n+1)=x(n)+(h/6)*(K1+2*K2+2*K3+K4);
end
plot(tx‘r‘)?
hold?on
xt=dsolve(‘Dx=-t*x‘‘x(0)=1‘);???%求出微分方程的精確符號解
ezplot(xt[0?3])?
xlabel(‘t‘)?
ylabel(‘x‘)?
title([‘h=‘num2str(h)])?
legend({‘4th?Runge-Kutta‘‘symbolic?solution‘})
%根據精確符號解,求出在時間t上,x的精確值
n=1;
for?t=0:h:3
????xt_value(n)=eval(xt);
????n=n+1;
end
error=abs(x-xt_value);???%Runge-Kutta方法得到的x,與精確的x之間的誤差值
figure
t=0:h:3;??
plot(terror)???%繪制出?時間t-誤差error?圖
xlabel(‘t‘)?
ylabel(‘error‘)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????776??2018-04-21?19:20??MATLAB四階龍格庫塔法?求解微分方程數值解?源程序代碼\fourth_order_Runge_Kutta.m
?????目錄???????????0??2014-06-07?12:45??MATLAB四階龍格庫塔法?求解微分方程數值解?源程序代碼\
評論
共有 條評論