I'm having some trouble outputting a 2D vector as a grid in the Terminal.
What I've done is create a class Grid
that creates a grid (in this case, a square grid), by creating a set of x and y co-ordinates whose spacing is dx
and dy
. I can then set the domain, by choosing maximum and minimum values for x and y. I then want to create a value V(x,y) which is a function of x,y[j].
Maybe some code will give a better idea of what I'm trying...
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Grid
{
// min/max value of square grid
double xmin,xmax,ymin,ymax;
public:
//x,y coordinates
vector<double> x,y;
//value V(x,y) = v(xi,yj) = v[i][j]
vector< vector<double> >v;
//grid spaces
double dx,dy;
// change grid size
void changeGridDomain(double xmin_,double xmax_,double ymin_,double ymax_)
{
xmin=xmin_;
xmax=xmax_;
ymin=ymin_;
ymax=ymax_;
}
// change grid storage
void changeGridStorage(int n,int m)
{
x.resize(n);
y.resize(m);
for (int k=0;k<=m;k++)
v.resize(n,vector<double>(k,0));
}
//update grid
void updateGrid()
{
dx=(xmax-xmin)/(x.size()-1);
dy=(ymax-ymin)/(y.size()-1);
for(int i=0; i<x.size();i++)
{
x[i]=xmin+i*dx;
}
for(int j=0;j<y.size();j++)
{
y[j]=ymin+j*dy;
}
}
void GridFunction()
{
for(int i=0; i<x.size(); i++)
for(int j=0; j<y.size(); j++)
{
//V(x_i,y_j)
v[i][j]=x[i]*y[j];
cout << v[i][j] << "\n";
}
}
};
int main() {
Grid g;
//create an 11x11 grid
g.changeGridStorage(11,11);
//fix the max and min values of x and y to be 0 and 1
g.changeGridDomain(0,1,0,1);
g.updateGrid();
//implemet the function v
g.GridFunction();
return (EXIT_SUCCESS);
}
Here, I've simply got my function, V, to be the product of the x and y co-ordinates.
The problem is, the output is just a line of 100 values corresponding to x_i*y_j. What I want is a grid, so that I can maybe do a surface plot.
Any ideas on how I'd do this?