Write a C++ program in which you define a function, input an initial guess, and
ID: 3732713 • Letter: W
Question
Write a C++ program in which you define a function, input an initial guess, and implement Newton's Method. Your output should record each guess and the value of the function at the guess. Stop when two consecutive guesses are within 10 or after 15 steps. As usual, the program and the output should be imported into SWP. Evaluate your results. Values should be shown with 15 digit accuracy.
Run the program for the following:
1.f(x)=x³-10x²+22x+6. Start with an initial guess of x=2. What goes wrong? Try another guess.
2.f(x)=179.53x-436.85x+257.32x²-0.001. Find all three positive roots.
Explanation / Answer
1 .
// C++ program for implementation of Newton Raphson Method for
#include<bits/stdc++.h>
#define EPSILON 0.00000001
using namespace std;
// for function x³-10x²+22x+6
double func(double x)
{
return x*x*x - 10*x*x + 22*x+6;
}
double derivFunc(double x)
{
return 3*x*x - 20*x + 22 ;
}
void newton(double x)
{
int count =0 ;
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON && count <= 15)
{
h = func(x)/derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
count++;
cout << "for iteration " << count << " value of x is " << x << endl;
}
cout << "The value of the root is : " << x;
}
// Driver program to test above
int main()
{
double x0 = 2 ; // Initial values assumed
newton(x0);
return 0;
}
Output : for iteration 1 value of x is 5
for iteration 2 value of x is 2
for iteration 3 value of x is 5
for iteration 4 value of x is 2
for iteration 5 value of x is 5
for iteration 6 value of x is 2
for iteration 7 value of x is 5
for iteration 8 value of x is 2
for iteration 9 value of x is 5
for iteration 10 value of x is 2
for iteration 11 value of x is 5
for iteration 12 value of x is 2
for iteration 13 value of x is 5
for iteration 14 value of x is 2
for iteration 15 value of x is 5
for iteration 16 value of x is 2
The value of the root is : 2
This shows there is an oscillation between the values for finding the root which defeats the purpose of Newtons Method.
2. Code
// C++ program for implementation of Newton Raphson Method for
#include<bits/stdc++.h>
#define EPSILON 0.00000001
using namespace std;
// for function 179.53x-436.85x+257.32x²-0.001
double func(double x)
{
return 179.53 * (x*x*x*x*x*x) - 436.85 * (x*x*x*x) + 257.32* (x*x) - 0.001;
}
double derivFunc(double x)
{
return 179.53 * 6 * (x*x*x*x*x) -436.85 * 4 * (x*x*x) + 257.32 * 2 *x ;
}
void newton(double x)
{
int count =0 ;
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON && count <= 15)
{
h = func(x)/derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
count++;
cout << "for iteration " << count << " value of x is " << x << endl;
}
cout << "The value of the root is : " << x;
}
// Driver program to test above
int main()
{
double x0 = 2 ; // Initial values assumed
newton(x0);
return 0;
}
For intial values = 2 , The value of the root is : 1.197
For intial values = 1 , The value of the root is : 0.999994
For intial value = 0.5, The value of the root is : -0.00197135
Since this a even function if x is a root then -x will also be the root of the function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.