追趕法求解方程組(C語言)
阿新 • • 發佈:2019-01-29
編寫用追趕法解三對角線方程組的程式,並解下列方程組:
#include<stdio.h>
#include<math.h>
void main()
{
int i;
int n=4;
float x[4];
float c[4][5]={2,-1,0,0,5,
-1,2,-1,0,-12,
0,-1,2,-1,11,
0,0,-1,2,-1};
float y[10];
float d[10][11]={-4,1 ,0,0,0,0,0,0,0,0,-27,
1,-4,1,0,0,0,0,0,0,0,-15,
0,1,-4,1,0,0,0,0,0,0,-15,
0,0,1,-4,1,0,0,0,0,0,-15,
0,0,0,1,-4,1,0,0,0,0,-15,
0,0,0,0,1,-4,1,0,0,0,-15,
0,0,0,0,0,1,-4,1,0,0,-15,
0,0,0,0,0,0,1,-4,1,0,-15,
0 ,0,0,0,0,0,0,1,-4,1,-15,
0,0,0,0,0,0,0,0,1,-4,-15};
void zhui(float *,int,float[]);
zhui(c[0],4,x);
zhui(d[0],10,y);
for(i=0;i<=3;i++)printf("x[%d]=%f\n",i+1,x[i]);
printf("\n");
for(i=0;i<=9;i++)printf("x[%d]=%f\n",i+1,y[i]);
}
//追趕法
void zhui(float *u,int n,float x[])
{
int i,j;
float p;
for(i=1;i<=n-1;i++)
{
p=*(u+i*(n+1)+i-1)/(*(u+(i-1)*(n+1)+i-1));
*(u+i*(n+1)+i-1)=0;
*(u+i*(n+1)+i)-=p*(*(u+(i-1)*(n+1)+i));
*(u+i*(n+1)+n)-=p*(*(u+(i-1)*(n+1)+n));
}
x[n-1]=(*(u+(n-1)*(n+1)+n))/(*(u+(n-1)*(n+1)+n-1));
for(j=n-2;j>=0;j--)
{
x[j]=((*(u+j*(n+1)+n))-x[j+1]*(*(u+j*(n+1)+j+1)))/(*(u+j*(n+1)+j));
}
}