Question

C PROGRAM NOT C++

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 a

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?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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\n");
   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\n");// division by zero occured
   if(iter>Max_iter)printf("Method faild to converge\n");// method diverges
   if(mod(y)<=tol)printf("Root = %f\tIterations used = %d\n",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

Add a comment
Know the answer?
Add Answer to:
C PROGRAM NOT C++ Make sure your program reports out the following: - The polynomial that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • we are currently using C++ through reply.it thanks for your help. 9. If we define f(r)=...

    we are currently using C++ through reply.it thanks for your help. 9. If we define f(r)= r"-c, then solving f(x) = 0 is equivalent to finding e/n for n > 0. Given an approximate value of r, the Newton-Raphson method consists of calculating a approximation rnew, by using new f(x) Inew r In our case, this reduces to: (n-1)r c Inew= nrn-1 I 2 Write & program to find cn for positive n and c. The program should prompt for...

  • II. Using Newton’s method, write a MATLAB program to find the fixed point of the following...

    II. Using Newton’s method, write a MATLAB program to find the fixed point of the following function: ?(?) = √? + ?? accurate to at least 8 decimal places. (HINT: finding the fixed point of f(x) is the same as finding the zero of g(x) = f(x) − x. ) The output of this program should display in a single table (i) the solution for the fixed point, (ii) the initial guess, (iii) the number of iterations it took to...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • PYTHON Write a program to enter a valid variable till the user wants to end. The...

    PYTHON Write a program to enter a valid variable till the user wants to end. The program should display the count of positive, negative, zeros and letters (upper and lower case)/symbols (error handling) entered. Based on the problem, you need to design an algorithm as follows: 1. Get the user input until “n” is entered 2. Add to the positive if it is greater than zero 3. Add to the negative if it is less than zero 4. Add to...

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • _28. Using the following function prototype which statement about the argument passed to parameter Als true....

    _28. Using the following function prototype which statement about the argument passed to parameter Als true. void F(const int A[], int Cnt): A. The argument is modified when changes are made to parameter A in function F. B. The argument passed to parameter A must always have the same number of elements, every time function Fis invoked. C. Changes can not be made to parameter A in function F. D. Every element of the argument passed to parameter A must...

  • The program has to be written in C The expected output: Please make sure to use...

    The program has to be written in C The expected output: Please make sure to use two-dimensional arrays. OPTIONAL TAKE-HOME QUIZ WORTH 10 POINTS OUT OF A GRAND TOTAL OF 110 FOR THE COURSE A DC electrical circuit consists of an ideal battery with user-suppled terminal voltage VB which drives a resistive load comprised of a user-supplied number m of branches connected in parallel across each other, with each branch consisting of a variable user-supplied number n of resistors and...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions...

    I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT