Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何使用RK4算法求解ODE?_Algorithm_Matlab_Math_Numerical Methods_Differential Equations - Fatal编程技术网

Algorithm 如何使用RK4算法求解ODE?

Algorithm 如何使用RK4算法求解ODE?,algorithm,matlab,math,numerical-methods,differential-equations,Algorithm,Matlab,Math,Numerical Methods,Differential Equations,我使用的是RK4算法: function R=RK4_h(f,a,b,ya,h) % Input % - f field of the edo y'=f(t,y). A string of characters 'f' % - a and b initial and final time % - ya initial value y0 % - h lenght of the step % Output % - R=[T' Y'] where T independent

我使用的是RK4算法:

function R=RK4_h(f,a,b,ya,h)

%  Input
%   - f field of the edo y'=f(t,y). A string of characters 'f'
%   - a and b initial and final time
%   - ya initial value y0
%   - h lenght of the step

%  Output
%   - R=[T' Y'] where T independent variable and Y dependent variable


N = fix((b-a) / h);
T = zeros(1,N+1);
Y = zeros(1,N+1);

%   Vector of the time values 
T = a:h:b;

%   Solving ordinary differential equation
Y(1) = ya;
for j = 1:N
    k1 = h*feval(f,T(j),Y(j));
    k2 = h*feval(f,T(j)+h/2,Y(j)+k1/2);
    k3 = h*feval(f,T(j)+h/2,Y(j)+k2/2);
    k4 = h*feval(f,T(j)+h,Y(j)+k3);
    Y(j+1) = Y(j) + (k1+2*k2+2*k3+k4)/6;
end
R=[T' Y'];
在我的主脚本中,我为每个值调用它,如下所示:

xlabel('x')
ylabel('y')

h=0.05;
fprintf ('\n First block \n');
xx = [0:h:1];
Nodes = length(xx);
yy = zeros(1,Nodes);
for i=1:Nodes
  fp(i)=feval('edo',-1,xx(i));
end
E=RK4_h('edo',0,1,-1,h);
plot(E);
fprintf ('\n%f',E);
问题是,当我尝试将RK4算法与edo公式结合使用时:

结果不符合逻辑,例如,的实际值为:y0=8,y1=11,53。但估计数字并不接近。E向量中的任意两个坐标都表示该问题的可行方法,因此我不知道这是否是正确的实现

执行时有一个基本错误?

函数edo将t作为第一个参数,y作为第二个参数。你有反向的参数

你的职能应该是:

 function edo = edo(t,y)   % NOT edo(y,t)
   edo = 6*((exp(1))^(6*t))*(y-(2*t))^2+2;
 function edo = edo(t,y)   % NOT edo(y,t)
   edo = 6*((exp(1))^(6*t))*(y-(2*t))^2+2;