Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Newton\'s method is a technique for estimating the zeros of a function. It uses

ID: 3669365 • Letter: N

Question

Newton's method is a technique for estimating the zeros of a function. It uses the derivative of a function to find the points where it crosses the x-axis. If you aren't familiar with it, please either Google it or review it in your calculus text book.

Your assignment is to write a C++ program to use Newtons method on the function f(x) = 2(x^2) + x - 7

The program should take as it's input the following

1. The point of the first guess for the program to use.

2. The accuracy to which the number should be found (i.e. 0.0001 would be entered to find the number to the nearest thousandth).

The program will need a loop that will stop when the desired accuracy is met and then display the result. It should also contain comments in the code to explain all parts of the code, and correct messages for the user to work with.

Since Newtons method can fail if the initial starting point of the guess is to far off from any real roots, or is too close to a point where the derivative is close to zero, the program should include a feature that will break the loop if it goes through too many iterations (as that might indicate divergence), and display an appropriate error message.

Explanation / Answer

C++ program for Newton-Raphson Method


#include<iostream.h>
#include<conio.h>
#include<math.h>
#define EPS 0.000001
#define MAXIT 20
#define F(x) (x)*(x)*(x)+(x)*(x)-3*(x)-3
#define FD(x) 3*(x)*(x)+2*(x)-3

void main(){
clrscr();
int count;
float x0, xn, fx, fdx, fxn;
cout<<" NEWTON RAPHSON'S METHOD";
cout<<endl<<"x^3+x^2-3x-3";
cout<<endl<<"Enter the initial value of x:";
cin>>x0;
count=1;

begin:
fx=F(x0);
fdx=FD(x0);
xn=x0-fx/fdx;
if (fabs((xn-x0)/xn) < EPS){
cout<<"Approximate Root: "<<xn<<endl;
fxn =F(xn);
cout<<"Functional Value: "<<fxn<<endl;
cout<<"No. of Iterations: "<<count<<endl;
}
else{
x0=xn;
count=count+1;
if(count<MAXIT){
goto begin;
}
else{
cout<<"SOLUTION DOES NOT CONVERGE!!"<<endl;
cout<<"Iterations: "<<MAXIT<<endl;
}
}
getch();
}

Sample Output:

----------------------------------------------------------------------------------------------------------------------------------------

NEWTON RAPHSON’S METHOD

x^3+x^2-3x-3

Enter the initial value of x:2

Approximate Root: 1.73205

Functional Value: -2.94213e-07

No. of Iterations: 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote