Skip to content

Instantly share code, notes, and snippets.

@behitek
Last active May 2, 2017 14:23
Show Gist options
  • Save behitek/8f13c612ad28367bce0683a8394151d3 to your computer and use it in GitHub Desktop.
Save behitek/8f13c612ad28367bce0683a8394151d3 to your computer and use it in GitHub Desktop.
Gửi FB Anh Hai
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
// Hang so e
// Duoc dung neu F(x) = 0 co chua e.
//VD: x * pow(e, x) - 1 = 0
const double e = 2.718281828459;
// So lan lap lai toi da
int maxLoop = 100;
// Gia tri do chinh xac
double epsilon = 0.0001;
// Dinh nghia ham F(x) = 0 tai day
#define F(x) ( pow(x,3) - pow(x,2) -2 )
// Dao ham cua F(x), chinh la F'(x) tai day
#define Fd(x) ( 3*pow(x,2) - 2*x )
// Dinh nghia F(x) dung trong phuong phap successive approximations
#define F1(x) ( pow(x,3) - pow(x,2) -2 )
/*
* Phuong phap Newton's de giai phuong trinh F(x) = 0
* Output:
* x - Nghiem gan dung nhat cua phuong trinh
* Return:
* So lan lap da su dung
*/
int NewtonMethodForEquation(double& x)
{
int n = 1;
while( ( fabs(F(x)) > epsilon ) && ( n <= maxLoop ) )
{
x = x - ( F(x) / Fd(x) );
n++;
}
return n;
}
/*
* Phuong phap Secant de giai phuong trinh F(x) = 0
* Input:
* x0 - Nghiem gan dung thu nhat
* x1 - Nghiem gan dung thu hai
* Output:
* x - Nghiem gan dung nhat cua phuong trinh
* Return:
* So lan lap da su dung
*/
int SecantMethodForEquation(double& x, double x0, double x1)
{
int n = 2;
while( ( fabs(F(x1)) > epsilon ) && ( n <= maxLoop ) )
{
x = x1 - (F(x1) * (x1 - x0)) / (F(x1) - F(x0));
x0 = x1;
x1 = x;
n++;
}
return n;
}
/*
* Phuong phap Successive approximations de giai phuong trinh F(x) = x
* Output:
* x - Nghiem gan dung nhat cua phuong trinh
* Return:
* So lan lap da su dung
*/
int SuccessiveApproxForEquation(double& x)
{
int n = 1;
while( ( fabs(x - F1(x)) > epsilon ) && ( n <= maxLoop ))
{
x = F1(x);
n++;
}
return n;
}
int main()
{
cout<<"\nGIAI PHUONG TRINH PHI TUYEN";
cout<<"\nNhap do chinh xac epsilon: ";
cin>>epsilon;
cout<<"\nNhap so lan lap toi da: ";
cin>>maxLoop;
double x; // Luu nghiem
int n; // Luu so lan lap
/* Vi du su dung Newton Method */
cout << "\nNewton's method: " << endl << endl;
cout << "Nhap nghiem ban dau gan dung: ";
cin >> x;
n = NewtonMethodForEquation(x);
if(n > maxLoop)
cout << "Trong " << maxLoop << " buoc lap, khong the tim thay ket qua!" << endl;
else
cout << "Ket qua la : " << x << " va tim duoc trong "
<< n << " buoc lap!" << endl;
double x0, x1;
/* Vi du su dung Secant Method */
cout << "\nSecant method: " << endl << endl;
cout << "Nhap nghiem gan dung thu nhat: ";
cin >> x0;
cout << "Nhap nghiem gan dung thu hai: ";
cin >> x1;
n = SecantMethodForEquation(x, x0, x1);
if(n > maxLoop)
cout << "Trong " << maxLoop << " buoc lap, khong the tim thay ket qua!" << endl;
else
cout << "Ket qua la : " << x << " va tim duoc trong "
<< n << " buoc lap!" << endl;
/* Vi du su dung Successive Approximations Method */
cout << "\nSuccessive approximations method: " << endl << endl;
cout << "Nhap nghiem ban dau gan dung: ";
cin >> x;
n = SuccessiveApproxForEquation(x);
if(n > maxLoop)
cout << "Trong " << maxLoop << " buoc lap, khong tim thay ket qua!" << endl;
else
cout << "Ket qua la: " << x << " va tim duoc trong "
<< n << " buoc lap!" << endl;
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment