使用MATLAB编写GS方法的位置搜索程序
function [x,iter] = gs(f,x0,eps) % GS - Gauss-Seidel method position search for f(x) = 0 % % Input: % - f: function handle for f(x) % - x0: initial guess for x % - eps: tolerance for stopping criterion % % Output: % - x: the approximated root of f(x) = 0 % - iter: the number of iterations used % % Example usage: % syms x; % f = x^2 - 2; % [x,iter] = gs(@(x) eval(subs(f,x)),1,1e-6)
iter = 0; x = x0; dx = eps + 1; while dx > eps x1 = f(x); dx = abs(x1 - x); x = x1; iter = iter + 1; end
end
原文地址: https://www.cveoy.top/t/topic/bsJR 著作权归作者所有。请勿转载和采集!