简体   繁体   English

我正在尝试在 for 循环中添加向量,但在 2 次迭代后我得到了错误的答案 (C)

[英]I am trying to add vectors in a for loop, but after 2 iterations I get an incorrect answer (C)

I am trying to implement the iterative formula Xn+1 = T*Xn + C, for a given number of iterations i.对于给定的迭代次数 i,我正在尝试实现迭代公式 Xn+1 = T*Xn + C。 Here X and C are vectors and T is a 3x3 matrix.这里 X 和 C 是向量,T 是一个 3x3 矩阵。 However, if I go beyond 2 iterations (fori=0;i<2;i++) the formula starts giving an incorrect result and I am not sure why.但是,如果我超过 2 次迭代 (fori=0;i<2;i++),公式开始给出不正确的结果,我不知道为什么。 Any help is appreciated.任何帮助表示赞赏。

#include <stdio.h>
int main(void) {
  double T[3][3] = {{0.0,-0.5,-0.75},
                   {0.6,0.0,0.4},
                   {0.25,-0.375,0.0}};

  double C[3] = {2.0,2.8,3.375};
  double x_n[3] = {0.0,0.0,0.0};
  double x_n_plus_1[3] = {0.0,0.0,0.0};
  double Tx[3] = {0.0,0.0,0.0};
  int i, row, col, ctr;
  for(i=0;i<3;i++) {
    for(row=0;row<3;row++) {
      for(col=0;col<3;col++){
        Tx[row] += T[row][col] * x_n[col]; //Tx_n = T*x_n
      }
    }
    for(ctr=0;ctr<3;ctr++) {
      x_n_plus_1[ctr] = Tx[ctr] + C[ctr];//x_n+1 = Tx_n + C
    }
    for(ctr=0;ctr<3;ctr++) {
      x_n[ctr] = x_n_plus_1[ctr];
    }
  }
  for(ctr=0;ctr<3;ctr++) {
    printf("%lf\n",x_n[ctr]);
  }
  return 0;
}

As pmg proposed: "You need to reset the elements of Tx to zero at the beginning (or end) of each i loop".正如pmg建议的那样:“您需要在每个 i 循环的开始(或结束)将 Tx 的元素重置为零”。 For each iteration of i, you need to re-initialize the Tx.对于 i 的每次迭代,您都需要重新初始化 Tx。 If you go beyond 2 iterations (fori=0;i<2;i++) the formula starts giving an incorrect result because, staring second iteration, Tx is not still equal to {0, 0, 0}如果超过 2 次迭代 (fori=0;i<2;i++),则公式开始给出错误结果,因为在第二次迭代时,Tx 仍不等于 {0, 0, 0}

#include <stdio.h>
int main(void) {
  double T[3][3] = {{0.0,-0.5,-0.75},
                   {0.6,0.0,0.4},
                   {0.25,-0.375,0.0}};

  double C[3] = {2.0,2.8,3.375};
  double x_n[3] = {0.0,0.0,0.0};
  double x_n_plus_1[3] = {0.0,0.0,0.0};

  int i, row, col, ctr;
  for(i=0;i<3;i++) {
    double Tx[3] = {0.0, 0.0, 0.0};
    for(row=0;row<3;row++) {
      for(col=0;col<3;col++){
        Tx[row] += T[row][col] * x_n[col]; //Tx_n = T*x_n
      }
    }
    for(ctr=0;ctr<3;ctr++) {
      x_n_plus_1[ctr] = Tx[ctr] + C[ctr];//x_n+1 = Tx_n + C
    }
    for(ctr=0;ctr<3;ctr++) {
      x_n[ctr] = x_n_plus_1[ctr];
    }
  }
  for(ctr=0;ctr<3;ctr++) {
    printf("%lf\n",x_n[ctr]);
  }
  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C程序中答案不正确,我缺少什么? - Incorrect answer in C program, what I am missing? 请帮忙。 我正在尝试从带有空格的文件中获取输入,但我一直在无限循环。 C编程 - Please help! I am trying to get input from a file with spaces I keep getting infinite loop. C programming 在 C 的“for”循环中添加两个条件后,出现错误 - After adding two conditions in a "for" loop in C I am getting errors 即使按y或no后,我也无法退出此循环 - I am not able to get out of this loop even after pressing y or no 我想在C语言中获得双倍答案 - I wanted to get answer in double in C 我无法使用 for 循环在这个 c 程序中获得基本概念 - I am unable to get the basic concept in this c program using for loop 我正在尝试在 C 中使用 scanf in loop 和 outside loop 输入三个字符,但没有一个正常工作 - I am trying to input three characters using scanf in loop and also outside loop in C but none is working properly 我正在尝试打印排序 function 的迭代,它似乎工作但有一些额外的数字 - I am trying to print iterations of the sorting function, it seems to work but there are some extra numbers 我正在尝试编写欧几里得算法,但是当我在C代码中使用循环时程序崩溃 - i am trying to write Euclidean Algorithm but program crash when i use loop in c code 在 C/C++ 中,我试图在没有 if-else 阶梯的 for 循环中调用不同的函数(在描述中解释) - In C/C++, I am trying to call different functions (explained in description) in a for loop without if-else ladder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM