我正在尝试在 MATLAB中实现用于 DE 系统的 Runge-Kutta 方法。我没有得到正确的答案,我不确定代码或我用来运行它的命令是否有问题。
这是我的代码:
function RKSystems(a, b, m, N, alpha, f)
h = (b - a)/N;
t = a;
w = zeros(1, m);
for j = 1:m
w(j) = alpha(j);
end
fprintf('t = %.2f;', t);
for i = 1:m
fprintf(' w(%d) = %.10f;', i, w(i));
end
fprintf('\n');
k = zeros(4, m);
for i = 1:N
for j = 1:m
k(1, j) = h*f{j}(t, w);
end
for j = 1:m
k(2, j) = h*f{j}(t + h/2, w + (1/2)*k(1));
end
for j = 1:m
k(3, j) = h*f{j}(t + h/2, w + (1/2)*k(2));
end
for j = 1:m
k(4, j) = h*f{j}(t + h, w + k(3));
end
for j = 1:m
w(j) = w(j) + (k(1, j) + 2*k(2, j) + 2*k(3, j) + k(4, j))/6;
end
t = a + i*h;
fprintf('t = %.2f;', t);
for k = 1:m
fprintf(' w(%d) = %.10f;', k, w(k));
end
fprintf('\n');
end
end
我正在尝试在这个问题上进行测试。这是我的命令和输出:
>> U1 = @(t, u) 3*u(1) + 2*u(2) - (2*t^2 + 1)*exp(2*t);
>> U2 = @(t, u) 4*u(1) + u(2) + (t^2 + 2*t - 4)*exp(2*t);
>> a = 0; b = 1; 阿尔法 = [1 1]; 米 = 2; h = 0.2; N = (b - a)/h;
>> RKSystems(a, b, m, N, alpha, {U1 U2});
t = 0.00;w(1) = 1.0000000000; w(2) = 1.0000000000;
t = 0.20;w(1) = 2.2930309680;w(2) = 1.6186020410;
t = 0.40;w(1) = 5.0379629523;w(2) = 3.7300162165;
t = 0.60;w(1) = 11.4076339762;w(2) = 9.7009491301;
t = 0.80;w(1) = 27.0898576892;w(2) = 25.6603894354;
t = 1.00;w(1) = 67.1832886708;w(2) = 67.6103125539;