Rename the function newton_raphson() provided in the code snippet below to secant () and modify it to match the iteration formula of the secant method. Recall that the secant method needs two initial points, which we will pass as the arguments x and x1. The function's signature should be: int secant (double *xi, double x0, double x1, int Nmax, double TOL), where Nmax is the maximum allowed number of iterations and TOL is the tolerance for locating the root. Hint: You will also need to define a second internal variable xp and initialise its value to x1. In each iteration, xp will be updated to the old value of x, and x will be updated to the new point. As with newton_raphson(), the variable xi is passed by reference to secant (), expecting it to fill in the value of the root. This part can be left untouched. Again, the function should return 0 if it finishes successfully and -1 if the maximum number of iterations is exceeded. The stopping condition can also remain the same and there is no need to implement further sanity checks. Do not add any other print statements. Answer: (penalty regime: 10, 20, ... %) Reset answer 1 int newton_raphson(double *xi, double x, int Nmax, double TOL) { 10 11 12 13 14 15 16 17-> /* Initialize Algorithm */ double x, fx, fpx, dx; int i; X = XÔ; fx = f(x0); fp(x0); fpx = dx = -fx/fpx; /* Run Iteration Loop */ i=0; while (!((fabs(dx) < TOL) || (i>Nmax))) { 234567890123+ 25 } 26 27 28 29 30 31 33 } *=22=222222222222222223 18 19 20 21 24 dx = -fx/fpx; x += dx; fx = f(x); fpx = fp(x); i++; if (i>Nmax) return -1; else printf("Finished after %d iterations.\n", i); return 0;
Rename the function newton_raphson() provided in the code snippet below to secant () and modify it to match the iteration formula of the secant method. Recall that the secant method needs two initial points, which we will pass as the arguments x and x1. The function's signature should be: int secant (double *xi, double x0, double x1, int Nmax, double TOL), where Nmax is the maximum allowed number of iterations and TOL is the tolerance for locating the root. Hint: You will also need to define a second internal variable xp and initialise its value to x1. In each iteration, xp will be updated to the old value of x, and x will be updated to the new point. As with newton_raphson(), the variable xi is passed by reference to secant (), expecting it to fill in the value of the root. This part can be left untouched. Again, the function should return 0 if it finishes successfully and -1 if the maximum number of iterations is exceeded. The stopping condition can also remain the same and there is no need to implement further sanity checks. Do not add any other print statements. Answer: (penalty regime: 10, 20, ... %) Reset answer 1 int newton_raphson(double *xi, double x, int Nmax, double TOL) { 10 11 12 13 14 15 16 17-> /* Initialize Algorithm */ double x, fx, fpx, dx; int i; X = XÔ; fx = f(x0); fp(x0); fpx = dx = -fx/fpx; /* Run Iteration Loop */ i=0; while (!((fabs(dx) < TOL) || (i>Nmax))) { 234567890123+ 25 } 26 27 28 29 30 31 33 } *=22=222222222222222223 18 19 20 21 24 dx = -fx/fpx; x += dx; fx = f(x); fpx = fp(x); i++; if (i>Nmax) return -1; else printf("Finished after %d iterations.\n", i); return 0;
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.5: A Closer Look: Loop Programming Techniques
Problem 12E: (Program) Write a program that tests the effectiveness of the rand() library function. Start by...
Related questions
Question
coding in c
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 3 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning