system of equations runs slow

1 次查看(过去 30 天)
Conrado Garcia
Conrado Garcia 2020-4-23
评论: darova 2020-4-23
Hi to everyone!
Here I present a problem that occurs to me when running a simulation
I have to solve a system of equations multiple times. The system is linear and can be solved by x=A\b. there are 273 variables. The system has to be solved 87600 times. Since every iteration depends on the results of the previous time step (t) the matrix A is changing. In the code below you have my approach. R(j,i) and C(j,i) are previously calculated.
The problem is that for every iteration of t it takes about 1 minute.
syms T [length(depth) length(r)]
syms Tbor
k=1;
t=1;
Tsol=Tm*ones(length(depth),length(r),tsimulation);
for t=1:tsimulation
for j=1:length(depth)
tic
for i=1:length(r)
Tprev=Tm;
if i==1 && j==1
% eqns(k)= 2*(Tf0(t)-Tb)/Rb + (T(j,1)-Tb)/R(j,1)== 0;
% k=k+1;
eqns(k)= (Tb(t)-T(j,i))/R(j,i) +(T(j,i+1)-T(j,i))/R(j,i+1) + (Tsurface(t)-T(j,i))/Rprima + (T(j+1,i)-T(j,i))/Rprima - C(j,i)*(T(j,i)-Tprev)/paso ==0;
end
if i==1 && j>1 && j<length(depth)
% eqns(k)= 2*(Tf0(t)-Tb(t))/Rb + (T(j,1)-Tb(t))/R(j,1)== 0;
% k=k+1;
eqns(k)= (Tb(t)-T(j,i))/R(j,i) +(T(j,i+1)-T(j,i))/R(j,i+1) + (T(j-1,i)-T(j,i))/Rprima + (T(j+1,i)-T(j,i))/Rprima - C(j,i)*(T(j,i)-Tprev)/paso ==0;
end
if i>1 && j==1 && i<length(r)
eqns(k)= (T(j,i-1)-T(j,i))/R(j,i) +(T(j,i+1)-T(j,i))/R(j,i+1) + (Tsurface(t)-T(j,i))/Rprima + (T(j+1,i)-T(j,i))/Rprima - C(j,i)*(T(j,i)-Tprev)/paso ==0;
end
if i>1 && j>1 && i<length(r) && j<length(depth)
eqns(k)= (T(j,i-1)-T(j,i))/R(j,i) +(T(j,i+1)-T(j,i))/R(j,i+1) + (T(j-1,i)-T(j,i))/Rprima + (T(j+1,i)-T(j,i))/Rprima - C(j,i)*(T(j,i)-Tprev)/paso ==0;
end
if i==length(r) || j==length(depth)
eqns(k)= T(j,i)==Tm;
end
k=k+1;
end
end
[A b]=equationsToMatrix(eqns,T);
Tb(t)=(-2*R(j,1)*Tf0(t)-Rb*Tsol(j,1,t))/(-2*R(j,1)-Rb);
if t==1
Tprev=Tm;
else
Tprev=Tsol(j,i,t-1);
end
Tcalc=A\b; toc;
Tcalc=double(Tcalc);
% Tcalc=[Tcalc{:}];
% Tcalc=double(Tcalc);
for j=1:length(depth)
for i=1:length(r)
Tsol(j,i,t)=Tcalc(i);
end
end
t
end
  1 个评论
darova
darova 2020-4-23
Can you tell which part of your code takes more (solving the system or creating using syms)?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by