checkgrad.m

来自「一个很好的Matlab编制的数据降维处理软件」· M 代码 · 共 52 行

M
52
字号
function d = check(f, X, e, P1, P2, P3, P4, P5);% checkgrad checks the derivatives in a function, by comparing them to finite% differences approximations. The partial derivatives and the approximation% are printed and the norm of the diffrence divided by the norm of the sum is% returned as an indication of accuracy.%% usage: checkgrad('f', X, e, P1, P2, ...)%% where X is the argument and e is the small perturbation used for the finite% differences. and the P1, P2, ... are optional additional parameters which% get passed to f. The function f should be of the type %% [fX, dfX] = f(X, P1, P2, ...)%% where fX is the function value and dfX is a vector of partial derivatives.%% Carl Edward Rasmussen, 2001-08-01.% This file is part of the Matlab Toolbox for Dimensionality Reduction v0.4b.% The toolbox can be obtained from http://www.cs.unimaas.nl/l.vandermaaten% You are free to use, change, or redistribute this code in any way you% want for non-commercial purposes. However, it is appreciated if you % maintain the name of the original author.%% (C) Laurens van der Maaten% Maastricht University, 2007argstr = [f, '(X'];                            % assemble function call stringsargstrd = [f, '(X+dx'];for i = 1:(nargin - 3)  argstr = [argstr, ',P', int2str(i)];  argstrd = [argstrd, ',P', int2str(i)];endargstr = [argstr, ')'];argstrd = [argstrd, ')'];[y dy] = eval(argstr);                         % get the partial derivatives dydh = zeros(length(X),1) ;for j = 1:length(X)  dx = zeros(length(X),1);  dx(j) = dx(j) + e;                               % perturb a single dimension  y2 = eval(argstrd);  dx = -dx ;  y1 = eval(argstrd);  dh(j) = (y2 - y1)/(2*e);enddisp([dy dh])                                        % print the two vectorsd = norm(dh-dy)/norm(dh+dy);       % return norm of diff divided by norm of sum

⌨️ 快捷键说明

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