带有时滞的常微分方程组的画图matlab 代码
时间: 2023-10-17 15:05:39 浏览: 142
假设你有一个带有时滞的常微分方程组,形式如下:
dx/dt = f(x(t), x(t - τ))
其中,x是一个向量,f是一个函数,τ是时滞时间。你想使用Matlab画出x的轨迹,可以按照以下步骤操作:
1. 定义方程组
```matlab
function dxdt = dde_system(t, x, Z)
% x(t) = x1(t), x2(t), ..., xn(t)
% Z是一个矩阵,其中每一列是x(t - τ),即x(t - τ), x(t - 2τ), ..., x(t - nτ)
% 在这个例子中,函数f是一个简单的线性函数
A = [-0.5, 0; 0.5, -0.5];
tau = 1;
dxdt = A * x + [1; 0.5] * Z(:, end);
end
```
2. 定义初始条件和时滞
```matlab
x0 = [1; 0]; % 初始条件
tau = 1; % 时滞
```
3. 设置绘图参数
```matlab
tspan = [0, 20]; % 时间范围
options = ddeset(); % 设置ddeset选项
sol = dde23(@dde_system, tau, @history, tspan, options); % 求解方程组
```
4. 定义历史函数
```matlab
function y = history(t)
% 历史函数定义了t < 0时的值
if t < 0
y = [1; 0];
else
y = deval(sol, t);
end
end
```
5. 绘图
```matlab
figure();
plot(sol.x, sol.y(1, :), 'b-', 'LineWidth', 2); % 绘制x1的轨迹
hold on;
plot(sol.x, sol.y(2, :), 'r-', 'LineWidth', 2); % 绘制x2的轨迹
xlabel('时间');
legend('x1', 'x2');
```
完整的代码如下:
```matlab
function dxdt = dde_system(t, x, Z)
% x(t) = x1(t), x2(t), ..., xn(t)
% Z是一个矩阵,其中每一列是x(t - τ),即x(t - τ), x(t - 2τ), ..., x(t - nτ)
% 在这个例子中,函数f是一个简单的线性函数
A = [-0.5, 0; 0.5, -0.5];
tau = 1;
dxdt = A * x + [1; 0.5] * Z(:, end);
end
function y = history(t)
% 历史函数定义了t < 0时的值
if t < 0
y = [1; 0];
else
y = deval(sol, t);
end
end
x0 = [1; 0]; % 初始条件
tau = 1; % 时滞
tspan = [0, 20]; % 时间范围
options = ddeset(); % 设置ddeset选项
sol = dde23(@dde_system, tau, @history, tspan, options); % 求解方程组
figure();
plot(sol.x, sol.y(1, :), 'b-', 'LineWidth', 2); % 绘制x1的轨迹
hold on;
plot(sol.x, sol.y(2, :), 'r-', 'LineWidth', 2); % 绘制x2的轨迹
xlabel('时间');
legend('x1', 'x2');
```
阅读全文
相关推荐
















