dy/dt=-xy;dx/dt=-xy-y
y0=1;x0=1
% 定义微分方程组
ode = @(t,y) [-y(1)*y(2); -y(1)*y(2)-y(2)];
% 定义初值条件
tspan = [0 10]; % 时间区间
y0 = [1; 1]; % 初值条件
% 调用ode45函数求解微分方程组
[t,y] = ode45(ode, tspan, y0);
% 绘制结果
plot(t, y(:,1), 'r', t, y(:,2), 'b');
xlabel('t'); ylabel('y');
legend('y', 'x');
% 定义微分方程组
f = @(t, y) [-y(1)*y(2); -y(1)*y(2)-y(2)];
% 定义初始条件
t0 = 0;
y0 = [1; 1];
% 求解微分方程组
[t, y] = ode45(f, [t0, 10], y0);
% 绘制解的图像
plot(t, y(:, 1), t, y(:, 2));
legend('y', 'x');
xlabel('t');
ylabel('y, x');
解决方案:
使用Matlab中的ode45函数,先定义微分方程组的函数形式,然后调用ode45函数求解。
代码如下:
function dydx = diff_eqns(t,yx)
% 定义微分方程组的函数形式
x = yx(1);
y = yx(2);
dydx = [-x*y; -x*y-y];
end
% 定义初始条件
yx0 = [1; 1];
% 解微分方程组
[t, yx] = ode45(@diff_eqns, [0 10], yx0);
% 绘制结果
plot(t, yx(:,1), t, yx(:,2));
legend('x', 'y');
xlabel('time');
ylabel('x y');
title('Solution of the differential equations system');
在命令行窗口中运行以上代码,即可得到微分方程组的数值解,同时绘制出x和y关于时间的图像。