Reputation: 171
I'm trying to implement Levenberg–Marquardt algorithm in Matlab. As one step of that I need to calculate Jacobian of a function at given point (x0,y0,z0).
My function for evaluation of Jacobian looks like this:
function [J]=GetJacobian(func,x)
% computes the Jacobian of a function
n=length(x);
fx=feval(func,x);
eps=1.e-8;
xperturb=x;
for i=1:n
xperturb(i)=xperturb(i)+eps;
J(:,i)=(feval(func,xperturb)-fx)/eps;
xperturb(i)=x(i);
end;
3D function that I want to minimize is given like this:
function [ y ] = QuadraticF( x )
% evaluates simple quadratic function at given X
% X is a 3x1 vector of coordinates
A = ...
[ 1 1 1
1 2 1
1 1 3];
b = [ 1; 5; 1 ];
c = 1;
y = 0.5 .* x' * A * x - b' * x + c;
end
It's simple quadratic function with arbitrary parameters.
When I try to evaluate Jacobian at (0,0,0) using this command:
GetJacobian(@QuadraticF, [ 0;0;0 ])
I get this answer:
ans =
-1.0000 -5.0000 -1.0000
Shouldn't Jacobian be given by 3x3 matrix if my function has 3 parameters? Is my problem of syntactic (coding) or conceptual nature?
Upvotes: 0
Views: 321
Reputation: 357
Your problem is conceptual. The function QuadraticF takes three real numbers and produces a single real number. The derivatives of that one function wrt. the three input variables make up the Jacobian. In order for the Jacobian to be a 3x3 matrix, you'd need a map that returns three numbers (f1,f2,f3) (a 3-tuple / a vector in R^3)
I suggest you read up a bit on multivariate calculus(http://en.wikipedia.org/wiki/Multivariable_calculus). Pick up an introductory textbook. Maybe one for physicists or engineers, if you find full-blown mathematical texts too hard.
Upvotes: 1