Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Dasproc C - 2019
- William Handi Wijaya
- 0087
- Menghitung estimasi nilai akar menggunakan metode newton
- */
- #include <stdio.h>
- #include <math.h>
- // function prototypes
- double next_approx(double, double, double);
- double func(double, double, double);
- double derivative(double, double);
- int main()
- {
- //variable declarations
- double x0, // variable to calculate
- n, // input - power of x
- c, // const of a polinomial
- x1, // Newton's approximation
- i, // loop variable
- temp; // Flag for old approx
- //get the value of n and c
- printf("\nEnter c - ");
- scanf("%lf", &c);
- printf("\nEnter n - ");
- scanf("%lf", &n);
- //calculate and display the results
- x0 = c / 2;
- x1 = x0 - (func(x0, n, c) / derivative(x0, n));
- //program loop
- for(i = 0; i < 100; i++)
- {
- //printing the result every i
- printf("\n%.6f \n", x1);
- x1 = next_approx(x1, n, c);
- }
- }
- //function to repeat Newton's method
- double next_approx(double temp, double n, double c)
- {
- double next_approx = temp - (func(temp, n, c) / derivative(temp, n));
- return next_approx;
- }
- //function to get the value of f(x)=x^n - c
- double func(double x, double n, double c)
- {
- return ((pow(x, n)) - c);
- }
- //function to get the value of f(x) derivative = n * x^(n-1)
- double derivative(double x, double n)
- {
- return (n * pow(x, n - 1));
- }
Advertisement
Add Comment
Please, Sign In to add comment