You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* `gcd(a::Poly, b::Poly)`: Finds the Greatest Common Denominator of
@@ -428,6 +437,72 @@ function gcd{T, S}(a::Poly{T}, b::Poly{S})
428
437
end
429
438
end
430
439
440
+
441
+
## Fit degree n polynomial to points
442
+
"""
443
+
444
+
`polyfit(x::AbstractVector, y::AbstractVector)`: Fit a polynomial of degree `n` through the points specified by `x` and `y` where `n <= length(x) - 1` using least squares fit.
445
+
446
+
Example:
447
+
448
+
```
449
+
xs = linspace(0, pi, 5)
450
+
ys = map(sin, xs)
451
+
polyfit(xs, ys, 2)
452
+
```
453
+
454
+
Original by [ggggggggg](https://github.com/Keno/Polynomials.jl/issues/19)
455
+
"""
456
+
functionpolyfit(x, y, n)
457
+
length(x) ==length(y) ||throw(DomainError)
458
+
n <=length(x) -1||throw(DomainError)
459
+
460
+
A = [ float(x[i])^p for i =1:length(x), p =0:n ]
461
+
Poly(A \ y)
462
+
end
463
+
464
+
## Find interpolating polynomial through the points
465
+
"""
466
+
467
+
* `polyinterpolate(x, y)`: Return interpolating polynomial of degree `n-1` or less.
468
+
469
+
Example
470
+
471
+
```
472
+
xs = linspace(0, pi, 3)
473
+
ys = map(sin, xs)
474
+
polyinterpolate(xs, ys)
475
+
```
476
+
477
+
This uses [Algorithm 1](http://www.ams.org/journals/mcom/1970-24-109/S0025-5718-1970-0258240-X/S0025-5718-1970-0258240-X.pdf) of Krogh, *Efficient Algorithms for Polynomial Interpolation and Numerical Differentiation*.
0 commit comments