Sunteți pe pagina 1din 3

/* NEWTON'S METHOD

-- finds solutions of a pol


ynomial --
All that a user has to do is inp
ut the polynomial's
degree and coefficients and the p
rogram will find all
real solutions to the hundredths p
lace and display them.
Pseudo-code:
1: Prompt for degree of polynomial
2: Store degree of polynomial
3: Prompt for (degree+1) coefficients
4: Store coefficients
5: Prints users polynomial equation to make sure it is what they wan
t
6: Setup an initial guess as guess = 10 and use Newton's method to c
onverge on that guess
Newton's Method
1: Set initial guess
2: if guess > epsilon
get new guess Xn+1 = Xn - f(x)/f'(x)
3: Check convergence
|Xn+1 - Xn| < epsilon
4: If not, set Xn+1 to Xn and find a new Xn+1 and rechec
k.
If it doesn't converge after so many tries make a new
initial guess.
5: If yes, store the root
7: Calculate reduced polynomial using synthetic division
8: Continue Newton's method until all solutions are found
9: Display all solutions
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float f(int NumCoeff, float polyCoeff, float Guess_n);
int Newton(int N, float polyCoeff[], float realZero[], int NumCoeff);
int synthetic_division(int N, float polyCoeff[], float compare, int NumCoeff, in
t p);
int main () //Steps 1-4, and 9
{
printf("\t\t\t\tNewton's Method");
int N = 0,
choice,
i,
NumCoeff;
float polyCoeff[50],
realZero[N];
while(1)
{
printf("\n\nWhat is the degree of your polynomial?\n");
scanf("%d",&N);
NumCoeff = N+1;
printf("\nEnter (%d) coefficients, separated by the enter key.\n",(N+1)
);
for (int i=N+1; i>0; i--)
scanf("%d", &polyCoeff[i]);
for (i=0; i<N; i++)
deriv[i] = NumCoeff[i]*(float)(N-i);
// Unnecessary, made for fun as well as to check my work
printf("Is this the equation you wish to use? (y/n):\n");
for (i=N; i>= 0; i--)
{
printf("%i", polyCoeff[i+1]);
printf("*");
printf("X");
printf("^");
printf("%i",i);
if(i>0) printf("+");
else printf("\n");
}
choice = getchar();
if (choice == 'y' || 'Y')
break;
else
continue;
}
Newton(N, polyCoeff[50], realZero[N], NumCoeff);
end:
printf("\nSolutions are:");
for(i=0;i<solution_count;++i)
printf(" %.2f",solution[i]);
printf("\n\n");
system("PAUSE");
return 0;
}
int Newton(int N, float polyCoeff[], float realZero[N], int NumCoeff) // Steps 5
- 6, 8
{
float Guess_n = 10,
epsilon = .00001,
compare = 10000,
Guess_n2,
notzero;
int p,
fail_count = 0,
solution_count = 0;
p=N;
while(p>0)
{
while(fabs(compare)>epsilon)
{
notzero=f(N,deriv,Guess_n);
while(notzero<pow(double 10,double -20))
{
//printf("Error, the evaluation of the derivative was zero. R
etrying with new guess");
++Guess_n;
}
Guess_n2=Guess_n-(f(NumCoeff,polyCoeff,Guess_n)/f(N,deriv,Guess_n)
);
if (fabs(Guess_n2-Guess_n) > compare)
{
Guess_n = Guess_n + 5;
compare = 10000;
++fail_count;
}
else
{
compare = fabs(Guess_n2-Guess_n);
Guess_n = Guess_n2;
}
if (fail_count >= 10) goto end;
}
realZero[p] = compare;
++solution_count;
synthetic_division(N, polyCoeff, compare, NumCoeff);
p--;
}
}
int synthetic_division(int N, float polyCoeff[], float compare, int NumCoeff) //
Step 7 // Needs so much help. Copied equation from Bretts'. No idea whats happe
ning here.
{
for(i=1;i<=NumCoeff-1;++i)
polyCoeff[i]=polyCoeff[i]*compare+polyCoeff[i+1];
}
float f(int NumCoeff, float polyCoeff, float Guess_n) //Need help // evaluates t
he polynomial expression
{
long i;
float = evaluate (NumCoeff, polyCoeff, Guess_n);
float answer = polyCoeff[NumCoeff-1];
for(i=0; i<NumCoeff-1; i++)
answer = answer + polyCoeff[i]*(float(pow(Guess_n,NumCoeff-i-1)));
return (answer);
}

S-ar putea să vă placă și