# Need help with matlab code for newton-raphson (need help really bad)

1. Jul 1, 2012

### fizzkilla

The code runs but I get the wrong answers can you take a look at the code for this .

******************
x(1) = 1.035
x(2) = 1.086
x(3) = 0.927
******************

clc
clear

F = @(x) [15*x(1)+(x(2)^2)-4*x(3)-13;...
(x(1)^2)+10*x(2)^2-x(3)-11;...
x(2)^2-25*x(3)+22];
J = @(x) [15+2*x(2)-4;2*x(1)+10-1;0+2*x(2)-25];

xo = [1 1 1];
n = 4;

es = .5*10^(2-n);

[xn,cnt] = newton_raphson(es,xo,J,F);

fprintf('******************\n')
for i = 1:length(xo)
fprintf(' x(%d) = %.3f\n',i',xn(i)')
end
fprintf('******************\n')
fprintf('It took %d iterations to converge!\n',cnt')

FUNCTION PROGRAM

function [xn, cnt] = newton_raphson(es,xo,J,F)

cnt = 0;
ea = 1;

while ea > es
delx = J(xo)\-F(xo);
xn = xo + delx;

ea = 100*max(abs((xn - xo) ./ xn));
xo = xn;
cnt = cnt + 1;

end

2. Jul 1, 2012

### AlephZero

J should be a 3 x 3 matrix of derivatives with $J_{ij} = \partial F_i / \partial x_j$, not a vector.

3. Jul 1, 2012

### fizzkilla

Ok then after now I get answers like inf inf and nan