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

In summary, The code runs but produces incorrect answers. The user is asking for someone to review the code. After running the code, the output for x(1), x(2), and x(3) are 1.035, 1.086, and 0.927, respectively. The code uses a Newton-Raphson method with an error tolerance of 0.5%. The program also includes a function for the Jacobian matrix and a function to calculate the root. However, the user notes that there may be an issue with the Jacobian matrix not being the correct size.
  • #1
fizzkilla
4
0
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
 
Physics news on Phys.org
  • #2
J should be a 3 x 3 matrix of derivatives with ##J_{ij} = \partial F_i / \partial x_j##, not a vector.
 
  • #3
Ok then after now I get answers like inf inf and nan
 

1. What is the Newton-Raphson method?

The Newton-Raphson method is an iterative algorithm used to find the roots of a given function. It is commonly used to solve systems of nonlinear equations and optimization problems.

2. How does the Newton-Raphson method work?

The method involves making an initial guess for the root of the function, then using the derivative of the function to calculate a new guess for the root. This process is repeated until the root is found within a desired level of accuracy.

3. What are the advantages of using the Newton-Raphson method?

One advantage is that it is a fast and efficient method for finding roots of a function. It also has a quadratic rate of convergence, meaning that the number of correct digits in the approximation roughly doubles with each iteration.

4. What are some challenges when implementing the Newton-Raphson method in Matlab?

One challenge is choosing a suitable initial guess for the root. If the initial guess is too far from the actual root, the method may not converge. Another challenge is dealing with functions that have multiple roots or have steep curves, which can lead to slow convergence or even divergence.

5. How can I improve the performance of my Newton-Raphson code in Matlab?

There are a few ways to improve the performance of the method in Matlab. One is to use vectorization, which allows for faster computation by performing operations on entire arrays instead of individual elements. Another is to use appropriate stopping criteria to prevent unnecessary iterations. Additionally, selecting an appropriate step size can also improve the convergence rate of the method.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top