首页 > 解决方案 > 第二个 for 循环我哪里出错了

问题描述

我正在尝试根据他在一本书中的方程式创建一个泰勒加权光束模式。一切都正确计算到某个点,但重新使用泰勒德诺姆的值而不是重新计算。这就是我所拥有的:

#include <math.h>
#include <vector>
#include <iostream>
#include <cmath>


using namespace std;

int main ()
{
double sidelobeRatio, nbar, zeros, wave, length;

cout << "Please enter side lobe ratio, nbar, number of zeros less than or equal to nbar, the wavelength and length of the array." << endl;

cin >> sidelobeRatio >> nbar >> zeros >> wave >> length;

vector<double> DbValues;

double taylorA = 1/M_PI * acosh(pow(10,sidelobeRatio/20));  //Calculates the value of A for sigma and Zn (step 1b)

double sigma = nbar/pow(pow(taylorA,2)+pow((nbar-0.5),2),0.5);  //Calculates the sigma constant for taylor weighting (step 2)

double Zn = sigma * pow(pow(taylorA,2)+pow((zeros-.5),2),(0.5));  //The Zn constant for taylor weighting (step 3)

for (double u=-90; u<=90; u=u + 45)
    {
        double z = (sin(u*M_PI/180)*length) / wave;  //Calculates z for taylor weighting constant (step 1a)

        double taylorSine = sin(M_PI*z)/(M_PI*z);  //The sinc function for beamforming (step 4a)
        cout << taylorSine << " this is taylor sine" << endl;

        double taylorNumerator = 1-pow((z/Zn),2); //The numerator for the taylor weight constant (step 4b)
        cout << taylorNumerator << " this is numerator" << endl;

        double newValue = 1;

        vector<double> newValues;

        for (int zeros=1; zeros <= nbar-1; zeros++)
            {
                double taylorDenom = 1-pow((z/zeros),2);  //The denomenator for the taylor weight constant (step 4c)

                double taylorValue = taylorSine*(taylorNumerator/taylorDenom);  //The taylor equation with capital pi operand (step 5)

                newValue *= taylorValue;

                newValues.push_back(newValue);

                cout << taylorValue << " this is taylorValue" << endl;
            }
        int i;
        for (i=0; i < newValues.size(); i++)
            {
                double convert = 10*log10(pow(newValues[i],2));  //converts F from linear to Db (step 6)

                DbValues.push_back(convert);
            }
        double j;
        for (j=0; j < DbValues.size(); j++)
            {
             cout << DbValues[j] <<endl;
            }
         cout << DbValues.size() << " this is DbValue" << endl;
         cout << newValues.size() << " this is newValue" << endl;
     }
return 0;
};

输出:

         -3.89817e-17 this is taylor sine
         -3 this is numerator
         -1.18126e-18 this is taylorValue
         -4.87271e-18 this is taylorValue
         -1.1566e-17 this is taylorValue
         -2.22753e-17 this is taylorValue
         -358.553
         -704.798
         -1043.53
         -1376.58
         4 this is DbValue
         4 this is newValue
         -nan this is taylor sine
         1 this is numerator
         -nan this is taylorValue
         -nan this is taylorValue
         -nan this is taylorValue
         -nan this is taylorValue
         -358.553
         -704.798
         -1043.53
         -1376.58
         -nan
         -nan
         -nan
         -nan
         8 this is DbValue
         4 this is newValue
        -3.89817e-17 this is taylor sine
        -3 this is numerator
        -1.18126e-18 this is taylorValue
        -4.87271e-18 this is taylorValue
        -1.1566e-17 this is taylorValue
        -2.22753e-17 this is taylorValue
        -358.553
        -704.798
        -1043.53
        -1376.58
        -nan
        -nan
        -nan
        -nan
        -358.553
        -704.798
        -1043.53
        -1376.58
        12 this is DbValue
        4 this is newValue

预期输出: 在此处输入图像描述

对于计算出的每个新 z,需要计算一个新的 taylor denom,但由于某种原因,我一遍又一遍地得到相同的 taylor denom。如果有什么需要进一步澄清,请告诉我。

澄清:当 u = 90 度时,如果用户输入 nbar 为 5,则需要计算 5 次 taylor denom,其中 z 和 n 从 1 变为 nbar-1。这没有发生,也许需要引入一个while循环?

我已经打印了 sigma、Zn 和 taylorA 并且计算正确。最后打印出来的尺寸总是正确的。它们中的值不是。

输入: 比率:30 nbar:5 个零点:5 波长:0.1 长度:1

标签: c++

解决方案


删除int i;并将其放入循环中。

    for (int i=0; i < newValues.size(); i++)
        {
            double convert = 10*log10(pow(newValues[i],2));  //converts F from linear to Db (step 6)

            DbValues.push_back(convert);
        }

推荐阅读