在MATLAB中求解多个非线性独立方程的最快方法? [英] Fastest method to solve multiple nonlinear independent equations in MATLAB?

查看:259
本文介绍了在MATLAB中求解多个非线性独立方程的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB有两种方法来求解非线性方程:

MATLAB has two methods to solve a nonlinear equation:

因此,可以使用以下方法来求解n非线性独立方程组:

Therefore, one can use the following methods to solve a system of n nonlinear independent equations:

  1. 使用循环使用fzero
  2. 分别求解方程式
  3. 使用循环使用fsolve
  4. 分别求解方程式
  5. 使用fsolve一起解决它们
  1. Use a loop to solve the equations separately using fzero
  2. Use a loop to solve the equations separately using fsolve
  3. Use fsolve to solve them together

我的直觉是:

  • 对于大型n,由于复杂度(梯度计算)为0(n ^ 2),循环方法比单个系统要快
  • 对于较小的n,循环可能会更慢,因为循环在MATLAB中具有较高的开销,并且可能会有一些恒定的启动时间
  • fzerofsolve快,因为它专门针对单个非线性方程.
  • A loop method is faster than a single system for large n as complexity (gradient calculation) is 0(n^2)
  • A loop may be slower for small n as a loop has a high overhead in MATLAB and there may be some constant startup time
  • fzero is faster than fsolve as it is specifically made for a single nonlinear equation.

问题:解决此问题的最快方法是什么?应该使用哪些选项来加快过程?

Question: What is the fastest method to solve this problem? Which options should be used to speed up the process?

相关线程

  • Solving multiple independent non linear equations
  • Solve a large number of independent nonlinear equations with fsolve without loops

推荐答案

评估某种方法性能的最佳方法是编写基准.考虑了四种情况:

The best approach to evaluate the performance of some method is to write a benchmark. Four cases are considered:

  1. 循环fzero :使用循环使用fzero
  2. 分别求解方程式
  3. 循环fsolve :使用循环使用fsolve
  4. 分别求解方程式
  5. 默认fsolve :将方程组作为一个方程组一起求解
  6. 独立解决方案:与默认解决方案相同,但
  1. loop fzero: uses a loop to solve the equations separately using fzero
  2. loop fsolve: uses a loop to solve the equations separately using fsolve
  3. default fsolve: solves the equations together as one system of equations
  4. independent fsolve: same as default fsolve, but specifies that the equations are independent


f = @(x) x.^2-1; % the set of non-linear equations
ns = 1:1:100; % the sizes for which the benchmark is performed
options=optimset('Display','off'); % disable displaying

figure
hold on
plot(ns, loopFSolve(f, ns, options), 'DisplayName', 'loop fsolve')
plot(ns, loopFZero(f, ns, options), 'DisplayName', 'loop fzero')
plot(ns, defaultFSsolve(f, ns, options), 'DisplayName', 'default fsolve')
plot(ns, independentFSolve(f, ns, options), 'DisplayName', 'independent fsolve')

legend ('Location', 'northwest')

function t = loopFZero(f, ns, options)
  t1 = timeit(@() fzero(f, rand(1), options));
  t = ns * t1;
end

function t = loopFSolve(f, ns, options)
  t1 = timeit(@() fsolve(f, rand(1), options));
  t = ns * t1;
end

function t = defaultFSsolve(f, ns, options)
  t = zeros(size(ns));
  for i=1:length(ns)
    n = ns(i);
    un = rand(n, 1);
    t(i) = timeit(@() fsolve(f, un, options));
  end
end

function t = independentFSolve(f, ns, options)
  t = zeros(size(ns));
  for i=1:length(ns)
    n = ns(i);
    un = rand(n, 1);
    options.Algorithm = 'trust-region-reflective';
    options.JacobPattern = speye(n);
    options.PrecondBandWidth = 0;

    t(i) = timeit(@() fsolve(f, un, options));
  end
end

结果

所有数字均显示了根据n函数的完整系统的计算时间,等式数量.

All the figures show the computation time for the complete system in function of n, the number of equations.

前两个图将n绘制到1000,间隔为100.后两个图将n绘制到100,间隔为1.对于每个图,第二个图与第一个图相同.一个但没有 loop fzero 的人,因为它比其他人慢得多.

The first two figures plot n up to 1000, with an interval of 100. The last two figures plot n up to 100 with an interval of 1. For each, the second plot is the same as the first one but without loop fzero as it is much slower than the others.

结论

  1. 循环解决:请勿使用,启动时间过长
  2. 循环fzero :您可以将其用于小型n(n < ~20的最快方法)
  3. 默认fsolve :您可以将其用于较小的n(最快的方法为~20 < n < ~50,但是与2和3的差异相对较小).
  4. 独立解决方案:您应将其用于大型n(~50 < n的最快方法)
  1. loop fsolve: do not use it, startup time is too high
  2. loop fzero: you can use it for small n (fastest method for n < ~20)
  3. default fsolve: you can use it for relative small n (fastest method for ~20 < n < ~50, but difference with 2 and 3 is relative small).
  4. independent fsolve: you should use it for large n (fastest method for ~50 < n)

通常,应该使用 independent fsolve ,仅适用于较小的n loop fzero ,即使用具有以下选项:

In general, you should use independent fsolve, only for small n loop fzero may be used instead, i.e. use fsolve with the following options:

options.Algorithm = 'trust-region-reflective';
options.JacobPattern = speye(n);
options.PrecondBandWidth = 0;

懒惰的人可能只使用默认fsolve ,因为它对于中等数量的方程(n < ~200)

Lazy people may just use the default fsolve as it has a reasonable performance for a moderate number of equations (n < ~200)

重要说明

Bob指出,fzero矢量化版本可能快一个数量级.我尚未对其进行测试,但是如果需要最佳性能,则绝​​对应该考虑使用它.

Bob pointed out that a vectorised version of fzero may be an order of magnitude faster. I have not tested it yet, but you should definitely consider it if you need maximal performance.

备注

  • 请注意,默认fsolve 的时间复杂度为O(n ^ 2),其他时间为O(n).
  • 请注意,在某些边界情况下,fzerofsolve的行为可能有所不同,例如fzero在搜索符号变化的位置时找不到x^2的解决方案.
  • Note that the time complexity of default fsolve is O(n^2) while the others are O(n).
  • Note that fzero and fsolve may behave differently in some boundary cases, f.e fzero will not found a solution for x^2 as it searches the location of a sign change.

这篇关于在MATLAB中求解多个非线性独立方程的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆