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

  • MATLAB
  • Thread starter fizzkilla
  • Start date
  • #1
4
0

Main Question or Discussion Point

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

ANSWERS
******************
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
 

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
6,994
291
J should be a 3 x 3 matrix of derivatives with ##J_{ij} = \partial F_i / \partial x_j##, not a vector.
 
  • #3
4
0
Ok then after now I get answers like inf inf and nan
 

Related Threads on Need help with matlab code for newton-raphson (need help really bad)

  • Last Post
Replies
1
Views
3K
Replies
29
Views
3K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
4K
Top