0
votes

Well, I got this Newton's Method for multiple variables code that was modified by me to function with 4 variables instead of 2, but I keep getting the error

Syntax error, unexpected ;

referencing line 9 which is the Jacobian matrix from the partial derivatives of the equations on function F. Every " " is another column and every ";" is another line.

clear;clc
function z=F(p1,p2,p3,p4) 
    z(1)=0.3*(500-p1)^0.5-0.2*(p1-p2)^0.5+0.2*(p1-p3)^0.5
    z(2)=0.2*(p1-p2)^0.5-0.1*(p2-p4)^0.5+0.2*(p2-p3)^0.5
    z(3)=0.1*(p1-p3)^0.5-0.2*(p3-p2)^0.5+0.1*(p3-p4)^0.5
    z(4)=0.1*(p2-p4)^0.5+0.1*(p3-p4)^0.5-0.2*(p4)^0.5
endfunction
function z=J(p1,p2,p3,p4) //Jacobiano
    z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p3)) 0;0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3) -0.2/(2*sqrt(p2-p3)) -0.1/(2*sqrt(p2-p4));0.1/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p3-p2)) -0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)) -0.1/(2*sqrt(p3-p4));0 0.1/(2*sqrt(p2-p4)) 0.1/(2*sqrt(p3-p4)) -0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction

The code doesn't end here, but the error is certainly in the format I used in the 4x4 Jacobian matrix or in the function itself, I just don't get why is wrong.

Appreciate any help.

1

1 Answers

1
votes

You forgot to close a parenthesis in column 191 of line 9, and you also forgot an operator on column 319.

If you're creating a matrix in which the elements are results of operations, you'd better use commas instead of blank spaces to separate them. You should also be using ... to break the matrix definition in more than one line to improve readability. Your Jacobian function would be a lot easier to debug if it were written like this:

function z=J(p1,p2,p3,p4) //Jacobiano
    z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)),...
        0.2/(2*sqrt(p1-p2)),...
       -0.2/(2*sqrt(p1-p3)),...
        0;...
        ...
        0.2/(2*sqrt(p1-p2)),...
       -0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3),...
       -0.2/(2*sqrt(p2-p3)),...
       -0.1/(2*sqrt(p2-p4));...
        ...
        0.1/(2*sqrt(p1-p3)),...
        0.2/(2*sqrt(p3-p2)),...
       -0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)),...
       -0.1/(2*sqrt(p3-p4));...
        ...
        0,...
        0.1/(2*sqrt(p2-p4)),...
        0.1/(2*sqrt(p3-p4)),...
       -0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction

In this style, your mistakes can be easily spotted: notice the missing ) in line 8, and the missing / in line 14.