Results 1 to 2 of 2

Thread: Spline interpolation

  1. #1
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Spline interpolation

    I have problem with QCustomPlot libarary. I made program where user inputs formula and x values then program calulates y values and displays them in nice table.

    After that program uses x values and calculated y values to draw quadratic function. But instead of drawing curve it draws sharp lines. Like statistical diagram or somethink like that.



    I tried QCP curve but i cant get it to work

    I added this line of code But it shows total mess.

    Qt Code:
    1. QCPCurve *newCurve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
    2. ui->customPlot->addPlottable(newCurve);
    3. newCurve->setName("Fermat's Spiral");
    4. newCurve->setData(x, x, y);
    To copy to clipboard, switch view to plain text mode 


    I would like to know how i can draw graph with those kind of values and draw it like a curve. smoothly.

    x- -1, -2, 1, 2, 0

    y - -0.5, -0.25, 0.5, 0.25, 0

    to a quadratic function.

    if you need here is code where I draw graph.

    Qt Code:
    1. kiek = 0;
    2.  
    3. //limitas is just counter which counted how many times user typed values
    4.  
    5. // user typed x values and calculated values are stored in double type array
    6.  
    7.  
    8.  
    9. QVector<double> x(limitas), y(limitas);
    10.  
    11.  
    12. for(int z= 0; z<limitas; z++){
    13.  
    14. x[z] = iksai[kiek];
    15. y[z] = d[kiek];
    16. kiek++;
    17.  
    18. }
    19. ui->customPlot->addGraph();
    20. ui->customPlot->graph(0)->setData(x, y);
    21.  
    22. max = *std::max_element(d, d + limitas);
    23.  
    24. max1 = *std::max_element(iksai, iksai + limitas);
    25.  
    26. min1 = *std::min_element(d, d + limitas);
    27.  
    28. min = *std::min_element(iksai, iksai + limitas);
    29.  
    30. ui->customPlot->xAxis->setRange(min, max1);
    31. ui->customPlot->yAxis->setRange(min1, max);
    32.  
    33. ui->customPlot->replot();
    To copy to clipboard, switch view to plain text mode 


    Added after 1 4 minutes:


    Ok i figured out its not related with QcustomPlot its all about spline interpolation. Can somebody explain me more about splines. I can understand them clearly
    Last edited by DomantasB; 10th November 2013 at 19:14.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Spline interpolation

    (...) to a quadratic function.
    For 4 points you don't need a quadratic polynomial. In general, for a given set of N points, there exists a polynomial of order N-1 that pass through all of those points. For example, 1 point requires a polynomial of order 0 - a "straight line". For 2 points, you can use a linear function f(x)=ax+b, polynomial of order 1. For three points you have a parabola f(x)=ax^2+bx+c (order 2) and so on...
    Can somebody explain me more about splines. I can understand them clearly
    Before digging into splines, I think it could be good to understand a simple polynomial interpolation and try to implement it.
    For splines, a simplest spline (a "linear spline") is created by connecting the linear segments between the given points ( often called "knots" ), so it's in fact a variation on polynomial interpolation - you now have a set of first order polynomials connecting the knots. Higher order splines are just higher order polynomials connecting the knots.
    This is a quite vast subject, rather hard to explain in few forum posts.
    I don't want to leave you with nothing, so for a start try to implement a simplest polynomial interpolation. Shouldn't be too hard and may fit your needs - you want the user to type in the values, so I don't think anyone will really want to type in more than 6,7 pairs of (x,y). Polynomial interpolation could be good for a start.
    If you type in sth. like "introduction to polynomial interpolation" or "introduction to splines" in google you should have more info than you need. Assuming that you can hangle the math, of course.
    Good luck

Similar Threads

  1. Plotting graphs in Qt
    By Computer Hater in forum Qt Programming
    Replies: 3
    Last Post: 9th November 2011, 20:26
  2. Replies: 2
    Last Post: 16th August 2011, 18:28
  3. Replies: 4
    Last Post: 4th January 2011, 09:47
  4. Efficiently plotting curves
    By Cruz in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2009, 13:24
  5. Plotting graphs in QGraphicsView using qwt
    By luis_claudio in forum Newbie
    Replies: 0
    Last Post: 14th September 2009, 19:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.