MATLAB CODE NEWTON METHOD
    조회 수: 26 (최근 30일)
  
       이전 댓글 표시
    
f = @(x) exp(x)-3*x;
fp = @(x) exp(x)-3;
x0 = 1; 
N = 10; 
tol = 1E-10;
x(1) = x0; % Set initial guess
n = 2; 
nfinal = N + 1; 
while (n <= N + 1)
  fe = f(x(n - 1));
  fpe = fp(x(n - 1));
  x(n) = x(n - 1) - fe/fpe;
  if (abs(fe) <= tol)
    nfinal = n; 
    break;
  end
  n = n + 1;
end
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Solution:')
xlabel('Iterations')
ylabel('X')
I have a few questions about my code. What is happening with variable n during execution? And how could I make my code printing an iteration table? I can see iterations in the graphic but I need to print a table also.
댓글 수: 0
답변 (5개)
  Geoff Hayes
      
      
 2018년 3월 5일
        Adomas - your code is using n as an index into x. On each iteration of the loop, you increment n by one in preparation for the next iteration. n will be the length of your array x and so will tell you how many iterations have occurred until the tolerance has been satisfied (or until the maximum N has been reached).
As for displaying the data in a table, do you mean your x? Something like
 table([1:length(x)]',x']
I'm using the ' so that the data is transposed (into a column).
댓글 수: 0
  Jan
      
      
 2018년 3월 5일
         What is happening with variable n during execution?
It is increased by 1 in each iteration. It counts the number of iterations.
 how could I make my code printing an iteration table?
Insert a command to display the current values:
...
x(n) = x(n - 1) - fe/fpe;
fprintf('%3d: %20g %20g\n', n, x(n), abs(fe));
if (abs(fe) <= tol)
  ...
댓글 수: 0
  Mohamed Elsayed Abo Heiba
 2019년 10월 18일
        f = @(x) exp(x)-3*x;
fp = @(x) exp(x)-3;
x0 = 1; 
N = 10; 
tol = 1E-10;
x(1) = x0; % Set initial guess
n = 2; 
nfinal = N + 1; 
while (n <= N + 1)
  fe = f(x(n - 1));
  fpe = fp(x(n - 1));
  x(n) = x(n - 1) - fe/fpe;
  if (abs(fe) <= tol)
    nfinal = n; 
    break;
  end
  n = n + 1;
end
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Solution:')
xlabel('Iterations')
ylabel('X')
댓글 수: 0
  Hamza saeed khan
 2020년 11월 24일
        f = @(x) exp(x)-3*x;
fp = @(x) exp(x)-3;
x0 = 1; 
N = 10; 
tol = 1E-10;
x(1) = x0; % Set initial guess
n = 2; 
nfinal = N + 1; 
while (n <= N + 1)
  fe = f(x(n - 1));
  fpe = fp(x(n - 1));
  x(n) = x(n - 1) - fe/fpe;
  if (abs(fe) <= tol)
    nfinal = n; 
    break;
  end
  n = n + 1;
end
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Solution:')
xlabel('Iterations')
ylabel('X')
table([1:length(x)]',x']
x(n) = x(n - 1) - fe/fpe;
fprintf('%3d: %20g %20g\n', n, x(n), abs(fe));
if (abs(fe) <= tol)
댓글 수: 0
  Aristi Christoforou
 2021년 4월 10일
        function[root]=newtonmethod(f,df,xo,tol,n)
xn=xo;
for k=1:n
    xn1=xn-f(xn)/df(xn);
    dx=abs(xn1-xn);
    xn=xn1;
    if dx<tol
        dis("newton method has converged")
        root=xn;
        return
    end
end
disp("no convergence after n interactions")
댓글 수: 0
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!