Matlab - 函数:返回变量向量

花花公子

我正在实施用于根查找的定点方法。下面是代码:

clear;
clc;

f = inline('y^4 - 2', 'y');
phii = inline('y - (y^4 -2)/(4*(y^3))', 'y');
x0 = 4;
toler = 10^-10;
nmax = 1000;

a = fixpoint(x0,toler,nmax,f,phii)

function[alpha,res,iters] = fixpoint(x_0,tol,max_iters,fun,phi)

    error = tol +1;
    iters = 0;
    xvect = x_0;
    x = x_0;
    res = fun(x);
    xdif = [];

    while iters < max_iters & error > tol
        iters = iters +1;
        x_n = phi(x);
        error = abs(x_n - x);
        xdif = [xdif; error];
        x = x_n;
        xvect = [xvect;x];
        res = [res;fun(x)];
    end
    alpha = xvect(end);
    residual = res(end);
    return
end

该方法可以正常工作。我遇到的唯一问题是我希望它返回所有三个:alpha、res(一个向量,其中最后一个条目是重要性的残差,但需要所有值)和迭代。

目前它只返回 alpha。

解决这个问题的最佳方法是什么?

挤压
[alpha,res,iters] = fixpoint(x0,toler,nmax,f,phii)

另外,return这里不需要

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章