Write a well commented C++ program to implement Newton\'s method of finding the
ID: 3800818 • Letter: W
Question
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int degre,i=0,cnt=0,flag=0;
int coef[10]={0};
float x=0,x2=0,t=0;
bool CalcF(float& x,float &v,float&g)
{
printf(" ******************************************************");
printf(" ITERATION X FX F'X ");
printf(" **********************************************************");
do
{
cnt++;
v=g=0;
for(i=degre;i>=1;i--)
{
v+=coef[i] * (pow(x,i)) ;
}
v+=coef[0];
for(i=degre;i>=0;i--)
{
g+=coef[i]* (i*pow(x,(i-1)));
}
t=x2;
x2=(x-(v/g));
x=x2;
printf(" %d %.3f %.3f %.3f ",cnt,x2,v,g);
}while((fabs(t - x))>=0.0001);
printf(" THE ROOT OF EQUATION IS %f",x2);
if(g==0)
return false;
return true;
}
int main()
{
cout<<"Enter Highest degree of polynomial:::: ";
cin>>degre;
for(i=0;i<=degre;i++)
{
printf(" x^%d::",i);
cin>>coef[i];
}
cout<<" ";
printf(" polynomial is ");
for(i=degre;i>=0;i--)//printing coeff.
{
printf(" %dx^%d",coef[i],i);
}
cout<<" Enter X---->";
cin>>x;
float v=0,g=0;
CalcF(x,v,g);
}
=============================================================================
akshay@akshay-Inspiron-3537:~/Chegg$ g++ newton.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter Highest degree of polynomial:::: 3
x^0::-6
x^1::-25
x^2::-3
x^3::4
polynomial is 4x^3 -3x^2 -25x^1 -6x^0
Enter X---->2
******************************************************
ITERATION X FX F'X
**********************************************************
1 5.273 -36.000 11.000
2 3.954 365.139 276.983
3 3.266 95.580 138.926
4 3.030 19.739 83.439
5 3.000 1.973 66.984
6 3.000 0.029 65.029
7 3.000 0.000 65.000
THE ROOT OF EQUATION IS 3.000000
=======================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.