I've been working on a programming problem that makes use of Newton's method to help find the root of an equation. The procedure starts with an initial guess of "c / 2" and c is equal to the nth power of x. The program uses test values consisting of the square root of 2, the cube root of 7 and the cube root of -1. I'm also making use of the iterative formula to help find the root consisting of the new guess of xj + 1 being equal to the current guess of xj minus the power rule function of xj divided by the derivative of xj.
The program makes 100 guesses before quitting and moving on to the next value in the input file. Here is what I have:
#include <stdio.h>
#include <math.h>
double power_rule(double, int); /* Function that calculates the power rule of the equation */
double derivative(double, int); /* Function that calculates the derivative of the equation */
int main(void)
{
/* Declare variables */
FILE *inp; /* The input file */
double curr_guess, /* The current guess value for the root of the equation */
new_guess, /* The new guess value for the root of the equation */
c; /* The current value to be used to determine the initial guess */
int trial_counter, /* Counts the guess trials for determining the root of the equation */
inp_value, /* The input value from the file */
inp_counter = 0, /* Counts the input values from the file */
input_status; /* The status of the file input */
/* Open file */
inp = fopen("root_test_values.dat", "r");
input_status = fscanf(inp, "%d", &inp_value);
/* Guess the root */
while (input_status != EOF)
{
if (inp_value < 0)
{
printf("\n%d is a negative value. Negative values cannot be used for root operations.\n", inp_value);
inp_counter++;
input_status = fscanf(inp, "%d", &inp_value);
}
else
{
if (inp_counter == 0)
c = pow(inp_value, 0.5);
else
c = pow(inp_value, 0.33);
curr_guess = c / 2;
printf("\nStarting guess for root is %.6f", curr_guess);
for (trial_counter = 0; trial_counter < 100; trial_counter++)
{
new_guess = curr_guess - (power_rule(curr_guess, inp_counter) / derivative(curr_guess, inp_counter));
printf("\nNew guess for root is %.6f", new_guess);
if (inp_counter == 0)
{
if (curr_guess == pow(c, 0.5))
{
printf("\nRoot found at %.6f", curr_guess);
break;
}
else if (new_guess == pow(c, 0.5))
{
printf("\nRoot found at %.6f", new_guess);
break;
}
}
else
{
if (curr_guess == pow(c, 0.33))
{
printf("\nRoot found at %.6f", curr_guess);
break;
}
else if (new_guess == pow(c, 0.33))
{
printf("\nRoot found at %.6f", new_guess);
break;
}
}
curr_guess = new_guess;
}
if (inp_counter == 0 && trial_counter == 100)
printf("\nNo root was found for the square root of %d.\n", inp_value);
if (inp_counter != 0 && trial_counter == 100)
printf("\nNo root was found for the cube root of %d.\n", inp_value);
inp_counter++;
input_status = fscanf(inp, "%d", &inp_value);
}
}
/* Close file */
fclose(inp);
return 0;
}
double power_rule(double x, int inp_count)
{
double n; /* The denomination of the fractional power */
if (inp_count == 0)
{
n = 1 / 0.5;
return pow(x, n);
}
else
{
n = 1 / 0.33;
return pow(x, n);
}
}
double derivative(double x, int inp_count)
{
double n, /* The denomination of the fractional power */
exp_sub; /* The subtracted exponent */
if (inp_count == 0)
{
n = 1 / 0.5;
exp_sub = n - 1;
return n * pow(x, exp_sub);
}
else
{
n = 1 / 0.33;
exp_sub = n - 1;
return n * pow(x, exp_sub);
}
}
The problem in my textbook seems to indicate that f(x) = nth root of c but I'm not sure. I am also not sure if Newton's method applies to the square or cube root of negative values. Some help would be appreciated. Thanks. :)


Sign In
Create Account


Back to top









