我有以下问题:我有一个矩阵 nxn(一个多数组 A[n][n])和一个向量 b[i],我用它在这两个数组之间做一行到列的乘积并生成一个新数组c[i]。
代码的第一部分工作正常:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void){
double A[4][4]={{0,0,1,0.5},{0.33,0,0,0},{0.33,0.5,0,0.5},{0.33,0.5,0,0}};
double b[4]={0.25,0.25,0.25,0.25};
double c[4]={0.,0.,0.,0.};
int n,i,j;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
c[i] += A[i][j]*b[j];
}
}
for(i=0;i<4;i++){
printf("c[%d]=%lf\n",i,c[i]);
}
printf("\n");
}
通过这种方式,我将获得一个 4 维向量,如:
c[0]=0.375
c[1]=0.082
c[2]=0.332
c[3]=0.207
现在的问题是我想迭代程序直到向量 b[i] 的分量和向量 c[i] 的分量之间的差异最大为 0.001。做迭代我想做一个做/当,但我不知道如何进行迭代。我将展示我的想法:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void){
double A[4][4]={{0,0,1,0.5},{0.33,0,0,0},{0.33,0.5,0,0.5},{0.33,0.5,0,0}};
double b[4]={0.25,0.25,0.25,0.25};
double c[4]={0.,0.,0.,0.};
int n,i,j;
do{
b[i]==c[i]; /* new add, but i don t know if it is the right place to put it..*/
for(i=0;i<4;i++){
for(j=0;j<4;j++){
c[i] += A[i][j]*b[j];
}
}
}while(abs(b[i]-c[i])<0.001); /*the condition into the while should be correct*/
for(i=0;i<4;i++){
printf("c[%d]=%lf\n",i,c[i]);
}
printf("\n");
}
结果应该是:
c[0]=0.38
c[1]=0.12
c[2]=0.29
c[3]=0.19
但它没有......我该怎么办?