0% found this document useful (0 votes)
44 views37 pages

C++ Polynomial Equation Solutions

The document contains code for solving equations using several numerical methods, including: 1) Bisection method for solving polynomial equations 2) Bisection method for solving transcendental equations 3) Secant method for solving equations

Uploaded by

Mạnh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views37 pages

C++ Polynomial Equation Solutions

The document contains code for solving equations using several numerical methods, including: 1) Bisection method for solving polynomial equations 2) Bisection method for solving transcendental equations 3) Secant method for solving equations

Uploaded by

Mạnh Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TPL – LP

Hoocner:
#include <iostream>
#define N 1000

using namespace std;

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

using namespace std;

1
TPL – LP

void nhap(int n, float a[] )


{
cout<<"Nhap cac he so: "<<endl;
for(int i=0; i <=n ; i++)
{
cout<<"Nhap he so a["<<i<<"]= ";
cin>>a[i];
};
}

void tinh(float c, int n, float a[] )


{
float b[N];
for(int i=0; i<=n; i++)
{
b[i]=a[i];
};
cout<<"Nhap c= ";
cin>>c;
for(int k=n; k>0; k--)
for(int i=1; i<=k; i++) b[i]= b[i-1] * c + b[i];
cout<< "Cac he so cua p[y+("<<c<<")] la:" <<endl;
for(int j=0; j<=n; j++)
{
cout<<"b["<<j<<"] la: "<<b[j]<<endl;
};
}
main()
{
int n;
float c, a[N];
char key;
cout<<"Nhap so bac: ";
cin>>n;
nhap(n,a);
for(int i=1; i<N; i++)
{
tinh(c,n,a);
cout<<endl;
cout<<"Nhap tiep hay khong? (Neu khong bam n, neu co thi nhan bat ki)";
cin>>key;
if(key=='n') break;
};

2
TPL – LP

return 0;
}

Macloranh Sin:
#include <iostream>
#include <math.h>
#define N 1000000

using namespace std;

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;
}

Giải phương trình phương pháp chia đôi:


#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5

using namespace std;

3
TPL – LP

void nhap( int n, float A[] )


{
cout << " Nhap he so: " << endl;
for( int i = 0; i <= n; i++ )
{
cout << " a[ " << i << " ]xX^( " << n - i << " )= ";
cin >> A[ i ];
};
}
float ham( int n, float A[], float x )
{
float kq = 0;
for( int i = 0; i <= n; i++ )
{
kq = kq + A[ i ] * pow( x, n - i );
};
return kq;
}
void nghiem( int n, float A[], float a, float b )
{
float c;
if( a >= b ) cout << " Loi!!! ";
else if( ham( n, A, a ) == 0 ) cout << a << " la nghiem cua phuong trinh ";
else if( ham( n, A, b ) == 0 ) cout << b << " la nghiem cua phuong trinh " ;
else if( ham( n, A, a ) * ham( n, A, b ) > 0 ) cout << " Khong phai khoang nghiem ";
else
{
if( ham( n, A, a ) > 0 )
{
for( int i = 0; i <= n; i++ )
A[ i ] = -1 * A[ i ];
do
{
c = ( a + b ) / 2;
if( ham( n, A, c ) > 0 )
{
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b;
b = c;
cout << setprecision( M ) << c << " \t + " << endl;
}
else
{

4
TPL – LP

cout << setprecision( M ) << a << " \t " << setprecision( M


) << b;
a = c;
cout << setprecision( M ) << c << " \t - " << endl;
};
} while ( fabs( ham( n, A, c ) ) > eps );
}
else
{
do
{
c = ( a + b ) / 2;
if( ham( n, A, c ) > 0 )
{
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b;
b = c;
cout << setprecision( M ) << c << " \t + " << endl;
}
else
{
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b;
a = c;
cout << setprecision( M ) << c << " \t - " << endl;
};
}
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;

5
TPL – LP

cout << " b = ";


cin >> b;
cout << " ----------------Result---------------- " << endl;
nghiem( n, A, a, b );
return 0;
}

Giải phương trình phương pháp chia đôi siêu việt:

#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5

using namespace std;

float ham( double x )


{
float f;
f = sin( x ) - x + 1/4; // 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
{
if( ham( a ) > 0 )
{
do
{
c = ( a + b ) / 2;
if( ham( c ) * -1 > 0 )
{
b = c;
cout << setprecision( M ) << a << " \t " << setprecision( M
) << b << " \t + " << endl;

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

Giải phương trình phương pháp dây cung:

#include <iostream>
#include <math.h>
#include <iomanip>
#define N 1000
#define eps 0.0001
#define M 5
using namespace std;

void nhap( int n, float A[] )


{
cout << " Nhap he so: " << endl;
for( int i = 0; i <= n; i++ )
{
cout << " a[ " << i << " ]xX^( " << n - i << " )= ";
cin >> A[ i ];
};
}
float ham( int n, float A[], float x)
{
float kq = 0;
for( int i = 0; i <= n; i++ )
{
kq = kq + A[ i ] * pow( x, n - i );
};
return kq;
}
void nghiem( int n, float A[], float a, float b)
{
float c;
if( a >= b ) cout << " Loi!!! ";
else if( ham( n, A, a ) == 0 ) cout << a << " la nghiem cua phuong trinh ";
else if( ham( n, A, b ) == 0 ) cout << b << " la nghiem cua phuong trinh ";
else if( ham( n, A, a ) * ham( n, A, b ) > 0 ) cout << " Khong phai khoang nghiem ";
else
{
do
{
c = a - ( ( b - a ) * ham( n, A, a ) ) / ( ham( n, A, b ) - ham( n, A, a ) );

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;
}

Giải phương trình phương pháp dây cung siêu việt:

#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

Giải phương trình phương pháp lặp:

#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

using namespace std;


float ham( float x )
{
float kq;
kq = exp(x) + x +1; // thay ham o day
return kq;
}
float dh( float x )
{
float kq;
kq = exp(x) +1; // thay ham o day
return kq;
}
float dhb2( float x )
{
float kq;
kq = exp(x);
return kq;
}
void nghiem( float c )
{
float y;
cout<< " x " << " \t " << " f(x)/f'(x) " << endl;
for( int i = 0; i < N ; i++ )
{
y = c - ham( c )/dh( c );
if( fabs( c - y ) > eps )
{
c = y;
cout << c << " \t " << ham( c ) / dh( c ) << endl;
}
else break;
};
cout<< " Vay nghiem cua phuong trinh la: " << setprecision(M) << y << endl;
}
main()
{
float x;
cout << " Nhap Xo: " << endl;
cin >> x;
cout << " ----------------Result---------------- " << endl;
nghiem( x );

12
TPL – LP

return 0;
}
Tách nghiệm định luật 3:

#include <iostream>
#include <math.h>
#define N 1000

using namespace std;

void nhap( int n, float A[] )


{
cout << " Nhap he so: " << endl;
for( int i = 0; i <= n; i++ )
{
cout << " a[ " << i << " ]xX^( " << n - i << " )= ";
cin >> A[ i ];
A[ i ] = abs( A[ i ] );
};
}
float max( float A[], int a, int b )
{
float max = A[ a ];
for( int i = a; i <= b; i++ )
{
if( max < A[ i ] ) max = A[ i ];
};
return max;
}
void tachnghiem( int n, float A[] )
{
float m1, m2;
m1 = max( A, 1, n );
cout<< " m1 = " << m1 << endl;
m1 = 1 + ( m1 / A[ 0 ] );
m2 = max( A, 0, n-1 );
cout<< " m2 = " << m2 << endl;
m2 = A[ n ] / ( m2 + A[ n ] ) ;
cout<< " Vay: " << m2 << " <= |x| <= " << m1;
}
main()
{
int n;

13
TPL – LP

float A[ N ];
cout<< " Nhap he so bac cua phuong trinh: ";
cin>> n;
nhap( n, A );
tachnghiem( n, A );
return 0;
}

Tách nghiệm định luật 4:

#include <iostream>
#include <math.h>
#define N 1000

using namespace std;

void nhap( int n, float A[] )


{
cout << " Nhap he so: " << endl;
for( int i = 0; i <= n; i++ )
{
cout << " a[ " << i << " ]xX^( " << n - i << " )= ";
cin >> A[ i ];
};
}
void tachnghiem( int n, float A[] )
{
float m;
float a;
float L;
if( A[ 0 ] < 0 ) cout<< " Loi !!";
else
{
for( int i = 0; i <= n; i++ )
{
if( A[ i ] < 0 )
{
m = i;
break;
};
};
cout << m << endl;
};

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;
}

Giải hệ phương trình phương pháp Gauss:

#include <iostream>
#define N 10

using namespace std;

void xuat( float A[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ )
{
cout<< A[ i ][ j ] << "\t ";
if( j == n + 1 ) cout<< endl;
};
}
void nhapdh( float A[ ][ N ], int n )

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;
}

Giải hệ phương trình phương pháp Gauss( đọc file ):

#include <iostream>
#include <fstream>
#define N 10

using namespace std;

void xuat( float A[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ )
{
cout<< A[ i ][ j ] << "\t ";
if( j == n + 1 ) cout<< endl;
};
}
void doihang( float A[ ][ N ], int n )
{
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 ];

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;
}

Giải hệ phương trình phương pháp Gauss - Siedel:


#include <iostream>
#include <math.h>
#include <iomanip>
#define N 10

using namespace std;

void nhap( float A[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n + 1; j++ )
{
cout<< " A[ " << i << " ][ " << j << " ]= ";
cin>> A[ i ][ j ];
}
}

void xuat( float A[ ][ N ], int h, int c )


{
for( int i = 1; i <= h; i++ )
for( int j = 1; j <= c; j++ )
{
cout<< setprecision( 4 ) << A[ i ][ j ] << " \t ";
if( j == c ) cout<< endl;
};
}

void tinh( float A[ ][ N ], int n )


{
float t;
float X[ N ][ N ];
cout<< " Nhap nghiem ban dau: " << endl;
for( int i = 1; i <= n; i++ )
{
cout<< " X " << i << " = ";
cin>> X[ 1 ][ i ];
}
19
TPL – LP

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;
}

Giải hệ phương trình phương pháp Gauss - Siedel( đọc file ):


#include <iostream>
#include <math.h>
#include <iomanip>

20
TPL – LP

#include <fstream>
#define N 10

using namespace std;

void xuat( float A[ ][ N ], int h, int c )


{
for( int i = 1; i <= h; i++ )
for( int j = 1; j <= c; j++ )
{
cout<< setprecision( 4 ) << A[ i ][ j ] << " \t ";
if( j == c ) cout<< endl;
};
}

void tinh( float A[ ][ N ], int n )


{
float t;
float X[ N ][ N ];
cout<< " Nhap nghiem ban dau: " << endl;
for( int i = 1; i <= n; i++ )
{
cout<< " X " << i << " = ";
cin>> X[ 1 ][ i ];
}
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;

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

using namespace std;

void xuat( float A[ ][ N ], int h, int c )


{
for( int i = 1; i <= h; i++ )
for( int j = 1; j <= c; j++ )
{
cout<< A[ i ][ j ] << "\t ";
if( j == c ) cout<< endl;
};
}

void doihang( float A[ ][ N ], int n )

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;
};
}

void biendoi( float A[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
{
float t = A[ i ][ i ];
for( int j = 1; j <= n + 1; j++ )
A[ i ][ j ] = A[ i ][ j ] / t;
};
xuat( A, n, n + 1 );
}

void nhapx( float A[ ][ N ], float X[ ], float R[ ], int n)


{
for( int j = 1; j <= n; j++ )
{
cout<< " Nhap X0 : " << j << " : = ";
cin>> X[ j ];
};
for( int j = 1; j <= n; j++ )
{
R[ j ] = A[ j ][ n + 1 ];
for( int k = 1; k <= n; k++ )
R[ j ] = R[ j ] - A[ j ][ k ] * X[ k ];
};
}

void tinh( float A[ ][ N ], float X[ ], float R[ ], int n )


{
int k = 1;

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;
}

Giá trị riêng Danhilepski:


#include <iostream>
#include <fstream>
#define N 10

using namespace std;

void nhan2mt( float A[ ][ N ], float B[ ][ N ], float C[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
C[ i ][ j ] = 0;
for( int k = 1; k <= n; k++ )
C[ i ][ j ] = C[ i ][ j ] + A[ i ][ k ] * B[ k ][ j ];
}
}

void xuat( float A[ ][ N ], int h, int c )


{

25
TPL – LP

for( int i = 1; i <= h; i++ )


for( int j = 1; j <= c; j++ )
{
cout<< A[ i ][ j ] << " \t ";
if( j == c ) cout<< endl;
};
}

void tinh( float A[ ][ N ], int n )


{
float B[ N ][ N ];
float M[ N ][ N ];
float M1[ N ][ N ];
for( int k = n - 1; k >= 1; k-- )
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
if( i == k )
{
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, A, B, n);
nhan2mt( B, M, A, n );
cout<< " Lan " << n - k << endl;
cout<< " Ma tran M^-1: " << endl;
xuat( M1, n, n );

26
TPL – LP

cout<< " Ma tran M: "<<endl;


xuat( M, n, n );
cout<< " Ma tran A: "<<endl;
xuat( A, n, n );
};
cout<< " Ket qua: " << endl;
xuat( A, 1, n );
}

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

using namespace std;

void nhan2mt( float A[ ][ N ], int n, float B[ ][ N ], int c, float C[ ][ N ] )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= c; j++ )
{
C[ i ][ j ] = 0;
for( int k = 1; k <= n; k++ )
C[ i ][ j ] = C[ i ][ j ] + A[ i ][ k ] * B[ k ][ j ];
}
}

void nhap( float L[ ], int n )

27
TPL – LP

{
for( int i = 1; i <= n; i++ )
{
cout<< " Nhap Lamda " << i << " : ";
cin>> L[ i ];
};
}

void xuat( float A[ ][ N ], int h, int c )


{
for( int i = 1; i <= h; i++ )
for( int j = 1; j <= c; j++ )
{
cout<< A[ i ][ j ] << " \t ";
if( j == c ) cout<< endl;
};
}

void mtrE( float E[ ][ N ], int n )


{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
if( j == i ) E[ i ][ j ] = 1;
else E[ i ][ j ] = 0;
};
}

void tinh( float L[ ], float A[ ][ N ], int n )


{
float B[ N ][ N ];
float M[ N ][ N ];
float M1[ N ][ N ];
float Y[ N ][ N ];
float X[ N ][ N ];
float E[ N ][ N ];
mtrE( E, n );
for( int k = n - 1; k >= 1; k-- )
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= n; j++ )
{
if( i == k )

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;
}

Nội suy Ayken dạng 1:


#include <iostream>
#define N 100
using namespace std;

void nhap( int n, float A[][ N ] )


{
for( int i=0; i<= n; i++ )
{
cout<< " X[ " << i << " ] = ";
cin>> A[ i ][ 0 ];
cout<< " Y[ " << i << " ] = ";
cin>> A[ i ][ 1 ];
}
}

void tinh(float x, int n, float A[][ N ] )


{
for( int i = 0; i <= n; i++ )
for( int j = 2; j <= n+3; j++ )
{
if( j == i + 2 ) A[ i ][ j ] = x - A[ i ][ 0 ];
else A[ i ][ j ] = A[ i ][ 0 ] - A[ j - 2 ][ 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;
}

void nhapx( float x, int n, float A[][ N ] )


{
float k;
cout<< " Nhap x: ";
cin>> x;
for( int i = 0; i <= n; i++ )
if( x == A[ i ][ 0 ] )
{
cout<< " f ( " << x << " ) = " << A[ i ][ 1 ];
k = 1;
break;
}
if( k != 1) tinh( x, n, A );
}
main()
{
float A[N][N];
int n;
float x;
char k;
cout<< " Nhap n: ";
cin>> n;
nhap( n, A );

31
TPL – LP

for( int i = 0; i < N; i++ )


{
nhapx( x, n, A );
cout<< endl;
cout<<" Nhan k + Enter de dung: ";
cin>> k;
if(k== 'k') break;
};
return 0;
}

Nội suy Ayken dạng 2:


#include <iostream>
#include <iomanip>
#define N 10
using namespace std;
void nhap( float A[ ][N], int n )
{
for( int i = 1; i <= n; i++)
{
cout<< " Nhap x( " << i << " )= ";
cin>> A[i][1];
cout<< " Nhap y( " << i << " )= ";
cin>> A[i][2];
cout<<"------"<<endl;
}
}
void tinh( float A[ ][N], int n, float x )
{
for( int i = 1; i <= n; i++)
A[i][n+2] = A[i][1] - x;
for( int j = 3; j < n + 2; j++ )
for( int i = j - 1; i <= n; i++ )
A[i][j] = ( A[j-2][j-1] * A[i][n+2] - A[i][j-1] * A[j-2][n+2] ) / ( A[i][1] -
A[j-2][1] );
}
void xuat( float A[ ][N], int n, float x )
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j < i + 2; j++ )
{
cout<< setprecision( 4 ) << A[i][j] << " \t ";
if(j == i + 1 )

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

Nội suy Newton:


#include <iostream>
#define N 10
using namespace std;
void nhap( float A[ ][N], int n )
{
for( int i = 0; i < n; i++)
{
cout<< "Nhap x( " << i << " )= ";
cin>> A[i][0];
cout<< "Nhap y( " << i << " )= ";
cin>> A[i][1];
cout<<"------"<<endl;
}
}
int test( float A[ ][N], int n )
{
float H[N];
int t = 0;
for( int i = n - 1; i >= 0; i--)
H[i] = A[i][0] - A[i-1][0];
for( int i = 0; i < n - 1; i++ )
if( ( H[i] - H[i+1] ) ==0 ) t++;
return t;
}
void tinh( float A[ ][N], int n )
{
int t = test( A, n );
if( t == ( n - 1) )
{
for( int j = 2; j <= n; j++ )
for( int i = j - 1; i < n; i++ )
A[i][j] = A[i][j-1] - A[i-1][j-1];
}
else cout<< "Loi!!!";
}
void xuat( float A[ ][N], int n )
{
int t = test( A, n );
if( t == ( n - 1) )
{
for( int i = 0; i < n; i++ )
for( int j = 0; j < i + 2; j++ )
34
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;
}

Tích phân hình thang:


#include <iostream>
#include <iomanip>
#define N 100

using namespace std;

float f( float x ) // doi ha`m cho^~ nay`, chu' y' dieu` kien. cua ham`
{
float f = 1 / ( 1 + x * x );
return f;
}

void tinh( float a, float b, int n )


{
float h = ( b - a ) / n;
float s = f( a ) / 2;
for( int i = 1; i < n; i++ ) s = s + f( a + i * h );
s = s + ( f( b ) / 2 );
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 ";

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

using namespace std;

float f( float x ) // doi ha`m cho^~ nay`


{
float f = 1 / ( 1 + x * x );
return f;
}

void tinh( float a, float b, int n )


{
float h = ( b - a ) / n;
float s = f( a );
for( int i = 1; i < n; i++ )

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;
}

Code Phương Pháp Tính 2018


Trần Phi Long 15DT1

37

You might also like