我正在编写一个模拟GPS系统的程序。我在数据中进行了硬编码,因此我可以在Visual Studio中进行调试。我实际上将在终端中运行该程序,以便管道输入数据。问题出在do-while循环中。我试图迭代时间,直到满足条件“检查> 0.01c”,其中c是光速。卫星的位置和时间xs和ts分别交织在一起。我不断用新的,更准确的t值重新计算xs。第二次通过,tnew不会改变。它是第一次这样做,所以我能想出的唯一原因就是它的变化是如此之小以至于它无关紧要。任何帮助都会很棒,我一直在调试这几个小时。我只包括围绕do-while循环的代码,我认为是问题:
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
// Go through each satellite and calculate ts and xs
for (int is = 0; is<24; is++)
{
// Compute ts and xs
double Initialxs[3];
for (int kk = 0; kk<3; kk++)
{
Initialxs[kk] = (R + h)*(u[is][kk] * cos(((2.0 * pi*tv) / (s / 2.0)) + ph[is][0]) + v[is][kk] * sin(((2.0 * pi*tv) / (s / 2.0)) + ph[is][0]));
}
double told = tv;
double tnew = tv - (sqrt((Initialxs[0] - xv[0])*(Initialxs[0] - xv[0]) + (Initialxs[1] - xv[1])*(Initialxs[1] - xv[1]) + (Initialxs[2] - xv[2])*(Initialxs[2] - xv[2]))) / c;
double check = abs(tnew - told);
double xs[3];
do {
told = tnew;
for (int ii = 0; ii < 3; ii++)
{
xs[ii] = (R + h)*(u[is][ii] * cos(2 * pi*told / (s / 2) + ph[is][0]) + v[is][ii] * sin(2 * pi*told / (s / 2) + ph[is][0]));
}
tnew = tv - (sqrt((xs[0] - xv[0])*(xs[0] - xv[0]) + (xs[1] - xv[1])*(xs[1] - xv[1]) + (xs[2] - xv[2])*(xs[2] - xv[2]))) / c;
check = abs(tnew - told);
cout << check << endl;
} while (check > (0.01 / c));
double ts = tnew;
return 0;
}