【问题标题】:Secant Method not converging, Matlab正割法不收敛,Matlab
【发布时间】:2018-02-24 20:33:56
【问题描述】:

我在 Matlab 中创建了一个程序来尝试找到 f(x) = exp(2x) + 3x - 4 的根(我的代码中的函数“fopg1”)。我的代码如下:

format long
tic;
for dum=1:1000;
    x(1) = 0.5;
    x(2) = 0.4;
    err_tol = 1e-8;
    iteration(1) = 1;
    n = 3;
    while err_estimate > err_tol
        iteration(n) = n;
        x(n) = x(n-1) - fopg1(x(n-2)) * ((x(n-1) - x(n-2)) / (fopg1(x(n-1)) - fopg1(x(n-2))));
        err_estimate(n) = abs(x(n) - x(n-1));
        n = n + 1;
    end
%end
time = toc;
avgtime = time/1000;
A = [iteration' x' err_estimate' tbd'];
f = '%2i %13.9f %13.9f %7.3f'; compose(f,A)

不幸的是,这并没有收敛。我觉得应该。我的程序有缺陷还是实际上没有收敛?提前致谢。

马丁

【问题讨论】:

  • 好吧,xf(x) = 0 肯定有一些,因为f(x) 是连续的(连续函数的组合),f(0) = -3f(1) > 0。因此,您的函数似乎存在逻辑错误。
  • dum 是一个 for 循环,用于获得超过 1000 次迭代的平均 tic toc 时间。一端不见了,我会解决的
  • 这就是重点。测量平均计算时间,因为 matlab 计算时间有时会有所不同。

标签: matlab convergence


【解决方案1】:

几天前我回答了一个非常相似的问题here。使用相同的代码,没有迭代限制并增加容差(1e-8 根据您的示例),我使用割线方法检测到 exp(2x) + 3x - 4 的预期收敛:

clear();
clc();

com = Inf;
i = 2;
err_tol = 1e-8;

f = @(x) exp(2*x) + 3*x - 4;

x(1) = 0.5;
x(2) = 0.4;

while (abs(com) > err_tol)
    com = f(x(i)) * (x(i) - x(i-1)) / (f(x(i)) - f(x(i-1)));
    x(i+1)= x(i) - com;

    i = i + 1;
    n = n - 1;
end

display(['Root X = ' num2str(x(end))]);

我收到的消息是:Root X = 0.47369。在此代码中实现附加数据对您来说应该不难。

【讨论】:

  • 非常感谢,我今天会看看这个。 1000 次迭代用于测量平均时间,对正割法本身没有任何影响。
猜你喜欢
  • 2014-09-08
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
相关资源
最近更新 更多