Newton’s method for nonlinear systems

6 visualizaciones (últimos 30 días)
Rita Sciuto
Rita Sciuto el 18 de Dic. de 2020
Comentada: Walter Roberson el 19 de Dic. de 2020
Hi, I'm trying to solve an nonlinear system with Newton's method.
I was trying to do this by my-self but since my code doens't work I looked around for a code and I found this one.
I'm new in matlab and maybe understanding this code is easier I can imagine, but actually I can't get what is written here:
function [x, nit] = newtonsys(F, J, x0, toll, nmax, p)
[n,m]=size(F);
nit=0;
Fxn=zeros(n,1);
x=x0;
err=toll+1;
for i=1:n, for j=1:n, Jxn(i,j)=eval(J((i-1)*n+j,:)); end; end
[L,U,P]=lu(Jxn); step=0;
while err>toll
if step == p
step = 0;
for i=1:n;
Fxn(i)=eval(F(i,:));
for j=1:n; Jxn(i,j)=eval(J((i-1)*n+j,:)); end
end
[L,U,P]=lu(Jxn);
else
for i=1:n, Fxn(i)=eval(F(i,:)); end
end
nit=nit+1; step=step+1; Fxn=-P*Fxn; y=forwardcol(L,Fxn);deltax=backwardcol(U,y);x=x+deltax; err=norm(deltax);
if nit>nmax
disp(Fails to converge within maximum number of iterations );
break
end
end
Doesn someone understand the idea in background? Can someone explain me what the code does? I neither understand why it uses "eval" function (I checked on matlab documentation but I can't understand this function here).
Thank you in advance
source: Numerical Mathematics (Alfio Quarteroni, Riccardo Sacco, Fausto Saleri)

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Dic. de 2020
That code expects that F will be a character array, with each row holding a blank-padded expression written in terms of the (scalar) variable x.
If you let the number of rows in F be called n, then J is expected to a character array with (n^2) rows, each row holding a blank-padded expression written in terms of the (scalar) variable x.
The rows in J are arranged in groups of n (number of rows in F) with successive rows in a group becoming a column -- so the third row of the second group of n rows in J would be associated with the third column of the second row of a matrix being built up.
I do not know why the code was programmed the way it is. I speculate that the code might have been designed before anonymous functions were added to MATLAB.
  2 comentarios
Rita Sciuto
Rita Sciuto el 18 de Dic. de 2020
Thank you for your answer. Then, do you think I can find another code fot the same method?
Walter Roberson
Walter Roberson el 19 de Dic. de 2020
I suggest that you convert the code to expect F to be the handle to a function that accepts one parameter, and returns a column vector of values; and that you convert J to be the handle to a function that accepts one parameter and returns a square matrix of values. And that you replace
for i=1:n, for j=1:n, Jxn(i,j)=eval(J((i-1)*n+j,:)); end; end
with
Jxn = J(x);
and likewise instead of calculating Fxn in a loop that you just Fxn = F(x);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by