R0qUuYdE2L9B0P

Newton Raphson (code from slides)

Dec 15th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. function NewtonRaphsonMethod
  2. i = 1;
  3. p0 = initial value; %initial conditions
  4. N = 100; %maximum number of iterations
  5. error = 0.0001; %precision required
  6. syms 'x'
  7. f(x) = Colebrook function %function we are solving
  8. df = diff(f); %differential of f(x)
  9.  
  10. while i <= N
  11. p = p0 - (f(p0)/df(p0)); %Newton-Raphson method
  12. if (abs(p - p0)/abs(p)) < error %stopping criterion when difference between iterations is below tolerance
  13. fprintf('Solution is %f \n', double(p))
  14. return
  15. end
  16. i = i + 1;
  17. p0 = p; %update p0
  18. end
  19.  
  20. fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', N, error) %error for non-convergence within N iterations
  21. end
Advertisement
Add Comment
Please, Sign In to add comment