Newton’s method for nonlinear systems
    8 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Rita Sciuto
 le 18 Déc 2020
  
    
    
    
    
    Commenté : Walter Roberson
      
      
 le 19 Déc 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)
0 commentaires
Réponse acceptée
  Walter Roberson
      
      
 le 18 Déc 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 commentaires
  Walter Roberson
      
      
 le 19 Déc 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); 
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Matrix Computations dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!