我有两个从二阶 ODE 得到的一阶 ODE:
y(0)=1
y'(0)=-1/3
u1'=u2
u2=u/9-(pi*u1*e^(x/3)*(2u2*sin(pi*x)+pi*u1cos(pi*x))
u1(0)=y(0)=1
u2(0)=y'(0)=-1/3
我的问题是如何设置正向欧拉?我有这个:
n=[0:0.01:2];
h=2./n;
我有两个从二阶 ODE 得到的一阶 ODE:
y(0)=1
y'(0)=-1/3
u1'=u2
u2=u/9-(pi*u1*e^(x/3)*(2u2*sin(pi*x)+pi*u1cos(pi*x))
u1(0)=y(0)=1
u2(0)=y'(0)=-1/3
我的问题是如何设置正向欧拉?我有这个:
n=[0:0.01:2];
h=2./n;
我们的方程式是:
u1' = u2
u2' = u1/9 - \pi u1 exp(x/3)(2u2 sin(\pi x) + \pi u1 cos(\pi x))
现在求解 y' = f(x,y) 的欧拉方法是:
y_{n+1} = y_{n} + h * f(x_n, y_n)
作为 MATLAB 代码,我们可以这样写:
h = 0.01; % Choose a step size
x = [0:h:2]; % Set up x
u = zeros(length(x),2);
u(1,:) = [1; -1/3]; % Initial Conditions for y
for ii = 2:length(x)
u(ii,:) = u(ii-1,:) + h * CalculateDeriv(x(ii-1),u(ii-1,:)); % Update u at each step
end
function deriv = CalculateDerivative(x,u)
deriv = zeros(2,1);
deriv(1) = u(2);
deriv(2) = u(1)/9 - pi*u(1)*exp(x/9)*(2*u(2)*sin(pi*x) + pi*u(1)*cos(pi*x))
end