1

For example, in Visual Studio there is tChart and its Series property, which is responsible for drawing lines of the graph. Here is an example of this code

for (int j = 1; j < Nt - 1; j++)
  {
   for (int i = 1; i < Nt - 1; i++)
    {
       chart2->Series["" + (j + 1).ToString()]->Points->AddXY(i, wht[j][i]);
    }
  }

And draw this graph with a lot of lines.

enter image description here

But my task is transfering in Qt Creator(because in Qt Creator may making a lot of opportunities) This code

void MainWindow::drawdifnet(int Nt)
{
    int N=Nt;
    int N1=pow(N,2);
    QVector<double> x22(N), y22(N1); 
    int ii=0,jj=0;
    for (int j = 0; j < Nt ; j++)
            {
                for (int i = 0; i < Nt ; i++)
                {          
                    x22[jj]=i;
                    y22[ii]=wht[j][i];
                    ui->widget_2->addGraph();
                    ui->widget_2->graph(0)->setData(x22,y22);
                  ii++;
                }
                jj++;
    }
    ui->widget_2->xAxis->setLabel("OsX");
    ui->widget_2->yAxis->setLabel("OsY");
    ui->widget_2->xAxis->setRange(30,30);
    ui->widget_2->replot();
}

Doesn't work correctly. Result is the empty widget

In first I with help debugger check the QVectors data In this pictures see that my dinamic array wht[j][i] in work and loaded in QVector yy[ii]

enter image description here enter image description here

I think the problem in the loop.

In QtCustomPlot tutorial this problem solving this code

ui->widget_2->graph(0)->setData(x,y);
ui->widget_2->graph(1)->setData(x11,y11);
ui->widget_2->graph(2)->setData(x22,y22);

But in my situation the quantity of lines is know when the program working.

How I create and assigned my array

void created(int Nt, int Nx) ///This function creating my dynamic array
{
    wht = new double *[Nt];
    for (int i = 0; i < Nt; i++)
        wht[i] = new double[Nx];
}

inline double fn(int T, double x) ///these 4 functions for my mathematical part(works good)
{
    if (x >= 0)
        return T;
    return 0;
}

inline double u0(int T, double x)
{
    return fn(T, x);
}

inline double u1(int T, double a, int xmin, double t)
{
    return fn(T, xmin - a * t);
}

inline double u2(int T, double a, int xmax, double t)
{
    return fn(T, xmax - a * t);
}


void calculatedifnet(int xmin, double hx, double ht, double a, int Nx, int Nt, int T)
//These main function.We have the empty array and in this function we fill array. Then we solve in the main loop and the fill the first indexes wht[j]
{
    for (int i = 0; i < Nt; i++)
    {
        wht[0][i] = u0(T, xmin + i*hx);//fill the second indexeswht[null][i]
    }

    for (int j = 0; j < Nt - 1; j++)//the calculated code(works right).The result writing in wht[j]
    {
        wht[j + 1][0] = u1(T, a, xmin, j*ht);
        for (int i = 1; i < Nt; i++)
        {
            double dudx = (wht[j][i] - wht[j][i - 1]) / hx;
            wht[j + 1][i] = -a * dudx * ht + wht[j][i];
        }
    }
} 
8
  • Why one of your vector is size N and the other N**2=NxN? Commented Aug 13, 2017 at 5:01
  • Becase the ln iteration j calculate 12 points. Every step j includes 12 steps calculation the coordinates. 12 graphs = 12 points in every graphs and so there are a different sizes. Commented Aug 13, 2017 at 5:08
  • What is the data type of wht? Commented Aug 13, 2017 at 5:12
  • @eyllanesc wht dinamic array of double Commented Aug 13, 2017 at 8:31
  • Could you show how you have declared and assigned it? Commented Aug 13, 2017 at 12:52

1 Answer 1

3

In your code there are the following errors:

  • If we observe x is a constant vector from 0 to Nt-1, then we only have to create it once:

QVector<double> x(Nt);
for (int i = 0; i < Nt ; i++)
    x[i]=i;//0 to Nt-1
  • addGraph() adds a graph and places it in the last position, if you want to graph you must access by the last index, not by the index 0:

ui->widget_2->addGraph()->setData(xx, yy);
  • Assuming that wht is of type QVector<QVector<double>> and of size NtxNt, then is not necessary to access each element, we can access each QVector<double> since the function setData() accepts as input this type of data. To the function setData() you must pass 2 vectors of the same size, but you were passing 2 vectors of Nt and Nt*Nt, this generated a warning:

ui->widget_2->addGraph()->setData(x, wht[j]);
  • setRange() places the range from a to b, but if they are the same QCustomPlot will never fit the range, for my test I set it as follows:

ui->widget_2->xAxis->setRange(0,Nt);
ui->widget_2->yAxis->setRange(0,Nt*Nt);

In short the code would be as follows:

void MainWindow::drawdifnet(int Nt){

    QVector<double> x(Nt);
    for (int i = 0; i < Nt ; i++)
        x[i]=i;//0 to Nt-1

    for (int j = 0; j < Nt ; j++)
        ui->widget_2->addGraph()->setData(x, wht[j]);

    /* if c++11
    for (auto& row: wht)
        ui->widget_2->addGraph()->setData(x, row);
    */

    ui->widget_2->xAxis->setLabel("OsX");
    ui->widget_2->yAxis->setLabel("OsY");
    ui->widget_2->xAxis->setRange(0,Nt);
    ui->widget_2->yAxis->setRange(0,Nt*Nt);
    ui->widget_2->replot();

}

Output:

enter image description here

Note: For the test wht[i][j] = i*j

In your case wht is a variable of type double**, also assume that Nx>=Nt, for this you must use the following code:

void MainWindow::drawdifnet(int Nt)
{
    QVector<double> x(Nt);
    for (int i = 0; i < Nt ; i++){
        x[i]=i;//0 to Nt-1
    }

    QVector<double> y(Nt);
    for(int i=0; i<Nt; i++){
        for(int j=0; j<Nt; j++){
            y[j] = wht[i][j];
        }
        ui->widget_2->addGraph()->setData(x, y);
    }

    ui->widget_2->xAxis->setLabel("OsX");
    ui->widget_2->yAxis->setLabel("OsY");
    ui->widget_2->xAxis->setRange(0,12);
    ui->widget_2->yAxis->setRange(0,3.5);
    ui->widget_2->replot();
}

Input:

  • created(12, 12);
  • calculatedifnet(1, .5, .5, 0.9, 12, 12, 3);

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

In this code error ui->widget_2->addGraph()->setData(x, wht[j]); no matching function for call to 'QCPGraph::setData(QVector<double>&, double*&)' ui->widget_2->addGraph()->setData(x, wht[j]);
Note: For the test wht[i][j] = ij it means that I write ui->widget_2->addGraph()->setData(x, ij);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.