matlab - How to reduce computational time in DDE simulation on Matlab
问题描述
I need to simulate a network of nodes. The weights of the edges are being given in a matrix . Due to the non-zero distances between the nodes, we consider time-delays, which are computed given the distances and an arbitrary velocity. The delay values have already been calculated. Given that A is symmetric, for a n by n matrix, we have m=n(n-1)/2 different time-delays.
load('A.mat'); % A is a symmetric matrix of dimensions n by n
load('tau.mat'); % tau is the delay array vector with n(n-1)/2 components
tf = 10;
sol = dde23(@(t,y,z)(dde(t,y,z,A)),tau,@(t)(history(t,A)),[0 tf]);
t = linspace(0,tf,200);
y = deval(sol,t);
figure
plot(t,y)
The network needs to be expressed in its analytic form as a dynamical system:
x_dot = A0* x + A1 *x(t-tau1) + ... + Am *x(t-tau_m)
, where A0 is the diagonal matrix of the initial A matrix, where it has been considered that since the distance between a node and itself is zero, there is no delay. In the absence of a way to initialize m different matrices of n by n, I created a P matrix as below. Each symmetric pair of A matrix would be put in its respective place of P and multiplied with the respective delay.
function dydt = dde(t,y,yd,A)
n = length(A);
A0 = diag(diag(A));
m = n*(n-1)/2;
P = zeros(n,n*m);
k = 0;
for i = 1:n
for j = 1:n
if j>i
P(i,j+n*k) = A(i,j);
P(j,i+n*k) = A(i,j);
k = k+1;
end
end
end
dydt = A0*y;
for c = 0:m-1
dydt = dydt + P(:,n*c+1:(c+1)*n)*yd(:,c+1);
end
end
function y = history(t,A)
y = rand(length(A),1);
end
If the code is technically correct, it is not efficient, since the time it does to produce results increase exponentially even for small number of nodes. For instance, for a network of 7 nodes it takes 2.1 seconds, for 8 nodes 7.6 seconds, for 9 nodes 30 seconds and for 10 nodes almost 121 seconds. This makes its use prohibitive for large networks. Does this make sense? Is there anything to do to decrease the time of computation?
解决方案
推荐阅读
- linux - 使用 bash 从标准输入读取二进制文件
- excel - 如何自动编号直到检测到合并单元格?
- python - 请求从 ASP 更改语言
- android - 我应该在将用户密码发送到服务器之前对其进行加密吗?
- android - 在一个 XAML 元素中从 2 个 ViewModel 绑定
- elasticsearch - 嵌套类型的 Elasticsearch 布尔查询中的模型“或 TRUE”?
- javascript - 如何使用expressjs将数据从一个路由传递到另一个nodejs
- sql - 在 SQL 中创建 Excel 日期时间序列/小数部分值
- c++ - 跳转到输入坐标定义的控制台屏幕位置
- kubernetes - gitlab runner cache“找不到缓存工厂”