نوع فایل

فرمت

سورس زبان سی پلاس پلاس

CPP , EXE

پیش نمایش از سورس کد پروژه

# include <stdio.h>
# include <math.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
double f(double x) //Definition of f(x) using double precision numbers
{
double function;
function = exp(x)+pow(2,-x)+2*cos(x)-6;
return(function);
}
double df(double x) //Derivative of f(x)= df(x)
{
double derivative;
derivative = exp(x)-pow(2,-x)-2*sin(x);
return(derivative);
}

void main()
{
clrscr();
int n, i=0; /* Number of steps n */
double x0; /* Initial guess at solution, x0 */
double x;
cout<<“Project newton raphson\n\n”;
cout<<“enter no of iterations\n”;
cin>>n;
cout<<“enter initial guess\n”;
cin>>x0;
double e;
cout<<“enter epsilon\n”;
cin>>e;
double x1;
x=x0;

cout<<“n\t\txn\t\t\tf(xn)\n”;
if (df(x) == 0) //Checks division by zero
cout<<“Method fails: zero derivative for this x value!”;
else
for(i=0;i<=n;i++) // Steps through the iteration
{
if(fabs(x-x1)>=e) //checks accuracy
{
cout<<i<<“\t\t”<<setprecision(6)<<setiosflags(ios::showpoint)<<setiosflags(ios::fixed)<<x<<“\t\t”<<f(x)<<“\n”;
x1=x;
x = x-(f(x)/df(x)); //Updates x using Newton
}
else
{
break;
}
}
if(fabs(x-x1)<e) //checks root with required accuracy found in given no of iterations
{
cout<<“so no of iterations=”<<i;
cout<<“\nroot=”<<x;

}

======