不动点迭代法的matlab实现

不动点迭代法(Fixed point iteration method)

原理网上已经很多了,这里不再赘述。
需要注意的是不动点迭代法的使用条件。

举个简单的例子,利用不动点法求解方程f(x) = 1 + 0.5*sin(x) − x = 0在(1,2)区间的根。
令g(x)=1+0.5*sin(x)


g = @(x) fun2(x);
% Initialization
x0 = 0;
tol = 1e-5;
maxIter = 40;

% Test biSection function
[xStar,xRoot] = fixPoint(g,x0,tol,maxIter);
fprintf('The fixed point is: %d\n',xStar);
fprintf('The root of the equation is: %d\n',xRoot);

function [xStar,xRoot] = fixPoint(fun2,x0,tol,maxIter)
% Inputs:
% fun2: a function handle, standing for the function written above
% x0: the initial guess of the fixed point
% tol: the tolerance within which the program can stop
% maxIter: the maximum number of iterations the program is allowed to run
% Outputs:
% xStar: the numerical value of the fixed point
% xRoot: the numerical value of the root

    x = zeros(maxIter,1);
    x(1) = fun2(x0);
    i = 1;

    while abs(fun2(x(i))-x(i))>tol && i<maxIter
        x(i+1) = fun2(x(i));
        xStar = x(i);
        i = i+1;
    end
    xRoot = x(i);

end

function gx = fun2(x)
    gx = 1+0.5*sin(x);
end

  • 11
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值