[英]Why is the b matrix not getting printed?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.14159265359
#define TINY 0.000000001;
double LUBKSB(double *b, double **a, int N, int *indx)
{
int i, ii, ip, j;
double sum;
ii = 0;
for(i=0;i<=N;i++)
{
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii)
{
for(j = ii;j<i;j++)
{
sum = sum - a[i][j] * b[j];
}
}
else if(sum)
{
ii = i;
}
b[i] = sum;
}
for(i=N;i>=0;i--)
{
sum = b[i];
for (j = (i+1); j<=N;j++)
{
sum = sum - a[i][j] * b[j];
}
b[i] = sum/a[i][i];
}
for (i=0;i<N;i++)
{
printf("b[%d]: %lf",i,b[i]);
}
}
double ludecmp(double **a, int N)
{
int i, imax, j, k;
double big, dum, sum, temp, d;
double *vv = (double *) malloc(N * sizeof(double));
int *indx = (int *) malloc(N * sizeof(double));
double *b = (double *) malloc(N * sizeof(double));
d = 1.0;
for (i=0;i<N;i++)
{
printf("Enter b[%d]",i);
scanf("%lf",&b[i]);
}
for(i=0;i<=N;i++)
{
big = 0.0;
for(j=0;j<=N;j++)
{
temp = abs(a[i][j]);
if (temp > big)
{
big = temp;
}
}
if (big == 0.0)
{
printf("Singular matrix\n");
exit;
}
vv[i] = 1.0/big;
}
for(j=0;j<=N;j++)
{
for(i=0;i<j;i++)
{
sum = a[i][j];
for(int k=0;k<i;k++)
{
sum = sum - a[i][k] * a[k][j];
}
a[i][j] = sum;
}
big = 0.0;
for(i=j;i<=N;i++)
{
sum = a[i][j];
for(k=0;k<j;k++)
{
sum = sum - a[i][k] * a[k][j];
}
a[i][j] =sum;
dum = vv[i] * abs(a[i][j]);
if(dum >= big)
{
big = dum;
imax = i;
}
}
if(j != imax)
{
for(k=0;k<=N;k++)
{
dum = a[imax][k];
a[imax][k] = a[j][k];
a[j][k] = dum;
}
d = -d;
vv[imax] = vv[j];
}
indx[j] = imax;
if (a[j][j] == 0)
{
a[j][j] = TINY;
}
if (j != N)
{
dum = 1.0/a[j][j];
for(i = (j+1); i<=N; i++)
{
a[i][j] = a[i][j] * dum;
}
}
}
LUBKSB(b,a,N,indx);
for (i=0;i<N;i++)
{
printf("b[%d]: %lf",i,b[i]);
}
}
int main()
{
int N, i, j;
printf("Enter the number of rows and columns: \n");
scanf("%d",&N);
double **a = (double **) malloc(N * sizeof(double*));
for (i = 0; i < N; i++)
a[i] = (double *) malloc(N * sizeof(double));
printf("Enter the values of matrix\n");
for(i = 0; i < N;i++)
{
for(j = 0;j < N; j++)
{
printf("Enter a[%d][%d]",i,j);
scanf("%lf",&a[i][j]);
}
}
ludecmp(a,N);
}
I am using these algorithms to find LU decomposition of matrix and trying to find solution Ax = b我正在使用这些算法来查找矩阵的 LU 分解并试图找到解决方案 Ax = b
Given a N ×N matrix A denoted as {a}N,Ni,j=1, the routine replaces it by the LU decomposition of a rowwise permutation of itself.给定一个表示为 {a}N,Ni,j=1 的 N ×N 矩阵 A,例程将其替换为其自身的行排列排列的 LU 分解。 “a” and “N” are input.
输入“a”和“N”。 “a” is also output, modified to apply the LU decomposition;
“a”也是 output,修改为应用 LU 分解; {indxi}N i=1 is an output vector that records the row permutation effected by the partial pivoting;
{indxi}N i=1 是一个 output 向量,记录了部分旋转所影响的行排列; “d” is output and adopts ±1 depending on whether the number of row interchanges was even or odd.
“d”是output,根据行交换数是偶数还是奇数采用±1。 This routine is used in combination with algorithm 2 to solve linear equations or invert a matrix.
此例程与算法 2 结合使用,以求解线性方程或矩阵求逆。
Solves the set of N linear equations A. x = b.求解 N 个线性方程组 A. x = b。 Matrix {a} N,N i,j=1 is actually the LU decomposition of the original matrix A, obtained from algorithm 1. Vector {indxi} N i=1 is input as the permutation vector returned by algorithm 1. Vector {bi} N i=1 is input as the righthand side vector B but returns with the solution vector X. Inputs {a} N,N i,j=1, N, and {indxi} N i=1 are not modified in this algorithm.
矩阵{a} N,N i,j=1实际上是算法1得到的原始矩阵A的LU分解。向量{indxi} N i=1作为算法1返回的置换向量输入。向量{bi } N i=1 作为右侧向量 B 输入,但返回解向量 X。输入 {a} N,N i,j=1, N 和 {indxi} N i=1 在此算法中未修改.
There are a number of problems with your code:您的代码存在许多问题:
i <= N
should be i < N
and i = N
should be i = N - 1
.i <= N
应该是i < N
并且i = N
应该是i = N - 1
。fabs
, not abs
. fabs
返回,而不是abs
。exit
should be exit(1)
or exit(EXIT_FALILURE)
.exit
应该是exit(1)
或exit(EXIT_FALILURE)
。return
statement.return
语句。 You should also free the memory you have allocated with the function free
.您还应该释放您分配的 memory 和 function
free
。 When you compile a C program you should also enable all warnings.当您编译 C 程序时,您还应该启用所有警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.