Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Newton Raphson Method C Program
- // Credit: script verse academy
- #include<stdio.h>
- #include<math.h>
- float f(float);
- float derivative(float);
- int main()
- {
- float x; // x is first approximation
- unsigned short i=1,n; // n is the number of iteration
- printf("First APPROXIMATION: ");
- scanf("%f", &x);
- printf("ITERATIONS: ");
- scanf("%hu", &n);
- while(i<=n)
- {
- x = x - f(x)/derivative(x);
- i++;
- }
- printf("APPROXIMATE ROOT: %f \n",x);
- return 0;
- }
- float f(float x) // f(x)
- {
- return pow(x,2)-5;
- }
- float derivative(float x) // f'(x)
- {
- return 2*x;
- }
- /* Input
- First APPROXIMATION: 2
- ITERATIONS: 5
- Output
- APPROXIMATE ROOT: 2.236068
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement