필터 지우기
필터 지우기

Newton’s method for nonlinear systems

조회 수: 1 (최근 30일)
Rita Sciuto
Rita Sciuto 2020년 12월 18일
댓글: Walter Roberson 2020년 12월 19일
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)

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 18일
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
Rita Sciuto
Rita Sciuto 2020년 12월 18일
Thank you for your answer. Then, do you think I can find another code fot the same method?
Walter Roberson
Walter Roberson 2020년 12월 19일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by