0

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

3
  • I suspect you managed to name a variable jacobian. Check that you didn't do that, run clear and try again Commented Jan 19, 2016 at 18:02
  • 1
    @Amir, please don't tag jacobian-matrix in this question... for the love of God please. Commented Jan 19, 2016 at 18:04
  • 1
    Mhh I'm afraid the function jacobian requires the Symbolic Math Toolbox Commented Jan 19, 2016 at 18:06

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

I don't think that's the problem, I am using a school computer and it says I have symbolic installed:Symbolic Math Toolbox Version 6.1 (R2014b)
@user2606257: Okay, I did not expect that error message when the symbolic toolbox is present. You have a function jacobian but it expects input arguments of type sym, not double. It analytically solves it, I do not know how to adapt it to your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.