Matlab 保存递归函数的中间变量值

Matlab 保存递归函数的中间变量值,matlab,recursion,save,Matlab,Recursion,Save,我试图编写一个计算差商的函数。我需要用这个来做多项式插值。给定节点x=linspace(a,b,n+1),节点上的函数值y=func(x)我想找到差商f[x0],f[x_0,x_1],…,f[x_0,x_1,…,x_n]。要计算f[xu 0,xu 1,…,xu n]我需要f[xu 0,xu 1,…,xu(n-1)]等,因此最好将中间步骤保存到f[xu 0,xu 1,…,xu n],这样在我到f[xu 0,xu 1,…,xu n]的过程中,我会将前面的差商保存为向量的元素 有人能告诉我如何更正代码

我试图编写一个计算差商的函数。我需要用这个来做多项式插值。给定节点
x=linspace(a,b,n+1)
,节点上的函数值
y=func(x)
我想找到差商
f[x0],f[x_0,x_1],…,f[x_0,x_1,…,x_n]
。要计算
f[xu 0,xu 1,…,xu n]
我需要
f[xu 0,xu 1,…,xu(n-1)]
等,因此最好将中间步骤保存到
f[xu 0,xu 1,…,xu n]
,这样在我到
f[xu 0,xu 1,…,xu n]
的过程中,我会将前面的差商保存为向量的元素

有人能告诉我如何更正代码以保存适当的差商值吗?代码如下:

function [fx, all_fx] = ilo2(a,b,x,y,fx,all_fx)
    if a == b
        fx(end+1) = y(a);
        if a == 1
            all_fx(end+1) = fx(end);
        end
        return
    end
    a;
    b;
    [c, all_fx] = ilo2(a+1,b,x,y,fx,all_fx);
    [d, all_fx] = ilo2(a,b-1,x,y,fx,all_fx);
    fx(end+1) = (c-d)/(b-a);
    if a == 1
        all_fx(end+1) = fx(end);
    end
end

如果a==1,我需要的差商是在'
条件下。

好的,我想我修正了它:

function [all_fx,fx] = ilo(a,b,x,y,all_fx,fx)
    if a == b
        fx(end+1) = y(a);
        if a == 1
            all_fx(end+1) = fx(end);
        end
        return
    end
    [all_fx,c] = ilo(a+1,b,x,y,all_fx,fx);
    [all_fx,d] = ilo(a,b-1,x,y,all_fx,fx);
    fx(end+1) = (c-d)/(b-a);
    if a == 1
        all_fx(end+1) = fx(end);
    end
end