【问题标题】:Simple Fixed Point Iteration MATLAB简单的定点迭代 MATLAB
【发布时间】:2018-02-21 01:25:02
【问题描述】:

我尝试编写定点迭代来找到 (x+1)^(1/3) 的解决方案。我不断收到以下错误: 错误:'g'未定义在第 17 行第 6 列附近 错误:调用自 第 17 行第 4 列的定点

    clear -all;
clc;

function f = f(x)

    f = (x+1)^(1/3)

    f = g(x)
end

# Start out iteration loop
x1 = 0;
x2 = g(x1);

iterations = 0; # iteration counter

while abs(x2-x1 > 1e-5)
    plot([x1 x1], [x1 g(x1)], 'k-')
    plot([x1 x1], [x1 g(x1)], 'k--')
    pause
    x1 = x2;
    x2 = g(x1);
    iterations = iterations + 1;
end

iterations
x1
x2

我不知道出了什么问题。我的逻辑似乎是正确的。

【问题讨论】:

    标签: matlab iteration fixed point


    【解决方案1】:

    我对你的代码稍作修改,可以得到f(x)=cos(x)-x的解决方案,你可以把g(x)改成你想要的。

    clear;
    clc;
    
    %# You function here
    g=@(x) cos(x);
    
    %# Start out iteration loop
    x1 = 0;
    x2 = g(x1);
    
    iterations = 0;% # iteration counter
    
    ezplot(g,[0,1]);
    hold on 
    ezplot('x',[[0,1]])
    
    while (abs(x2-x1) > 1e-5 && iterations<100)
        plot([x1 x1], [x1 x2], 'k-')
        plot([x1 x2], [x2 x2], 'k--')
         %pause
        iterations = iterations + 1;
        x1 = x2;
        x2 = g(x1);
    end
    iterations 
    [x1 x2]
    

    解决办法是:

    iterations =
    
        29
    
    ans =
    
        0.7391    0.7391
    

    使用输出图像:


    但是,您确定您的功能是正确的吗?看来这个函数不能用定点迭代来解决,因为这里f(x)=0等于g(x)=xg(x)=(x+1)^(1/3)+x。但是如果我们用h(x)=x(红色曲线)绘制g(x)(蓝色曲线),我们有:

    所以如果我们从 0 开始,迭代不能收敛(x1 会急剧增加但根是-1)。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多