【发布时间】:2017-04-14 09:18:40
【问题描述】:
我想在 Matlab 中使用 Runge Kutta 4 方法求解三个微分方程组(不允许使用 Ode45)。
经过长时间的查找,我在网上找到的要么是难以理解的示例,要么是根本不包含示例的一般解释。我想要一个关于如何正确实施我的解决方案的具体示例,或者我可以构建的类似问题的解决方案。
我已经走了很远; 我当前的代码在大多数组件上输出了一个包含 2 个正确小数的矩阵,我对此非常满意。
但是,当步长减小时,误差会变得很大。我知道我创建的 for 循环并不完全正确。我可能错误地定义了函数,但我很确定如果对 for 循环进行一些小的更改,问题就解决了,因为它似乎已经很好地解决了当前状态下的方程组。
clear all, close all, clc
%{
____________________TASK:______________________
Solve the system of differential equations below
in the interval 0<t<1, with stepsize h = 0.1.
x'= y x(0)=1
y'= -x-2e^t+1 y(0)=0 , where x=x(t), y=y(t), z=z(t)
z'= -x - e^t + 1 z(0)=1
THE EXACT SOLUTIONS for x y and z can be found in this pdf:
archives.math.utk.edu/ICTCM/VOL16/C029/paper.pdf
_______________________________________________
%}
h = 0.1;
t = 0:h:1
N = length(t);
%Defining the functions
x = zeros(N,1);%I am not entierly sure if x y z are supposed to be defined in this way.
y = zeros(N,1)
z = zeros(N,1)
f = @(t, x, y, z) -x-2*exp(t)+1;%Question: Do i need a function for x here as well??
g = @(t, x, y, z) -x - exp(t) + 1;
%Starting conditions
x(1) = 1;
y(1) = 0;
z(1) = 1;
for i = 1:(N-1)
K1 = h * ( y(i));%____I think z(i) is supposed to be here, but i dont know in what way.
L1 = h * f( t(i) , x(i) , y(i) , z(i));
M1 = h * g( t(i) , x(i) , y(i) , z(i));
K2 = h * (y(i) + 1/2*L1 + 1/2*M1);%____Again, z(i) should probably be here somewhere.
L2 = h * f(t(i) + 1/2*h, x(i)+1/2*K1 , y(i)+1/2*L1 , z(i)+1/2*M1);
M2 = h * g(t(i) + 1/2*h, x(i)+1/2*K1 , y(i)+1/2*L1 , z(i)+1/2*M1);
K3 = h * (y(i) + 1/2*L2 + 1/2*M2);%____z(i). Should it just be added, like "+z(i)" ?
L3 = h * f(t(i) + 1/2*h, x(i) + 1/2*K2 , y(i) + 1/2*L2 , z(i) + 1/2*M2);
M3 = h * g(t(i) + 1/2*h, x(i) + 1/2*K2 , y(i) + 1/2*L2 , z(i) + 1/2*M2);
K4 = h * (y(i) + L3 + M3);%_____z(i) ... ?
L4 = h * f( t(i)+h , x(i)+K3 , y(i)+L3, z(i)+M3);
M4 = h * g( t(i)+h , x(i)+K3 , y(i)+L3, z(i)+M3);
x(i+1) = x(i)+1/6*(K1+2*K2+2*K3+K4);
y(i+1) = y(i)+1/6*(L1+2*L2+2*L3+L4);
z(i+1) = z(i)+1/6*(M1+2*M2+2*M3+M4);
end
Answer_Matrix = [t' x y z]
【问题讨论】:
-
您的函数
f和g必须将t,x,y,z传递给它们,但实际上只是x和t的函数......这些函数正确吗?跨度> -
1/2*M1、1/2*M2和M3不正确。它们不应该被包括在内,因为只有y被加入到x的方程式中。 -
嗯,问题真的是我觉得网络上严重缺乏关于如何使用 Rk4 求解具有三个方程的微分方程的具体示例。再次;我只设法找到了非常糟糕的具体例子和一般描述。
-
没问题。事实上,我责怪文本给出了如何编写 RK 方法的如此糟糕的示例。单独处理方程而不是像向量一样处理会使代码非常难看、复杂且容易出错。但我很高兴你明白了。
-
嗯,我不同意它使代码复杂。我发现这比向量函数更直观。您能否推荐一个好的页面/pdf 来解决向量的类似问题(包含具体示例)?
标签: matlab runge-kutta