35 lines
890 B
Matlab
35 lines
890 B
Matlab
function [f, J] = numdiff(fun,x,delta)
|
|
%
|
|
%
|
|
% [f, J] = numdiff(fun,x,delta) computes jacobian with difference quotient
|
|
%
|
|
% INPUT
|
|
% fun function handle
|
|
% x point x to evaluate f and approximate jacobian
|
|
% delta stepsize for difference approximation
|
|
% OUTPUT
|
|
% f value f(x)
|
|
% J jacobian at x
|
|
%
|
|
% funktionswert bestimmen
|
|
f=fun(x);
|
|
|
|
% jakobimatrix mit nullen initialisieren
|
|
J=zeros(size(f,1),size(x,1));
|
|
for i=1:size(f)
|
|
for j=1:size(x)
|
|
% schrittweite dieses parameters zwischenspeichern
|
|
dj = delta(j);
|
|
% d = d(j)*e(j)
|
|
d = zeros(size(delta));
|
|
d(j) = dj;
|
|
% differenz ermitteln
|
|
df = (fun(x+d)-fun(x));
|
|
% relative differenz berechnen
|
|
J(i,j)=df(i)/dj;
|
|
end
|
|
end
|
|
|
|
end % function numdiff
|
|
|