Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function NewtonRaphsonMethod
- i = 1;
- p0 = initial value; %initial conditions
- N = 100; %maximum number of iterations
- error = 0.0001; %precision required
- syms 'x'
- f(x) = Colebrook function %function we are solving
- df = diff(f); %differential of f(x)
- while i <= N
- p = p0 - (f(p0)/df(p0)); %Newton-Raphson method
- if (abs(p - p0)/abs(p)) < error %stopping criterion when difference between iterations is below tolerance
- fprintf('Solution is %f \n', double(p))
- return
- end
- i = i + 1;
- p0 = p; %update p0
- end
- fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', N, error) %error for non-convergence within N iterations
- end
Advertisement
Add Comment
Please, Sign In to add comment