pleasehelpme
Member
یه سری برنامه ی محاسبات عددی:
روش دو بخشی:
نابجایی:
نیوتن:
تکرار ساده:
وتری:
روش دو بخشی:
کد:
//Bisection method
#include<iostream.h>
#include<math.h>
#include<conio.h>
double f(double x){return 3*x-exp(-x);}
void main(){
float a,b,eps,x;
int n=1;
clrscr();
cout<<"enter a,b,eps:";
cin>>a>>b>>eps;
x=(a+b)/2;
while(fabs(f(x))>=eps){
if (f(x)*f(a)>0)
a=x;
else
b=x;
x=(a+b)/2;
n++;
}
cout<<"ROOT = "<<x;
cout<<"\nITERATION = "<<n;
getch();
}
نابجایی:
کد:
//Regula falsi method
#include<iostream.h>
#include<math.h>
#include<conio.h>
double f(double x) { return x * x * x * x - sinh(x);}
void main(){
clrscr();
double eps,a,b,x;
int n=1;
cout<<"Enter a,b,eps:";
cin>>a>>b>>eps;
x = ( a * f(b) - b * f(a) ) / ( f(b) - f(a) );
while ( fabs( f(x) ) >= eps ){
cout<< x << "\n";
if ( f(x)*f(a)>0)
a=x;
else
b=x;
x=(a*f(b)-b*f(a)) / (f(b)-f(a));
n++;
}
cout<<"ROOT = " << x;
cout<<"\nITERATION = " << n;
getch();
}
نیوتن:
کد:
//Newton-Raphson method
#include<iostream.h>
#include<math.h>
#include<conio.h>
double f( double x ) { return 100 * sin(x) - x;}
double fprime( double x ) { return 100 * cos(x) - 1;}
void main(){
clrscr();
double eps,x0,x;
int n=1;
cout<< "Enter x0,eps:";
cin>> x0 >> eps;
x = x0 - f(x0) / fprime(x0);
while ( fabs(f(x) ) >= eps){
cout<< x << "\n";
x0 = x;
x = x0 - f(x0) / fprime(x0);
n++;
}
cout<< "ROOT = "<< x;
cout<< "\nITERATION = "<< n;
getch();
}
تکرار ساده:
کد:
//Fixed point iteration method
#include<iostream.h>
#include<math.h>
#include<conio.h>
double f(double x){return 100*sin(x)-x;}
double g(double x){return asin(0.01*x);}
void main(){
clrscr();
double eps,x0,x1,x;
int n=1;
cout<<"Enter x0,eps:";
cin>>x0>>eps;
x=g(x0);
while (fabs(x-x0) >= eps){
x0=x;
x=g(x0);
n++;
}
cout<<"ROOt ="<<x;
cout<<"\nITERATION ="<<n;
getch();
}
وتری:
کد:
/secant method
#include<iostream.h>
#include<math.h>
#include<conio.h>
double f( double x ) {return x * x * x * x - sinh(x);}
void main(){
clrscr();
double eps,x0,x1,x;
int n=1;
cout<<"Enter x0,x1,eps :\n";
cin>>x0>>x1>>eps;
x = x1 - ( f(x1) * (x1-x0) ) / ( f(x1) - f(x0) );
while( fabs(x1-x0) >= eps ){
cout<< x0 << "\t" << x1 << "\t" << x << "\n";
x0 = x1;
x1 = x;
x = x1 - ( f(x1) * ( x1 - x0 )) / ( f(x1) - f(x0) );
n++;
}
cout<<"ROOT = " << x;
cout<<"\nITERATION = " << n;
getch();
}