在以下脚本中,
syms x1 x2 x3;
f = -x1-x2-x3;
u = 1;
B = -log(20 - x1) - log(20 - x2) - log(25 - x3) - log(log(x1) - x2) - log(30 - x1 - x2) - log(30 - x1 - x3) - log(x1) - log(x2) - log(x3);
Theta = f + u * B;
% LS (x0, a, b, c)
xk = [5; 1.5; 5];
e = 0.1;
% Back Tracking (b, c, a)
b = 0.5;
c = 0.1;
a = 1;
% Hoising
D_Theta = gradient(Theta);
% LS Algorithm
while (norm(subs(D_Theta, [x1; x2; x3], xk)) >= e)
Pk = - subs(D_Theta, [x1; x2; x3], xk);
% Back Tracking Algorithm
while (double(subs(Theta, [x1; x2; x3], xk + a * Pk)) > double(subs(Theta, [x1; x2; x3], xk) + c * a * Pk' * subs(D_Theta, [x1; x2; x3], xk)))
a = a * b;
double(a)
end
xk = xk + a * Pk;
double(xk)
end
评估内环状况(第19行)的时间令人惊讶这么长(即需要数小时!)。
是什么原因以及如何解决?
答案 0 :(得分:1)
除非您特别需要保持数字变量符号,否则只需将它们设为数字:
将行更改为xk=double(xk);
并且迭代不会增加时间消耗,因为代码的最大问题是xk
中的数字在每次迭代中获得越来越多的术语。
通过此更改,整个代码在我的PC中占用9.7秒