help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problems with functions


From: Sergei Steshenko
Subject: Re: Problems with functions
Date: Wed, 20 Jul 2016 13:51:51 +0000 (UTC)





>________________________________
> From: mat_hunt <address@hidden>
>To: address@hidden 
>Sent: Tuesday, July 19, 2016 11:37 PM
>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.
>
>
>
>--
>View this message in context: 
>http://octave.1599824.n4.nabble.com/Problems-with-functions-tp4678670.html
>Sent from the Octave - General mailing list archive at Nabble.com.
>
>_______________________________________________




Your 'jacobian' function uses 'feval', and 'help feval' gives as an example

feval ("acos", -1)
=> 3.1416

, so jacobian("f",[1 1 1]) instead of jacobian(f,[1 1 1]) that you have might 
be better.

I.e. the rule of thumb I remember is that you either have a function handle or 
a function name as quoted string.


--Sergei.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]