I am creating a MATLAB code to estimate the Jacobian matrix using forward differences. Here is the code:
%Calculate Jacobian Numerically using forward differences:
function J=Jacobianest(x,F) % X is a column vector of variables, f is a column function vector
h=1e-7;
n=length(x);
J=zeros(n);
for i=1:n
xp=x;
xp(i)=x(i)+h;
J(:,i)=1/h*(F(xp)-F(x));
end
When I run it the following error appears:
Jacobianest(x,'multivariable_newton_fun') Subscript indices must either be real positive integers or logicals. Error in Jacobianest (line 9) J(:,i)=1/h*(F(xp)-F(x));
I looked at other questions for answers and tried debugging/dbstop
, but I can't seem to find anything obvious.
I am using i
as the column index which should just have integer values from 1
to n
(all integers hopefully).