fxstiff.m

来自「matlab算法集 matlab算法集」· M 代码 · 共 36 行

M
36
字号
function dfdx = fxstiff (t,x,f)
%---------------------------------------------------------------------- 
% Usage:       dfdx = fxstiff (t,x,f)
%
% Description: Estimate the Jacobian matrix J = df(t,x)/dx numerically
%              using central differences. Return the result in the
%              n by n matrix dfdx. 
%
% Inputs:      t = time
%              x = n by 1 state vector 
%              f = string containing name of function whose 
%                  derivatives are to be estimated.
%
% Outputs:     dfdx = n by n Jacobian matrix containing derivatives,
%                     dfdt(k,j) = df(k)/dx(j). 
%---------------------------------------------------------------------- 

   chkvec (x,2,'fxstiff');
   chkfun (feval(f,t,x),3,'fxstiff');
   n = length(x);
   x0 = zeros (n,1);
   x1 = zeros (n,1); 
   x2 = zeros (n,1);
   d = eps^(1/3);
   x0 = x;
   for j = 1 : n
      x0(j) = x(j) + d;  	
      x1 = feval(f,t,x0);
      x0(j) = x(j) - d;
      x2 = feval(f,t,x0);
      x0(j) = x(j);
       for i = 1 : n
         dfdx(i,j) = (x1(i) - x2(i))/(2*d);
       end
   end

⌨️ 快捷键说明

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