Advertisement
Guest User

Newton

a guest
Apr 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 0.76 KB | None | 0 0
  1. x = -2;
  2. Tol = 0.0000001;
  3. count = 0;
  4. dx=1;   %this is a fake value so that the while loop will execute
  5. f=-13;    % because f(-2)=-13
  6. fprintf('step      x           dx           f(x)\n')
  7. fprintf('----  -----------  ---------    ----------\n')
  8. fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
  9. xVec=x;fVec=f;
  10. while (dx > Tol || abs(f)>Tol)  %note that dx and f need to be defined for this statement to proceed
  11.     count = count + 1;
  12.     fprime = 3*x^2 + 3;
  13.     xnew = x - (f/fprime);   % compute the new value of x
  14.     dx=abs(x-xnew);          % compute how much x has changed since last step
  15.     x = xnew;
  16.     f = x^3 + 3*x + 1;       % compute the new value of f(x)
  17.     fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
  18. end
  19.  
  20. % This produces the following output:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement