C++
Còn đây là mấy cái code mà mình vừa học vừa làm:
1./ Dùng phương pháp Newton-Raphson để giải phương trình bậc 2.
#include
#include
#include
/* Giai phuong trinh bac 2 bang phuong phap NewtonRaphson
y=a*x^2+b*x+c
*/
int main()
{
double x , x_new ; // estimates of the root
double epsilon ; // tolerances
double f , fprime ; // values of the function and of its derivative
int nmax ; // max number of iterations
int n; // running index
/* Khai bao cac hang so cua phuong trinh a, b, c
*/
double a , b, c;
cout << "Newton-Raphson Method to solve Quadric equation y=a*x^2+b*x+c \n";
cout <<" \n\n enter value of a = ";
cin >> a;
cout <<" \n\n enter value of b = ";
cin >> b;
cout <<" \n\n enter value of c = ";
cin >> c;
cout <<" \n\n enter your initial guess of the root = ";
cin >> x;
cout <<" \n\n enter the maximum number of iterations = ";
cin >> nmax;
cout <<" \n\n enter the root tolerance (epsilon value) = ";
cin >> epsilon;
//cout <<" \n\n enter the function value tolerance ... ";
// cin >> ftol;
// Prepare the heading of the table
// cout <<"\n\n x f(x) f'(x) x_new \n\n";
cout <<"\n\n x x_new \n\n";
cout<< setiosflags(ios:: scientific);
cout<< setprecision(10);
// START THE LOOP
for( n = 1; n <= nmax ; n++ )
{
f = a*pow(x,2.0) +b*x+c ;
fprime = 2 *a* x ;
x_new = x - f / fprime ;
cout< // cout< cout<
if( fabs((x_new - x )/x ) < epsilon ) break;
// if( fabs(f) < ftol ) break; Cai nay la khong can thiet
x = x_new ;
}
cout << endl;
cout <<" enter e (exit) to terminate the program....";
char hold;
cin>> hold;
system ("pause");
return 0;
}
Made by Nam Lethanh
1./ Dùng phương pháp Newton-Raphson để giải phương trình bậc 2.
#include
#include
#include
/* Giai phuong trinh bac 2 bang phuong phap NewtonRaphson
y=a*x^2+b*x+c
*/
int main()
{
double x , x_new ; // estimates of the root
double epsilon ; // tolerances
double f , fprime ; // values of the function and of its derivative
int nmax ; // max number of iterations
int n; // running index
/* Khai bao cac hang so cua phuong trinh a, b, c
*/
double a , b, c;
cout << "Newton-Raphson Method to solve Quadric equation y=a*x^2+b*x+c \n";
cout <<" \n\n enter value of a = ";
cin >> a;
cout <<" \n\n enter value of b = ";
cin >> b;
cout <<" \n\n enter value of c = ";
cin >> c;
cout <<" \n\n enter your initial guess of the root = ";
cin >> x;
cout <<" \n\n enter the maximum number of iterations = ";
cin >> nmax;
cout <<" \n\n enter the root tolerance (epsilon value) = ";
cin >> epsilon;
//cout <<" \n\n enter the function value tolerance ... ";
// cin >> ftol;
// Prepare the heading of the table
// cout <<"\n\n x f(x) f'(x) x_new \n\n";
cout <<"\n\n x x_new \n\n";
cout<< setiosflags(ios:: scientific);
cout<< setprecision(10);
// START THE LOOP
for( n = 1; n <= nmax ; n++ )
{
f = a*pow(x,2.0) +b*x+c ;
fprime = 2 *a* x ;
x_new = x - f / fprime ;
cout<
if( fabs((x_new - x )/x ) < epsilon ) break;
// if( fabs(f) < ftol ) break; Cai nay la khong can thiet
x = x_new ;
}
cout << endl;
cout <<" enter e (exit) to terminate the program....";
char hold;
cin>> hold;
system ("pause");
return 0;
}
Made by Nam Lethanh
Không có nhận xét nào:
Đăng nhận xét