I created a program which outputs a y coordinate based on a value of an x.
Here is my code.
double xmax,xmin,xspac;
cout << " Input a value of minimum value for x: " << endl;
cin >> xmin;
cout << " Input a value of maximum value for x: " << endl;
cin >> xmax;
cout << " Input a value of interval spacing: " << endl;
cin >> xspac;
if(xmin < myvector[0].x || xmax > myvector[n_points-1].x)
{
cout << " Error, outside the bounds of the given points " << endl;
}
else{
for (xmin; xmin <= xmax; xmin = xmin+xspac){
if (xmin >= myvector[0].x && xmin <= myvector[1].x)
{
int ctr = 0;
cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
if (xmin > myvector[1].x && xmin <= myvector[2].x)
{int ctr = 1;
cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
if (xmin > myvector[2].x && xmin <= myvector[3].x)
{int ctr = 2;
cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
}
}
The function is in the form f(h) = y + b(h-x) + c(h-x)^2 + d(h-x)^3. However the values for x,y,b,c,d are stored in a vector so in the code above i accessed the elements one by one. Is there a way to compress the code so that the "If" statement loops.