# Problems with functions

5 messages
Open this post in threaded view
|

## Problems with functions

 I have defined a function: function  y=f(x) y_1=x(1)-x(2)+x(3)-2; y_2=2*x(1)-x(2)+x(3)-3; y_3=2*x(1)+x(2)-x(3)-1; y=[y_1 y_2 y_3]; endfunction and I have written a function to compute the Jacobian of that function: function J=jacobian(func,x) % computes the Jacobian of a function n=length(x); fx=feval(func,x); eps=1.e-8; % could be made better xperturb=x; for i=1:n xperturb(i)=xperturb(i)+eps; J(:,i)=(feval(func,xperturb)-fx)/eps; xperturb(i)=x(i); endfor; endfunction when I use the command: jacobian(f,[1 1 1]) I get the error: error: 'x' undefined near line 2 column 5 error: called from: error:   /home/mat/Matlab programs/f.m at line 2, column 4 error: evaluating argument list element number 1 What is going on? The code was working perfectly fine 10 minutes ago and now it's throwing up errors.
Open this post in threaded view
|

## Re: Problems with functions

 ----- Original Message ----- > From: mat_hunt  > To: help-octave> Cc: > Date: 2016/7/20, Wed 05:37 > Subject: Problems with functions > > I have defined a function: > > function  y=f(x) > y_1=x(1)-x(2)+x(3)-2; > y_2=2*x(1)-x(2)+x(3)-3; > y_3=2*x(1)+x(2)-x(3)-1; > y=[y_1 y_2 y_3]; > endfunction > > and I have written a function to compute the Jacobian of that function: > > function J=jacobian(func,x) > % computes the Jacobian of a function > n=length(x); > fx=feval(func,x); > eps=1.e-8; % could be made better > xperturb=x; > for i=1:n > xperturb(i)=xperturb(i)+eps; > J(:,i)=(feval(func,xperturb)-fx)/eps; > xperturb(i)=x(i); > endfor; > endfunction > > when I use the command: > > jacobian(f,[1 1 1]) > > I get the error: > > error: 'x' undefined near line 2 column 5 > error: called from: > error:   /home/mat/Matlab programs/f.m at line 2, column 4 > error: evaluating argument list element number 1 > > What is going on? The code was working perfectly fine 10 minutes ago and now > it's throwing up errors. > This is not an answer but a comment. You can calculate numerical jacobinan function "jacobs" in the optim package. http://octave.sourceforge.net/optim/function/jacobs.htmlTatsuro _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Problems with functions

 In reply to this post by mat_hunt On 19/07/16 13:37, mat_hunt wrote: > jacobian(f,[1 1 1]) Try: jacobian(@f, [1 1 1]) best, Colin _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave