Problems with functions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems with functions

mat_hunt
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problems with functions

tmacchant
----- 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.html


Tatsuro

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problems with functions

Colin Macdonald-2
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
Reply | Threaded
Open this post in threaded view
|

Re: Problems with functions

Sergei Steshenko
In reply to this post by mat_hunt





>________________________________
> From: mat_hunt <[hidden email]>
>To: [hidden email]
>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.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problems with functions

mat_hunt
In reply to this post by mat_hunt
Hi,

I realise my problem. I had to create a function handle for my function in the form of h=@f, then the routine works perfectly fine.

This is going to be part of a bigger program which I am going to solve a weakly nonlinear equation called the Korteweg-deVries (KdV) equation. I then want to generalise the equations I solve using this technique.

Mat