我从二阶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;
答案 0 :(得分:0)
我们的等式是:
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