Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

c++ - The code is for to plot naca airfoil. I received the out of bound error when printing yc

This code uses for loop to find values of yc by putting value of x in the equation.The error is "error: Mat::operator(): index out of bounds terminate called after throwing an instance of 'std::logic_error' what(): Mat::operator(): index out of bounds".

#include<iostream>
#include<armadillo>
#include<vector>

using namespace std;
using namespace arma;

int main()
{

float a[3];
float c;
int gp = 100;

cout << "Please Enter NACA 4 digits" << endl;
cout << "Please Enter 1st digit" << endl;
cin >> a[0] ;
cout << "Please Enter 2nd digit" << endl;
cin >> a[1] ;
cout << "Please Enter last 2 digits" << endl;
cin >> a[2] ;
cout << "Please Enter Chord Length" << endl;
cin >> c;

float m=(a[0]*c)/100;
float p=(a[1]*c)/10;
float t=(a[2]*c)/100;

cout << m << endl;
cout << p << endl;
cout << t << endl;

vec x = linspace<vec>(0, c, gp);
vec yc = linspace<vec>(0, 1, gp);

for(int i=0;i<=100;i=i+1)
    {
        if (x(i)=0 && x(i) <= p){
        cout<< x(i)<<endl;
        yc(i)= (m/(p*p))*(2*p*(x(i))-(x(i)*x(i)));
    }
        else {
        yc(i)=(m/((1-p)*(1-p)))*((1-(2*p))+(2*p*x(i))-(x(i)*x(i)));
    }
    }
cout<<yc<<endl;
return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

linspace<vec> is most likely a zero based array. With 100 elements, the last index is 99.

Therefore, replace

for(int i=0;i<=100;i=i+1)

with

for(int i=0;i<100;++i)

I've replaced i=i+1 as I find that notation unbearable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...