forked from mymmsc/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,374 @@ | ||
1.��˹������Ԫ�� | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
#define N 10 | ||
#define EPS 1e-10 //����EPSΪ1����10��-10�η� | ||
void main() | ||
{float A[N][N+1]; //����zengguang���� | ||
|
||
float sum=0; | ||
int i,j,k; | ||
int n; | ||
int flag=1; | ||
|
||
|
||
while(flag) | ||
{ | ||
printf("������ϵ������Ĵ�С��"); | ||
scanf("%d", &n); | ||
if(n>N){ | ||
printf("�������\n"); | ||
continue; | ||
} | ||
flag=0; | ||
} | ||
printf("������ϵ������ֵ��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
{ | ||
printf("a[%d][%d]: ", i, j); | ||
scanf("%f", &A[i][j]); | ||
} | ||
} | ||
|
||
/*��ʾԭʼ����*/ | ||
printf("\nԭʼ����\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
printf("%0.3f ",A[i][j]); | ||
printf("\n"); | ||
} | ||
printf("\n\n"); | ||
|
||
int x[N]; | ||
int Pr,t,total; | ||
for(k=1;k<N;k++) | ||
{ | ||
Pr=k-1; | ||
for(i=k;i<N;i++) | ||
{ | ||
if(abs(A[i][k-1])>abs(A[Pr][k-1])) | ||
{ | ||
Pr=i; | ||
} | ||
}//End For i | ||
if(abs(A[i][k-1])<EPS) | ||
{ | ||
printf("��Ԫ�ӽ���0������ʧЧ\n"); | ||
exit(0); | ||
} | ||
if(Pr>k-1) | ||
{ | ||
for(j=k-1;j<N+1;j++) | ||
{ | ||
t=A[k-1][j]; | ||
A[k-1][j]=A[Pr][j]; | ||
A[Pr][j]=t; | ||
}//End For j | ||
} //End if | ||
for(i=k;i<N;i++) | ||
{ | ||
t=A[i][k-1]/A[k-1][k-1]; | ||
for(j=k-1;j<N+1;j++) | ||
{ | ||
A[i][j]=A[i][j]-t*A[k-1][j]; | ||
} //End For j | ||
} //End For i | ||
} //End For k | ||
if(abs(A[N-1][N-1])<EPS) | ||
{ | ||
printf("��Ԫ�ӽ���0������ʧЧ\n"); | ||
exit(0); | ||
} //End If | ||
x[N-1]=A[N-1][N]/A[N-1][N-1]; | ||
for(i=N-2;i>=0;i--) | ||
{ | ||
total=A[i][N]; | ||
for(j=N-1;j>=i+1;j--) | ||
{ | ||
total-=A[i][j]*x[j]; | ||
} | ||
x[i]=total/A[i][i]; | ||
}//End For i | ||
for(i=0;i<N;i++) | ||
{ | ||
printf("\nx[%d]=%d\n",(i+1),x[i]); | ||
}//End For i | ||
} | ||
|
||
?2. LU�ֽⷨ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define N 10 //�����С��Χ | ||
|
||
/* | ||
* ʹ���Ѿ������x����ǰ����x����getx()���ã� | ||
* float a[][] ����U | ||
* float x[] ������� | ||
* int i �����ţ�����XԪ����ţ� | ||
* int n �����С | ||
* return ��ʽ����Ҫ�ĺ� | ||
*/ | ||
float getmx(float a[N][N], float x[N], int i, int n) | ||
{ | ||
float mx = 0; | ||
int r; | ||
for(r=i+1; r<n; r++) | ||
{ | ||
mx += a[i][r] * x[r]; | ||
} | ||
return mx; | ||
} | ||
|
||
/* | ||
* ʹ���Ѿ������y����ǰ����y����gety()���ã� | ||
* float a[][] ����L | ||
* float y[] ����Y | ||
* int i ����YԪ����� | ||
* int n �����С | ||
* return ��ʽ����Ҫ�ĺ� | ||
*/ | ||
float getmy(float a[N][N], float y[N], int i, int n) | ||
{ | ||
float my = 0; | ||
int r; | ||
for(r=0; r<n; r++) | ||
{ | ||
if(i != r) my += a[i][r] * y[r]; | ||
} | ||
return my; | ||
} | ||
|
||
/* | ||
* �ⷽ���飬����ijx | ||
* float a[][] ����U | ||
* float x[] ������� | ||
* int i ������ | ||
* int n �����С | ||
* return ������ĵ�i���⣨����X�ĵ�i��Ԫ��ֵ�� | ||
*/ | ||
float getx(float a[N][N], float b[N], float x[N], int i, int n) | ||
{ | ||
float result; | ||
|
||
if(i==n-1) //�������һ��x��ֵ | ||
result = (float)(b[i]/a[n-1][n-1]); | ||
else //��������xֵ�����ڹ�ʽ�е���Ͳ��֣���Ҫ����getmx()������ | ||
result = (float)((b[i]-getmx(a,x,i,n))/a[i][i]); | ||
|
||
return result; | ||
} | ||
|
||
/* | ||
* ������Y����������һԪ��ֵ | ||
* float a[][] ����L | ||
* float y[] ����Y | ||
* int i ����YԪ����� | ||
* int n �����С | ||
* return ����Y�ĵ�i��Ԫ��ֵ | ||
*/ | ||
float gety(float a[N][N], float b[N], float y[N], int i, int n) | ||
{ | ||
float result; | ||
|
||
if(i==0) //�����һ��y��ֵ | ||
result = float(b[i]/a[i][i]); | ||
else //��������yֵ�����ڹ�ʽ�е���Ͳ��֣���Ҫ����getmy()������ | ||
result = float((b[i]-getmy(a,y,i,n))/a[i][i]); | ||
|
||
return result; | ||
} | ||
|
||
void main() | ||
{ float l[N][N]={0}; //����L���� | ||
float u[N][N]={0}; //����U���� | ||
float y[N]={0}; //��������Y | ||
float x[N]={0}; //��������X | ||
float a[N][N]; //����ϵ������ | ||
float b[N]; //�����Ҷ��� | ||
float sum=0; | ||
int i,j,k; | ||
int n; | ||
int flag=1; | ||
|
||
while(flag) | ||
{ | ||
printf("������ϵ������Ĵ�С��"); | ||
scanf("%d", &n); | ||
if(n>N){ | ||
printf("�������\n"); | ||
continue; | ||
} | ||
flag=0; | ||
} | ||
printf("������ϵ������ֵ��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
{ | ||
printf("a[%d][%d]: ", i, j); | ||
scanf("%f", &a[i][j]); | ||
} | ||
} | ||
printf("�������Ҷ������飺\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
printf("b[%d]: ", i); | ||
scanf("%f", &b[i]); | ||
} | ||
|
||
/*��ʾԭʼ����*/ | ||
printf("\nԭʼ����\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
printf("%0.3f ",a[i][j]); | ||
printf("\n"); | ||
} | ||
printf("\n\n"); | ||
|
||
/*��ʼ������l*/ | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
{ | ||
if(i==j) l[i][j] = 1; | ||
} | ||
} | ||
|
||
/*��ʼLU�ֽ�*/ | ||
/*��һ�����Ծ���U�����н��м���*/ | ||
for(i=0; i<n; i++) | ||
{ | ||
u[0][i] = (float)(a[0][i]/l[0][0]); | ||
} | ||
/*�ڶ���������LU�ֽ�*/ | ||
for(i=0; i<n-1; i++) | ||
{ | ||
/*�ԡ�L�С����м���*/ | ||
for(j=i+1; j<n; j++) | ||
{ | ||
for(k=0,sum=0; k<n; k++) | ||
{ | ||
if(k != i) sum += l[j][k]*u[k][i]; | ||
} | ||
l[j][i] = (float)((a[j][i]-sum)/u[i][i]); | ||
} | ||
/*�ԡ�U�С����м���*/ | ||
for(j=i+1; j<n; j++) | ||
{ | ||
for(k=0,sum=0; k<n; k++) | ||
{ | ||
if(k != i+1) sum += l[i+1][k]*u[k][j]; | ||
} | ||
u[i+1][j] = (float)((a[i+1][j]-sum)); | ||
} | ||
} | ||
|
||
/*�������l*/ | ||
printf("����L��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
{ | ||
printf("%0.3f ", l[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
/*�������u*/ | ||
printf("\n����U��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
for(j=0; j<n; j++) | ||
{ | ||
printf("%0.3f ", u[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
/*�ش���ʽ��������Y*/ | ||
for(i=0; i<n; i++) | ||
{ | ||
y[i] = gety(l,b,y,i,n); | ||
} | ||
|
||
/*��ʾ����Y*/ | ||
printf("\n\n����Y��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
printf("y%d = %0.3f\n", i+1,y[i]); | ||
} | ||
|
||
/*�ش���ʽ��������X*/ | ||
for(i=n-1; i>=0; i--) | ||
{ | ||
x[i] = getx(u,y,x,i,n); | ||
} | ||
|
||
/*��ʾ����X*/ | ||
printf("\n\n����X��\n"); | ||
for(i=0; i<n; i++) | ||
{ | ||
printf("x%d = %0.3f\n", i+1,x[i]); | ||
} | ||
} | ||
|
||
|
||
|
||
?3.�ſ˱ȵ����� | ||
|
||
#include"stdio.h" | ||
#include"math.h" | ||
#define N 3 | ||
#define M 27 | ||
void main() | ||
{ | ||
int i,j,k=1; | ||
float a[N][N],b[N],sum,x[N]; | ||
printf("�밴������ԭ�������\n"); | ||
for(i=0;i<N;i++) | ||
{ | ||
for(j=0;j<N;j++) | ||
{ | ||
scanf("%f",&a[i][j]); | ||
} | ||
scanf("%f",&b[i]); | ||
} | ||
printf("��������������Ϊ:\n"); | ||
for(i=0;i<N;i++) | ||
{ | ||
for(j=0;j<N;j++) | ||
{ | ||
printf("%f ",a[i][j]); | ||
} | ||
printf("%f \n",b[i]); | ||
} | ||
|
||
for(i=0;i<N;i++) | ||
{ | ||
x[i]=0; | ||
} | ||
while(k<=M) | ||
{ | ||
printf("��%d�ε����Ľ��Ϊ��\n",k++); | ||
for(i=0;i<N;i++) | ||
{ | ||
sum=0; | ||
for(j=0;j<N;j++) | ||
{ | ||
sum=sum+a[i][j]*x[j]; | ||
} | ||
x[i]=x[i]+(b[i]-sum)/a[i][i]; | ||
printf("x(%d)=%f\n",i+1,x[i]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|