r/computerscience • • Jan 11 '21

Dead simple point interpolation with C++

401 Upvotes

15 comments sorted by

19

u/longuyen2306 Jan 11 '21

The idea is simple, you can enter as many points as you want and you also have the option of how "powerful" your approximating function should be. I think the principle is well-known if you have a linear algebra course taken, so I will not spend too many words :)

Implemented with good old C++. Link: https://github.com/longmakesstuff/Graph-Creator

20

u/crimson1206 Jan 11 '21

Nice project.

Maybe add a disclaimer that it’s a least squares best fit if the degree doesn’t match number of points + 1.

9

u/NanoAlpaca Jan 12 '21

There seems to be some numerical instability or implementation bug with higher polynomial orders. The fit for orders 3 to 5 was much better than the fit at order 10+. You might get overfitting and lots of strange wiggles with high order polynomials, but the values at points should get closer to the curve with increasing order, otherwise you could just set the higher order coefficients to zero.

1

u/crimson1206 Jan 12 '21

I think op already answered somewhere that this issue was due to overflow.

1

u/NanoAlpaca Jan 12 '21

Overflow? Wouldn't this internally use float or double? Should be pretty hard to overflow that. Maybe it would help to scale everything to -1 to 1, solve for the coefficients and then scale back the coefficients afterwards to real value range?

1

u/crimson1206 Jan 12 '21 edited Jan 12 '21

Yeah that explanation surprised me too. I didn’t look at the implementation so there might be some other issue.

Edit: Had a quick look at the code and OP uses an inverse to solve the linear system of equations. I'm not sure how stable eigens implementation is but that might be an issue.

1

u/Ullerkk Aug 17 '24

The vandermonde matrix is basically a nightmare in terms of numerical stability & overflow issues.

3

u/minto7 Jan 11 '21

Great project! Can you share some resources to learn development using C++! I do competitive in C++, but never got a chance to actually create an application using it. How should I start? Any suggestions will be helpful

11

u/longuyen2306 Jan 11 '21

https://www.learncpp.com/ is the to go I think. If you want to get really proficient with C pointer, try out learn C the hard way.

To stick with C++ (which can be tough and boring at first) you need to have a goal of what you would like to do with the language (the possibility is limitless). If you want to do some GUI application, try out SFML (there are tons of tutorial on youtube). The library is very well made, easy to learn and involve very little pointer usage. GLFW is another alternative which does almost the same. If you are interested in computer graphic, try out after a few projects the framework OpenGL. If you are more interested in desktop application, take a look at Qt :)

Good luck.

Also I would recommend my favorite youtuber Muzkaw: https://www.youtube.com/channel/UCn4hK5NexKlmMAd3BMEHlmQ

2

u/minto7 Jan 11 '21

Thanks man 👍🏼

1

u/Yologamer1084 Jan 12 '21

damn, that's nice

1

u/[deleted] Jan 12 '21

I loved it, thanks for sharing.

1

u/nickgn12 Jan 12 '21

Very cool! I notice that the solver starts getting unstable for large polynomial order, I’m guessing if you’re using a monomial basis in your vandermonde (1, x, x2, x3, etc.) the columns will start looking almost linearly dependent of one another and this gives difficulties in a linear solve. I’d be interested to see if using Chebyshev polynomials would help at all here? You would just have to change out the matrix columns.

1

u/DrRetr0 Jan 13 '21

How im a new to c++ can someone help me