3
For this challenge, when given a list of (x,y) points your submission needs to output a polynomial function that goes through all of them.
For example, if your points were [(0,0), (2,5)]
, you could return y = 2.5*x
or y = x + 1/4x^2
.
- All points passed in will consist only of integers
- Two points will never have the same
y
value - Your function be of the form
y = a*x^n + ... b*x^m
. Reducing of terms is allowed. - Constants (the exponent and coefficient) can either be in decimal or fractional form. Decimals should be accurate to at least 3 decimal places
- You should be able to handle an arbitrary number of points
- Output must be a string starting with "y=". Whitespace anywhere in the string is fine.
Test cases
Note, the given output is not the only valid output, just one of the possibilities.
(1,4) -> y=x+3
(0,0), (1,8) -> y=8x
(0,6), (-1,90) -> y=-84x+6
(6,4), (70,4), (-1,-6) -> y=−10/497x^2+760/497x−316/71
(4,5), (5,6), (6,7) -> y=x+1
(20,1), (-20,1), (0,5), (10, 4), (11,12), (17,4), (2,4) -> y=488137/10424165400x^6−643187/473825700x^5−87561839/10424165400x^4+550999039/1042416540x^3−21590713027/5212082700x^2+300110420/52120827x+5
This is a code-golf, so make your answers as short as possible in your favorite language!
1Your title says something about plotting but the challenge about outputting polynomials. Having the corresponding results for your test cases would probably help clear this up (and make the test cases a lot more useful). – Martin Ender – 2016-08-14T21:42:49.180
1Also how about built-ins? (Mathematica's is
InterpolatingPolynomial
so it's probably not at risk of winning.) – Martin Ender – 2016-08-14T21:47:34.297Builtins are fine. – Nathan Merrill – 2016-08-14T21:49:19.487
The output needs to be a string. The input is flexible – Nathan Merrill – 2016-08-14T22:01:16.723
Can coefficients in the output be (string representations of) floats instead of fractions? Say with 4 decimals? – Luis Mendo – 2016-08-14T22:02:06.157
Does the output need
y=
? Do exponents have to be explicitly listed? – xnor – 2016-08-14T22:02:39.563@LuisMendo yes. – Nathan Merrill – 2016-08-14T22:06:20.453
PS: I think you know about the existence of the sandbox by now :D – flawr – 2016-08-14T22:15:55.943
@flawr I know, I didn't expect so many problems when posting this challenge. – Nathan Merrill – 2016-08-14T22:16:39.567
I don't think so. I'm organizing input as a list of x,y pairs – Nathan Merrill – 2016-08-14T22:32:21.790
You are right. Sorry – Luis Mendo – 2016-08-14T22:39:10.177
Bah, it is a duplicate. I thought I did a pretty good search as well. – Nathan Merrill – 2016-08-14T22:51:35.857