C PROGRAM NOT C++ Make sure your program reports out the following: - The polyno
ID: 3789031 • Letter: C
Question
C PROGRAM NOT C++
Make sure your program reports out the following:
- The polynomial that the user has entered
- The initial guess that the user has entered
- Whether or not the Newton-Raphson algorithm converged to a solution
- If successful, the root of the polynomial that the algorithm found
- The number of iterations that the Newton-Raphson algorithm required to converge to a solution
Also, add any numerical protections to the algorithm that you find necessary HINT: Divide by zero protection?
2.1 Newton-Raphson Algorithm: Write a program that prompts the user to input the coeflicients co, ci,..., cs of a 5th-order polynomial and an initial value r that serves as a the starting condition for the Newton-Raphson algorithm. The 5th-order polynomial has the form We know that the first derivative of y with respect to r is dar We can use this information to find the roots of the polynomial. The basic idea, in the Newton-Raphson method, is as follows: (a) Given an initial guess I, and polynomial coeflicients c, calculate y (b) Check to see if y is close enough to zero, ie. within some small tolerance close to zero. (i) If so then terminate. Algorithm has converged! (ii) If not, then continue (c) Use the current value of a to calculate y' (d) Create a new guess for r using the update formula r -r- (e) Increment a counter to count the number of algorithm iterations Check to see if the number of iterations has exceeded a predetermined count limit (say 500 (i) If so then terminate. Algorithm has failed! (ii) Ir not, then return to step aExplanation / Answer
C program Newton_Raphson.c
#include<stdio.h>
double mod(double y) // function to find the absolute value of a number
{
if(y<0) return (-1*y);
return y;
}
int main()
{
float C[6],x,y,dy,tol = 0.0000001,error;
int i,iter =0,Max_iter = 500;
printf("Enter the values of coefficients C0,C1,...,C5 ");
for(i=0;i<6;i++) // loop to get the coefficients
{
printf("C%d = ",i);
scanf("%f",&C[i]);
}
printf("Enter the value of initial guess x = ");
scanf("%f",&x);// taking initial guess
y = C[0]+x*(C[1]+x*(C[2]+x*(C[3]+x*(C[4]+x*C[5]))));// evaluating y for initial guess
dy = C[1]+x*(C[2]*2+x*(C[3]*3+x*(C[4]*4+x*C[5]*5)));// evaluating dy for initial guess
while( mod(y)>tol && iter<=Max_iter && dy!=0) //loop for further rectification of root
{
x = x - y/dy;// next x value
y = C[0]+x*(C[1]+x*(C[2]+x*(C[3]+x*(C[4]+x*C[5]))));// evaluating y for new x
dy = C[1]+x*(C[2]*2+x*(C[3]*3+x*(C[4]*4+x*C[5]*5)));// evaluating dy for new x
iter++; // increase iteration
}
if(dy == 0)printf("Division by zeor ");// division by zero occured
if(iter>Max_iter)printf("Method faild to converge ");// method diverges
if(mod(y)<=tol)printf("Root = %f Iterations used = %d ",x,iter);// method converges
}
TESTING the program
$ ./a.out
Enter the values of coefficients C0,C1,...,C5
C0 = -54
C1 = 99
C2 = -60
C3 = 20
C4 = -6
C5 = 1
Enter the value of initial guess x = 5
Root = 3.000000 Iterations used = 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.