#define ZERO 1.0E-20</p>
using namespace::std;
void NewtonRaphson(double,double,int);
double f(double);
double Df(double);
int main(void)
{
double a,epsilon;
int Max;
cout<<"초E기¾a값ƨ£ a:";
cin>>a;
cout<<endl;
cout<<"근¾U의C 허a용¯e오¯A차¡À:";
cin>>epsilon;
cout<<endl;
cout<<"최O대¥e반öY복¬©ö횟¨ö수ùo:";
cin>>Max;
cout<<endl;
NewtonRaphson(a,epsilon,Max);
}
void NewtonRaphson(double a,double epsilon,int Max)
{
double c, cprev;//x축a 교¾©ø차¡À점¢®:c , 이I전u단¥U계Æe의C 값ƨ£ : cprev
double error;
int n;
c=a-f(a)/Df(a);
for(n=1;n<=Max; n++)
{
cout<<"n= " << setw(3) << n << "\t";
cout<<"c= " << setiosflags(ios::fixed) << setw(9) << setprecision(6) << c <<endl;
cout<<"f(c)= " << setiosflags(ios::fixed) << setw(9) << setprecision(6) << f(c) <<endl;
//f(c)가Æ¢® 0dp 근¾U접¡Ë하I여¯¨Ï 해¨ª가Æ¢® 되ìC는¥A지o 검ÆE사íc
if(fabs(f(c)) <ZERO)
{
cout<<"근¾U사íc해¨ª는¥A " << setprecision(8)<<c;
return;
}
a=c;
cprev=c;
c=a-f(a)/Df(a);
error=fabs(c-cprev);
cout<<"error="<<setiosflags(ios::fixed)<<setw(12)<<setprecision(8)<<error<<endl;
if(error <epsilon)
{
cout<<"근¾U사íc해¨ª는¥A: "<<setprecision(8)<<c;
return;
}
}
}
double f(double x)
{
return 1+cos(x)-4*x;
}
double Df(double x)
{
return -sin(x)-4;
}
</fstream></cmath></iomanip></iostream></textarea>
</div>