user2606257
user2606257

Reputation: 65

Jacobian function returning error "Undefined function 'jacobian' for input arguments of type 'double'"

I'm trying to use a Jacobian to solve an equation using the Newton Raphson method but I keep getting a type 'double' error. Symbolic is installed as well. I am wondering if I am using F correctly here. Do I have to use the Jacobian separately for F(1) and F(2)? Here is the script:

X=[0.75 0.25]; %N-dimensional array, first guess then solution
Y=[0 0];

F(1)=(X(1)^2)+(X(2)^2)-1; %right hand side functions
F(2)=X(1)+X(2)-1; %right hand side functions

MAXIT=10;
ITEST=1;
counter=0;
ABSER=0.00001;
RELER=.1;

DFDX=jacobian(F,X);

[X,ITEST,counter] =NLNR(X,F,MAXIT,counter,ABSER,RELER,ITEST,DFDX);


fprintf('answer for X1 is %d  and X2 is %d and ITEST is %d.\n',X(1),X(2),ITEST);
fprintf('number of interations is %d.\n',counter);

and this is the function:

function [X,ITEST,counter] =NLNR(X,F,MAXIT,counter,ABSER,RELER,ITEST)

while ITEST==1  %run loop as long as ITEST is 1
    counter=counter+1;  %use counter to keep track of iterations


    dX=DFDX/(-F);

    X=X+dX;


        if abs(Y(1)-X(1))<ABSER %check convergence
            ITEST=3;
        end

        if abs((Y(1)-X(1))/X(1))<RELER %check convergence
            ITEST=3;
        end

        if counter>MAXIT %check divergence
            ITEST=2;
        end

    Y(1)=X(1); %set Y to check diff in next loop
    Y(2)=X(2);        


end

end

Upvotes: 0

Views: 2148

Answers (2)

horchler
horchler

Reputation: 18484

It looks like you can easily convert your incorrect use of the symbolic jacobian function to use symbolic math:

X = [0.75 0.25];   
x = sym('x',[1 2]);
F = [x(1)^2+x(2)^2-1;
     x(1)+x(2)-1];

DFDX = jacobian(F,x)
DFDX_sub = subs(DFDX,x,X)

which returns

DFDX =

[ 2*x1, 2*x2]
[    1,    1]


DFDX_sub =

[ 3/2, 1/2]
[   1,   1]

Then you can use double to convert DFDX_sub to a floating point array. Note that the first argument to jacobian can also be a handle to a function that returns a vector (as opposed to a symbolic function or expression):

X = [0.75 0.25];   
x = sym('x',[1 2]);
F = @(x)[x(1)^2+x(2)^2-1;
         x(1)+x(2)-1];

DFDX = jacobian(F,x)
DFDX_sub = subs(DFDX,x,X)

Upvotes: 3

Daniel
Daniel

Reputation: 36710

There is no function called jacobian present in your matlab installation. If you read the documentation you will notice that both functions with that name are part of a toolbox:

http://www.mathworks.com/help/symbolic/jacobian.html http://www.mathworks.com/help/mbc/mbccommandline/jacobian.html

Probably you don't have these toolboxes installed or licensed.

Best solution for you would probably be to search at matlab file exchange for an implementation which matches your requirements.

Upvotes: 1

Related Questions