⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newtonsys.m

📁 这是一种不错的牛顿算法
💻 M
字号:
function [x, niter] = newtonsys(f, x0, tol, maxiter, varargin)% NEWTONSYS Newton's method for a system of equations.%    % Input parameters:%   f: Name of model function as a string. The function must be%      defined in the following way: the first argument must take a%      1xn column vector with the values x_1, x_2, ..., x_n for%      the evalation of f. The second argument of the function must%      accept a FLAG. If FLAG is empty, the function must be%      evaluated regularly, and the result must be returned as a%      colunm vector. If the FLAG is set to 'jacobian', the%      function must return the evaluation result of the jacobian%      matrix instead (as nxn matrix). After the FLAG parameter,%      the function may specify an arbitrary number of additional%      parameters. See below for an example model function%      definition.%   x0: initial guess, a 1xn column vector.%   tol: Error tolerance for the relative increment of x.%   maxiter: maximum number of iterations to perform.%   p1, p2, ..., pn: An arbitrary number of parameters that are%                    passed on to the function f when evaluated.%% Output paramters:%	  x: Approximation to the solution, 1xn column vector.%   niter: Number of iterations performed.%%      Example for calling newton:%        [x, niter] = newton('robotarm', [pi/2; pi/2], 1e-5, ...%                            20, [4; 2], [3; 4])%%      Example for the definition of the function f:%        function out = robotarm(x, flag, l, p)%        if isempty(flag)%          flag = '';%        end%        switch lower(flag)%         case ''%	         out = [l(1)*cos(x(1))-l(2)*cos(x(1)+x(2))-p(1); ...%                 l(1)*sin(x(1))-l(2)*sin(x(1)+x(2))-p(2)];	%         case 'jacobian'%	         out = [-l(1)*sin(x(1))+l(2)*sin(x(1)+x(2)), ...%                 l(2)*sin(x(1)+x(2)); ...%                 l(1)*cos(x(1))+l(2)*cos(x(1)+x(2)), ...%                 -l(2)*cos(x(1)+x(2))];	%        end%% Note: The newton method will fail if the determinant of the%       jacobian matrix becomes zero.	% Author : Andreas Klimke, Universit鋞 Stuttgart% Version: 1.0% Date   : June 13, 2003% Initializationx = x0;x_old = x;niter = 0;for k = 1:maxiter	% Iteration step	x = x - feval(f, x, 'jacobian', varargin{:}) \ ...			feval(f, x, [], varargin{:});	niter = niter + 1;			% Break condition	if norm(x) < eps		if norm(x-x_old) <= tol			return		end	else		if norm(x-x_old)/norm(x) <= tol			return		end	end		x_old = x;endwarning('Maximum number of iterations reached.');	

⌨️ 快捷键说明

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