View Full Version : Spline interpolation

10th November 2013, 19:36
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.

QCPCurve *newCurve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
newCurve->setName("Fermat's Spiral");
newCurve->setData(x, x, y);

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.

kiek = 0;

//limitas is just counter which counted how many times user typed values

// user typed x values and calculated values are stored in double type array

QVector<double> x(limitas), y(limitas);

for(int z= 0; z<limitas; z++){

x[z] = iksai[kiek];
y[z] = d[kiek];

ui->customPlot->graph(0)->setData(x, y);

max = *std::max_element(d, d + limitas);

max1 = *std::max_element(iksai, iksai + limitas);

min1 = *std::min_element(d, d + limitas);

min = *std::min_element(iksai, iksai + limitas);

ui->customPlot->xAxis->setRange(min, max1);
ui->customPlot->yAxis->setRange(min1, max);


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

10th November 2013, 22:53
(...) 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 ;)