如何将 fzero 的结果放入列矩阵

How to put results from fzero into a column matrix

我想我已经解决了这个问题,但是如何将每次迭代的 x 值从 fzero 上传到列矩阵中?我已经使用二分法找到了零并且结果似乎匹配。

当我运行代码:

% second part
clear all; close all;
x= 100:0.01:200; % domain
f = @(x) sqrt((x*9.8)/0.25)* tanh(sqrt((0.25 * 9.8) / x) * 4) - 36;

%x02 = fzero(f, 100, optimset('Tolx', 1*e*(-6))) % What's the point
%of this?
x03 = fzero(f, 100, optimset('Display', 'iter')) % 100 is initial starting point

我明白了:

Search for an interval around 100 containing a sign change:
 Func-count    a          f(a)             b          f(b)        Procedure
    1             100      -1.22895           100      -1.22895   initial interval
    3         97.1716      -1.34001       102.828      -1.12319   search
    5              96      -1.38767           104      -1.08085   search
    7         94.3431      -1.45679       105.657      -1.02236   search
    9              92      -1.55819           108     -0.942334   search
   11         88.6863      -1.70937       111.314     -0.834198   search
   13              84      -1.94034           116     -0.690568   search
   15         77.3726      -2.30675       122.627     -0.504071   search
   17              68      -2.92417           132     -0.268914   search
   19         54.7452      -4.07851       145.255     0.0168687   search

Search for a zero in the interval [54.7452, 145.255]:
 Func-count    x          f(x)             Procedure
   19         145.255     0.0168687        initial
   20         144.882    0.00947591        interpolation
   21         144.405  -2.82264e-05        interpolation
   22         144.407   8.38448e-08        interpolation
   23         144.407    7.4607e-13        interpolation
   24         144.407  -1.42109e-14        interpolation
   25         144.407             0        interpolation

Zero found in the interval [54.7452, 145.255]

x03 =

  144.4067

使用二分法与 144.4092 进行比较。此外,我尝试将公差设置为 10(-6),但无法正常工作,我做错了什么?还是我处理这一切都错了?

我的猜测是,该问题希望您复制并粘贴 fzero. However, that's kind of lame, copy-and-pasting data is a bad habit to get into, and the results only have few decimal places of precision. You can do this using an output function and the 'OutputFcn' option'Display' 选项的输出,尽管这有点 hack。

f = @(x)sqrt((x*9.8)/0.25)*tanh(sqrt((0.25*9.8)./x)*4)-36;
global xout fout;
opts = optimset('Display', 'iter', 'TolX', 1e-6, 'OutputFcn', @outfun);
[x0, f0, exitflag] = fzero(f, 100, opts)
xout
fout

其中 outfun 是一个子函数(或单独的 M 文件)定义为:

function stop = outfun(x, optVals, state)
stop = false;
global xout fout;
switch(state)
    case 'init'
        xout = x;
        fout = optVals.fval;
    case 'iter'
        if ~strcmp(optVals.procedure,'search')
            xout = [xout;x];
            fout = [fout;optVals.fval];
        end
    otherwise

end

当然还有其他方法可以做到这一点,并且可能有一些避免使用 global。另外,请注意,我没有在 fzero 的搜索阶段(根括号)输出状态,因为向量是恒定的。