I'm trying to write code to find A
in a system of linear equations Ax=B
, so I used LU decomposition. Now that I have L and U properly, I'm stuck in the forward substitution in order to get the y in B=Ly.
我正在尝试编写代码来在线性方程组Ax = B中找到A,所以我使用了LU分解。现在我已经正确地使用了L和U,为了得到B = Ly中的y,我陷入了前向替换。
I wrote some code in MatLab that works perfectly, but I can't get the same results rewriting the code in C. So I was wondering if someone may know what i'm doing wrong, I'm not fully used to C.
我在MatLab中编写了一些完美的代码,但是我无法在C中重写相同的结果。所以我想知道是否有人可能知道我做错了什么,我还没有完全习惯C.
Here's my code in MatLab:
这是我在MatLab中的代码:
y(1,1) = B(1,1)/L(1,1);
for i= 2:n
sum=0;
sum2=0;
for k = 1: i-1
sum = sum + L(i,k)*y(k,1);
end
y(i,1)=(B(i,1)-sum)/L(i,i);
end
where L
is my lower triangle matrix, B
is a vector of the same size, and n
is 2498 in this case.
其中L是我的下三角矩阵,B是相同大小的向量,在这种情况下n是2498。
My C code is the following:
我的C代码如下:
float sum = 0;
y_prev[0]=B[0]/(float)Low[0][0];
for (int i = 1; i < CONST; i++)
{
for (int k = 0; k < i-1; k++)
{
sum = sum +Low[i][k]*y_prev[k];
}
y_prev[i]= (B[i]- sum)/(float)Low[i][i];
}
1 个解决方案
#1
2
One difference between the codes comes from the way you've changed the for loop indices to work with the zero based indexing in C. (I can't run the MATLAB version, and don't have some of the context for the code, so there may be other differences.)
代码之间的一个区别来自于您将for循环索引更改为使用C中基于零的索引的方式。(我无法运行MATLAB版本,并且没有代码的某些上下文,所以可能还有其他差异。)
The variables i
and k
have values which are smaller by 1 in the C code. This is exactly what you want for the loop indices, but a problem arises when you use i
to control the number of iterations in the inner loop over k
. This is i-1
in both versions of the code, even though i
has different values. For instance, in the first iteration of the outer loop the inner loop runs once in the MATLAB code but not at all in the C one.
变量i和k具有在C代码中小1的值。这正是您想要的循环索引,但是当您使用i来控制内部循环中的迭代次数超过k时会出现问题。这两个版本的代码都是i-1,即使我有不同的值。例如,在外循环的第一次迭代中,内循环在MATLAB代码中运行一次,但在C循环中根本不运行。
A possible fix would be to rewrite the inner loop in the C code as
一个可能的解决方法是重写C代码中的内部循环
for (int k = 0; k < i; k++)
{
sum = sum +Low[i][k]*y_prev[k];
}
A second difference is that you're resetting sum
to zero in the MATLAB code but not in the C (the MATLAB code also has a sum2
which doesn't seem to be used?). This will cause differences in y_prev[i]
for i>0
.
第二个区别是你在MATLAB代码中将sum重置为零,而不是在C中(在MATLAB代码中也有一个似乎没有使用的sum2?)。对于i> 0,这将导致y_prev [i]的差异。
#1
2
One difference between the codes comes from the way you've changed the for loop indices to work with the zero based indexing in C. (I can't run the MATLAB version, and don't have some of the context for the code, so there may be other differences.)
代码之间的一个区别来自于您将for循环索引更改为使用C中基于零的索引的方式。(我无法运行MATLAB版本,并且没有代码的某些上下文,所以可能还有其他差异。)
The variables i
and k
have values which are smaller by 1 in the C code. This is exactly what you want for the loop indices, but a problem arises when you use i
to control the number of iterations in the inner loop over k
. This is i-1
in both versions of the code, even though i
has different values. For instance, in the first iteration of the outer loop the inner loop runs once in the MATLAB code but not at all in the C one.
变量i和k具有在C代码中小1的值。这正是您想要的循环索引,但是当您使用i来控制内部循环中的迭代次数超过k时会出现问题。这两个版本的代码都是i-1,即使我有不同的值。例如,在外循环的第一次迭代中,内循环在MATLAB代码中运行一次,但在C循环中根本不运行。
A possible fix would be to rewrite the inner loop in the C code as
一个可能的解决方法是重写C代码中的内部循环
for (int k = 0; k < i; k++)
{
sum = sum +Low[i][k]*y_prev[k];
}
A second difference is that you're resetting sum
to zero in the MATLAB code but not in the C (the MATLAB code also has a sum2
which doesn't seem to be used?). This will cause differences in y_prev[i]
for i>0
.
第二个区别是你在MATLAB代码中将sum重置为零,而不是在C中(在MATLAB代码中也有一个似乎没有使用的sum2?)。对于i> 0,这将导致y_prev [i]的差异。