C++ Polynomial Equation Solutions
C++ Polynomial Equation Solutions
Hoocner:
#include <iostream>
#define N 1000
int a[N];
int n;
float c, p;
void nhap()
{
cout<< "Nhap so bac: ";
cin>> n;
cout<< "Nhap cac he so: " << endl;
for ( int i = 0; i <= n; i++ )
{
cout<< "Nhap he so a( " << i << " ): " << endl;
cin>> a[i];
};
}
void tinh()
{
cout<< "Nhap diem can tinh gia tri ham: ";
cin >> c;
p = a[0];
for ( int i = 1; i <= n; i++ )
{
p = p * c +a[i];
}
cout<< "Gia tri ham tai diem c = " << c << " la: " << p;
}
main()
{
nhap();
for( int i = 0; i < 3; i++) tinh();
return 0;
}
Hoocner tổng quát:
#include <iostream>
#define N 1000
1
TPL – LP
2
TPL – LP
return 0;
}
Macloranh Sin:
#include <iostream>
#include <math.h>
#define N 1000000
float sine(float x)
{
float q = x;
float t = x;
float eps=10^(-50);
for(int i=2; i<N; i=i+2)
{
t = -t*((x*x)/((i)*(i+1)));
q = q+t;
if(abs(t)<eps) break;
};
return q;
}
main()
{
float X;
cout<<"Nhap X theo don vi radian: ";
cin>> X;
cout<<"Sin("<<X<<")="<< sine(X)<<endl;
cout<<"Sin co san: sin("<<X<<")="<<sin(X);
return 0;
}
3
TPL – LP
4
TPL – LP
5
TPL – LP
#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5
6
TPL – LP
}
else
{
a = c;
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b << " \t - " << endl;
};
} while ( fabs( ham( c ) ) > eps );
}
else
{
do
{
c = ( a + b ) / 2;
if( ham( c ) > 0 )
{
b = c;
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b << " \t + " << endl;
}
else
{
a = c;
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b << " \t - " << endl;
};
}
while ( fabs( ham( c ) ) > eps );
};
cout << "Nghiem cua phuong trinh la: " << setprecision( M ) << c << endl;
};
}
main()
{
float a, b;
cout << " Nhap khoang nghiem: " << endl;
cout << " a = ";
cin >> a;
cout << " b = ";
cin >> b;
cout << " ----------------Result---------------- " << endl;
nghiem( a, b );
return 0;
7
TPL – LP
#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5
using namespace std;
8
TPL – LP
if( ham( n, A, c ) == 0 ) cout << setprecision( M ) << c << " la nghiem cua
phuong trinh ";
else
{
cout << setprecision( M ) << a << " \t " << setprecision( M ) << b
<< " \t " << setprecision( M ) << c << " \t " << setprecision( M ) << ham( n, A, c ) << endl;
if( ham( n, A, c ) * ham( n, A, a ) < 0 ) b = c;
else a = c;
};
}
while( fabs( ham( n, A, c ) ) > eps );
cout << " Nghiem cua phuong trinh la: " << setprecision( M ) << c << endl;
};
}
main()
{
int n;
float A[ N ];
float a, b;
cout << " Nhap he so bac cua phuong trinh: ";
cin >> n;
nhap( n, A );
cout << " Nhap khoang nghiem: " << endl;
cout << " a = ";
cin >> a;
cout << " b = ";
cin >> b;
cout << " ----------------Result---------------- " << endl;
nghiem( n, A, a, b );
return 0;
}
#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5
using namespace std;
float ham( float x )
{
9
TPL – LP
float f;
f = exp( x ) + x - 2; // Thay the ham o day
return f;
}
void nghiem( float a, float b)
{
float c;
if( a >= b ) cout << " Loi!!! ";
else if( ham( a ) == 0 ) cout << a << " la nghiem cua phuong trinh ";
else if( ham( b ) == 0 ) cout << b << " la nghiem cua phuong trinh ";
else if( ham( a ) * ham( b ) > 0 ) cout << " Khong phai khoang nghiem ";
else
{
do
{
c = a - ( ( b - a ) * ham( a ) ) / ( ham( b ) - ham( a ) );
if( ham( c ) == 0 ) cout << setprecision( M ) << c << " la nghiem cua
phuong trinh ";
else
{
cout << setprecision( M ) << a << " \t " << setprecision( M ) << b
<< " \t " << setprecision( M ) << c << " \t " << setprecision( M ) << ham( c ) << endl;
if( ham( c ) * ham( a ) < 0 ) b = c;
else a = c;
};
}
while( fabs( ham( c ) ) > eps );
cout << " Nghiem cua phuong trinh la: " << setprecision( M ) << c << endl;
};
}
main()
{
float a, b;
cout << " Nhap khoang nghiem: " << endl;
cout << " a = ";
cin >> a;
cout << " b = ";
cin >> b;
cout << " ----------------Result---------------- " << endl;
nghiem( a, b );
return 0;
}
10
TPL – LP
#include <iostream>
#include <math.h>
#include <iomanip>
#define eps 0.00001
#define M 4
using namespace std;
float ham( float x )
{
float f;
f = pow(x + 1, (float) 1 / 3 ); // Thay the ham o day
return f;
}
void nghiem( float c )
{
float g;
cout << " x " << " \t " << " g(x) " << endl;
while( fabs( c - ham( c ) ) > eps )
{
cout << setprecision(M) << c << " \t " << setprecision(M) << ham( c ) << endl;
g = ham( c );
c = g;
};
cout<< " Vay nghiem cua phuong trinh la: " << setprecision(M) << c << endl;
}
main()
{
float c;
cout << " Nhap Xo: " << endl;
cin >> c;
cout << " ----------------Result---------------- " << endl;
nghiem( c );
return 0;
}
Giải phương trình phương tiếp tuyến:
#include <iostream>
#include <math.h>
#include <iomanip>
#define eps 0.00001
#define M 4
#define N 1000
11
TPL – LP
12
TPL – LP
return 0;
}
Tách nghiệm định luật 3:
#include <iostream>
#include <math.h>
#define N 1000
13
TPL – LP
float A[ N ];
cout<< " Nhap he so bac cua phuong trinh: ";
cin>> n;
nhap( n, A );
tachnghiem( n, A );
return 0;
}
#include <iostream>
#include <math.h>
#define N 1000
14
TPL – LP
a = A[ 0 ];
cout<< a << endl;
for( int i = 0; i <= n; i++ )
{
if( a > A[ i ] ) a = A[ i ];
};
a = abs( a );
cout<< a << endl;
cout<< A[ 0 ] << endl;
cout<< a / A[ 0 ] <<endl;
cout<< 1 / m << endl;
cout<< pow( ( a / A[ 0 ] ), 1 / m ) << endl;
L = 1 + pow( ( a / A[ 0 ] ), 1 / m );
cout<< " Vay can tren cua nghiem duong cua phuong trinh la: " << L;
}
main()
{
int n;
float A[ N ];
cout<< " Nhap he so bac cua phuong trinh: ";
cin>> n;
nhap( n, A );
tachnghiem( n, A );
return 0;
}
#include <iostream>
#define N 10
15
TPL – LP
{
float c;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ )
{
cout<< " A[ " << i << " ][ " << j << " ] = \t ";
cin>> A[ i ][ j ];
};
xuat( A, n );
cout<< endl;
for( int i = 1; i <= n; i++ )
if( A[ i ][ i ] == 0 )
for( int j = i + 1; j <= n + 1; j++ )
if( A[ j ][ i ] != 0 )
for( int k = 1; k <= n + 1; k++ )
{
c = A[ j ][ k ];
A[ j ][ k ] = A[ i ][ k ];
A[ i ][ k ] = c;
};
}
void nghiem( float A[ ][ N ], int n )
{
float X[ N ];
float m;
for( int i = 1; i <= n; i++ )
for( int k = 1; k <= n - 1; k++ )
{
m = - A[ i + k ][ i ] / A[ i ][ i ];
for( int j = 1; j <= n + 1; j++ )
A[ i + k ][ j ] = A[ i + k ][ j ] + m * A[ i ][ j ];
};
X[ n + 1 ] = 0;
for( int i = n; i > 0; i-- )
{
float s = 0;
for( int j = i + 1; j <= n + 1; j++ )
{
s = s + ( A[ i ][ j ] / A[ i ][ i ] ) * X[ j ];
X[ i ]= ( A[ i ][ n + 1 ] / A[ i ][ i ] ) - s;
};
};
cout<< " Sau khi bien doi: " << endl;
16
TPL – LP
xuat( A, n);
cout<< " Nghiem He phuong trinh la: " << endl;
for( int i = 1; i <= n; i++ ) cout << " X[ " << i << " ]= " << X[ i ] << endl;
}
main()
{
int n ;
float A[ N ][ N ];
cout<< " Nhap n= ";
cin>> n;
nhapdh( A, n);
nghiem( A, n);
return 0;
}
#include <iostream>
#include <fstream>
#define N 10
17
TPL – LP
A[ i ][ k ] = c;
};
}
void nghiem( float A[ ][ N ], int n )
{
float X[ N ];
float m;
for( int i = 1; i <= n; i++ )
for( int k = 1; k <= n - 1; k++ )
{
m = - A[ i + k ][ i ] / A[ i ][ i ];
for( int j = 1; j <= n + 1; j++ )
A[ i + k ][ j ] = A[ i + k ][ j ] + m * A[ i ][ j ];
};
X[ n + 1 ] = 0;
for( int i = n; i > 0; i-- )
{
float s = 0;
for( int j = i + 1; j <= n + 1; j++ )
{
s = s + ( A[ i ][ j ] / A[ i ][ i ] ) * X[ j ];
X[ i ]= ( A[ i ][ n + 1 ] / A[ i ][ i ] ) - s;
};
};
cout<< " Sau khi bien doi: " << endl;
xuat( A, n);
cout<< " Nghiem He phuong trinh la: " << endl;
for( int i = 1; i <= n; i++ ) cout << " X[ " << i << " ]= " << X[ i ] << endl;
}
main()
{
int n ;
float A[ N ][ N ];
ifstream infile;
[Link]("[Link]");
infile >> n;
cout<< n << endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ ) infile>> A[ i ][ j ];
xuat( A, n);
[Link]();
doihang( A, n);
cout<< " ---- " << endl;
18
TPL – LP
xuat( A, n);
nghiem( A, n);
return 0;
}
int k = 2;
do
{
for( int i = 1; i <= n; i++ )
{
X[ k ][ i ] = A[ i ][ n + 1 ] / A[ i ][ i ];
for( int j = 1; j <= n; j++ )
if( j != i ) X[ k ][ i ] = X[ k ][ i ] - ( A[ i ][ j ] * X[ k - 1 ][ j ] ) / A[ i
][ i ];
};
t = fabs( X[ k ][ 1 ] - X[ k - 1 ][ 1 ] );
k++;
} while( t > 0.001 );
cout<< " X1 \t X2 \t X3 " << endl;
xuat( X, k - 1, n);
cout<< " Nghiem cua He phuong trinh la: ";
for( int i = 1; i <= n; i++ ) cout<< X[ k - 1 ][ i ] << " \t ";
cout<< endl;
}
main()
{
float A[ N ][ N ];
int n;
char k;
cout<< " Nhap n = ";
cin>> n;
nhap( A, n);
xuat( A, n, n + 1);
for( int i = 1; i < 100; i++ )
{
tinh( A, n);
cout<< " Muon dung thi nhan k + Enter: ";
cin>> k;
if( k == 'k' ) break;
};
return 0;
}
20
TPL – LP
#include <fstream>
#define N 10
21
TPL – LP
main()
{
float A[ N ][ N ];
int n;
char k;
ifstream infile;
[Link]("[Link]");
infile >> n;
cout<< n << endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ ) infile>> A[ i ][ j ];
[Link]();
xuat( A, n, n + 1);
for( int i = 1; i < 100; i++ )
{
tinh( A, n);
cout<< " Muon dung thi nhan k + Enter: ";
cin>> k;
if( k == 'k' ) break;
};
return 0;
}
Giải hệ Phương trình phương pháp giảm dư:
#include <iostream>
#include <fstream>
#include <math.h>
#define N 10
22
TPL – LP
{
float c;
for( int i = 1; i <= n; i++ )
if( A[ i ][ i ] == 0 )
for( int j = i + 1; j <= n + 1; j++ )
if( A[ j ][ i ] != 0 )
for( int k = 1; k <= n + 1; k++ )
{
c = A[ j ][ k ];
A[ j ][ k ] = A[ i ][ k ];
A[ i ][ k ] = c;
};
}
23
TPL – LP
int l;
int t;
float max;
do
{
int l = 0;
max = fabs( R[ 1 ] );
for( int j = 2; j <= n; j++ )
if( fabs( R[ j ] ) > max )
{
max = fabs( R[ j ] );
t = j;
};
float d = R[ t ];
for( int i = 1; i <= n; i++ ) // R
{
if( i != t ) R[ i ] = R[ i ] - A[ i ][ t ] * d;
else R[ i ] = 0;
};
;
for( int i = 1; i <= n; i++ ) // x
{
if( i != t ) X[ i ] = X[ i ];
else X[ i ] = X[ i ] + R[ i ];
};
for( int i = 1; i <= n; i++ )
cout<< X[ i ] << " \t ";
for( int i = 1; i <= n; i++ )
cout<< R[ i ] << " \t ";
cout<< endl;
for( int i = n + 1; i <= 2 * n; i++ )
if( fabs( X[ i ] ) <= 0.001 ) l++;
k++;
} while( l < n );
for( int i = 1; i <= n; i++ )
cout<< " Nghiem X " << i << " : " << X[ i ] << endl;
}
main()
{
float A[ N ][ N ];
float X[ N ];
float R[ N ];
24
TPL – LP
int n;
char k;
ifstream infile;
[Link]("[Link]");
infile >> n;
cout<< n << endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ ) infile>> A[ i ][ j ];
[Link]();
xuat( A, n, n + 1);
doihang( A, n );
biendoi( A, n );
for( int i = 1; i < 100; i++ )
{
nhapx( A, X, R, n );
tinh( A, X, R, n );
cout<< " Muon dung thi nhan k + Enter: ";
cin>> k;
if( k == 'k' ) break;
};
return 0;
}
25
TPL – LP
26
TPL – LP
main()
{
float A[ N ][ N ];
int n;
ifstream infile;
[Link]("[Link]");
infile >> n;
cout<< n << endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n ; j++ ) infile>> A[ i ][ j ];
[Link]();
xuat( A, n, n );
tinh( A, n);
return 0;
}
Véc tơ riêng Danhilepski:
#include <iostream>
#include <fstream>
#define N 10
27
TPL – LP
{
for( int i = 1; i <= n; i++ )
{
cout<< " Nhap Lamda " << i << " : ";
cin>> L[ i ];
};
}
28
TPL – LP
{
M1[ i ][ j ] = A[ k + 1 ][ j ];
if( j == k ) M[ i ][ j ] = 1 / A[ k + 1 ][ k ];
else M[ i ][ j ] = ( - A[ k + 1 ][ j ] ) / A[ k + 1 ][ k ];
}
else
{
if( j == i )
{
M1[ i ][ j ] = 1;
M[ i ][ j ] = 1;
}
else
{
M1[ i ][ j ] = 0;
M[ i ][ j ] = 0;
};
};
};
nhan2mt( M1, n, A, n, B );
nhan2mt( B, n, M, n, A );
nhan2mt( E, n, M, n, B );
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
E[ i ][ j ] = B[ i ][ j ];
};
cout<< " M " << endl;
xuat( B, n, n );
for( int k = 1; k <= n; k++ )
{
float c = 1;
Y[ n ][ 1 ] = 1;
for( int i = n - 1; i > 0; i-- )
{
c = c * L[ k ];
Y[ i ][ 1 ] = c;
};
cout<< " Y " << k << endl;
xuat( Y, n, 1 );
nhan2mt( B, n, Y, 1, X );
cout<< " X " << k << endl;
xuat( X, n, 1 );
};
29
TPL – LP
main()
{
float A[ N ][ N ];
float L[ N ];
int n;
ifstream infile;
[Link]("[Link]");
infile >> n;
cout<< n << endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n ; j++ ) infile>> A[ i ][ j ];
[Link]();
xuat( A, n, n );
nhap( L, n );
tinh( L, A, n);
return 0;
}
30
TPL – LP
};
for( int i = 0; i <= n; i++ )
{
A[ i ][ n + 3 ] = 1;
for( int j = 2; j < n+3 ; j++ ) A[ i ][ n + 3 ] = A[ i ][ n + 3 ] * A[ i ][ j ];
};
float W = 1;
float S = 0;
for( int i = 0; i <= n; i++ ) W = W * A[ i ][ i + 2 ];
for( int i = 0; i <= n; i++ ) S = S + ( A[ i ][ 1 ] / A[ i ][ n + 3 ] );
cout<< " Bang noi suy Ayken:" << endl;
for( int i = 0; i <= n; i++ )
for( int j = 2; j < n + 4; j++ )
{
cout<< A[i][j] << " \t ";
if(j == n + 3 ) cout<< endl;
};
cout<< " f( " << x << " ) = " << S * W;
}
31
TPL – LP
32
TPL – LP
{
for( int k = 1; k <= n-i+1; k++ ) cout<<" \t ";
cout<< A[i][n+2] << endl;
};
};
cout<<"f( " << x << " ) = " << A[n][n+1] << endl;
}
main()
{
float A[N][N];
int n;
float x;
char k;
cout<< " Nhap n: ";
cin>> n;
nhap( A, n );
for( int i = 0; i < N; i++ )
{
cout<< " Nhap x: ";
cin>> x;
tinh( A, n, x );
cout<<" Bang noi suy: " <<endl;
xuat( A, n, x );
cout<<" Nhan k de dung ";
cin>> k;
if(k== 'k') break;
};
return 0;
}
33
TPL – LP
{
cout<< A[i][j] << " \t ";
if(j == i + 1 ) cout<< endl;
};
}
}
main()
{
float A[N][N];
int n;
cout<< "Nhap n: ";
cin>> n;
nhap( A, n );
tinh( A, n );
cout<<" Bang noi suy: " <<endl;
xuat( A, n );
return 0;
}
float f( float x ) // doi ha`m cho^~ nay`, chu' y' dieu` kien. cua ham`
{
float f = 1 / ( 1 + x * x );
return f;
}
35
TPL – LP
cout<< endl;
cout<< " ----- Tich phan cua f(x) tu " << a << " den " << b << " = " << s * h << endl;
}
main()
{
float a, b;
int n;
char k;
for( int i = 0; i < N; i++ )
{
cout<< " Nhap can duoi: ";
cin>> a;
cout<< " Nhap can tren: ";
cin>> b;
cout<< " Nhap so doan chia: ";
cin>> n;
if( a > b ) cout<< " Loi \a " << endl;
else tinh( a, b, n );
cout<< " Muon dung bam k + Enter: ";
cin>> k;
if( k == 'k' ) break;
};
return 0;
}
Tích phân Parabol:
#include <iostream>
#include <iomanip>
#define N 100
36
TPL – LP
{
if( i % 2 == 0 ) s = s + 2 * f( a + i * h );
else s = s + 4 * f( a + i * h );
};
s = s + f( b );
cout<< " x : \t ";
for( int i = 0; i <= n; i++ ) cout<< setprecision( 3 ) << ( a + i * h ) << " \t ";
cout<< endl;
cout<< " y : \t ";
for( int i = 0; i <= n; i++ ) cout<< setprecision( 3 ) << f( a + i * h ) << " \t ";
cout<< endl;
cout<< " ----- Tich phan cua f(x) tu " << a << " den " << b << " = " << ( (s * h) / 3 ) <<
endl;
}
main()
{
float a, b;
int n;
char k;
for( int i = 0; i < N; i++ )
{
cout<< " Nhap can duoi: ";
cin>> a;
cout<< " Nhap can tren: ";
cin>> b;
cout<< " Nhap so doan chia: ";
cin>> n;
if( ( n % 2 != 0 ) || ( a > b ) ) cout<< " Loi \a " << endl;
else tinh( a, b, n );
cout<< " Muon dung bam k + Enter: ";
cin>> k;
if( k == 'k' ) break;
};
return 0;
}
37