fixed_point.m

来自「matlab function ---> find roots using」· M 代码 · 共 38 行

M
38
字号
function fixed_point(p0, N)

%Fixed_Point(p0, N) approximates the root of the equation f(x) = 0
%rewritten in the form x = g(x), starting with an initial approximation p0. 
%The iterative technique is implemented N times.
%The user has to enter the function g(x)at the bottom

i = 1;
p(1) = p0;
tol = 1e-06;
while i <= N
   p(i+1) = g(p(i));
   if abs(p(i+1)-p(i)) < tol		%stopping criterion
      disp('The procedure was successful after k iterations')
      k = i
      disp('The root to the equation is')
      p(i+1)
      return
   end
   i = i+1;
end

if abs(p(i)-p(i-1)) > tol | i > N
   disp('The procedure was unsuccessful')
   disp('Condition |p(i+1)-p(i)| < tol was not sastified')
   tol
   disp('Please, examine the sequence of iterates')
   p = p'
   disp('In case you observe convergence, then increase the maximum number of iterations')
   disp('In case of divergence, try another initial approximation p0 or rewrite g(x)')
   disp('in such a way that |g''(x)|< 1 near the root')
end

%this part has to be changed accordingly with the specific function g(x)
function y = g(x);

y = 5/x^2+2;
   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?