Advertisement
Sourav_CSE

Newton_Raphson in C

Mar 26th, 2020
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. // Newton Raphson Method C Program
  2. // Credit: script verse academy
  3.  
  4. #include<stdio.h>
  5. #include<math.h>
  6.  
  7. float f(float);
  8. float derivative(float);
  9.  
  10. int main()
  11. {
  12. float x; // x is first approximation
  13.  
  14. unsigned short i=1,n; // n is the number of iteration
  15.  
  16. printf("First APPROXIMATION: ");
  17. scanf("%f", &x);
  18.  
  19. printf("ITERATIONS: ");
  20. scanf("%hu", &n);
  21.  
  22. while(i<=n)
  23. {
  24. x = x - f(x)/derivative(x);
  25. i++;
  26. }
  27.  
  28. printf("APPROXIMATE ROOT: %f \n",x);
  29. return 0;
  30. }
  31. float f(float x) // f(x)
  32. {
  33. return pow(x,2)-5;
  34. }
  35. float derivative(float x) // f'(x)
  36. {
  37. return 2*x;
  38. }
  39. /* Input
  40. First APPROXIMATION: 2
  41. ITERATIONS: 5
  42. Output
  43. APPROXIMATE ROOT: 2.236068
  44. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement