September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,24 @@
# The polynomial approximation
f(x) = a*x**2 + b*x + c
# Initial values for parameters
a = 0.1
b = 0.1
c = 0.1
# Fit f to the following data by modifying the variables a, b, c
fit f(x) '-' via a, b, c
0 1
1 6
2 17
3 34
4 57
5 86
6 121
7 162
8 209
9 262
10 321
e
print sprintf("\n --- \n Polynomial fit: %.4f x^2 + %.4f x + %.4f\n", a, b, c)

View file

@ -14,9 +14,9 @@ var (
)
func main() {
a := Vandermonde(x, 2)
b := mat64.NewDense(11, 1, y)
c := mat64.NewDense(3, 1, nil)
a := Vandermonde(x, degree)
b := mat64.NewDense(len(y), 1, y)
c := mat64.NewDense(degree+1, 1, nil)
qr := new(mat64.QR)
qr.Factorize(a)

View file

@ -1,15 +0,0 @@
REAL :: n=10, x(n), y(n), m=3, p(m)
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y = (1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
p = 2 ! initial guess for the polynom's coefficients
SOLVE(NUL=Theory()-y(nr), Unknown=p, DataIdx=nr, Iters=iterations)
WRITE(ClipBoard, Name) p, iterations
FUNCTION Theory()
! called by the solver of the SOLVE function. All variables are global
Theory = p(1)*x(nr)^2 + p(2)*x(nr) + p(3)
END

View file

@ -1,2 +0,0 @@
SOLVE performs a (nonlinear) least-square fit (Levenberg-Marquardt):
p(1)=2.997135145; p(2)=2.011348347; p(3)=0.9906627242; iterations=19;

View file

@ -1,4 +0,0 @@
function polyfit(x, y, n)
A = [ float(x[i])^p for i = 1:length(x), p = 0:n ]
A \ y
end

View file

@ -1,7 +0,0 @@
julia> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
julia> y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
julia> polyfit(x, y, 2)
3-element Array{Float64,1}:
1.0
2.0
3.0

View file

@ -1,2 +1 @@
V=[1,6,17,34,57,86,121,162,209,262,321]~;
M=matrix(#V,3,i,j,(i-1)^(j-1));Polrev(matsolve(M~*M,M~*V))
polinterpolate([0..10],[1,6,17,34,57,86,121,162,209,262,321])

View file

@ -1,2 +1,2 @@
lsf(X,Y,n)=my(M=matrix(#X,n+1,i,j,X[i]^(j-1))); Polrev(matsolve(M~*M,M~*Y~))
lsf([0..10], [1,6,17,34,57,86,121,162,209,262,321], 2)
V=[1,6,17,34,57,86,121,162,209,262,321]~;
M=matrix(#V,3,i,j,(i-1)^(j-1));Polrev(matsolve(M~*M,M~*V))

View file

@ -0,0 +1,2 @@
lsf(X,Y,n)=my(M=matrix(#X,n+1,i,j,X[i]^(j-1))); Polrev(matsolve(M~*M,M~*Y~))
lsf([0..10], [1,6,17,34,57,86,121,162,209,262,321], 2)

View file

@ -0,0 +1,46 @@
constant x = {0,1,2,3,4,5,6,7,8,9,10}
constant y = {1,6,17,34,57,86,121,162,209,262,321}
constant n = length(x)
function regression()
atom {xm, ym, x2m, x3m, x4m, xym, x2ym} @= 0
for i=1 to n do
atom xi = x[i],
yi = y[i]
xm += xi
ym += yi
x2m += power(xi,2)
x3m += power(xi,3)
x4m += power(xi,4)
xym += xi*yi
x2ym += power(xi,2)*yi
end for
xm /= n
ym /= n
x2m /= n
x3m /= n
x4m /= n
xym /= n
x2ym /= n
atom Sxx = x2m-power(xm,2),
Sxy = xym-xm*ym,
Sxx2 = x3m-xm*x2m,
Sx2x2 = x4m-power(x2m,2),
Sx2y = x2ym-x2m*ym,
B = (Sxy*Sx2x2-Sx2y*Sxx2)/(Sxx*Sx2x2-power(Sxx2,2)),
C = (Sx2y*Sxx-Sxy*Sxx2)/(Sxx*Sx2x2-power(Sxx2,2)),
A = ym-B*xm-C*x2m
return {C,B,A}
end function
atom {a,b,c} = regression()
function f(atom x)
return a*x*x+b*x+c
end function
printf(1,"y=%gx^2+%gx+%g\n",{a,b,c})
printf(1,"\n x y f(x)\n")
for i=1 to n do
printf(1," %2d %3d %3g\n",{x[i],y[i],f(x[i])})
end for

View file

@ -0,0 +1,22 @@
include pGUI.e
constant x = {0,1,2,3,4,5,6,7,8,9,10}
constant y = {1,6,17,34,57,86,121,162,209,262,321}
IupOpen()
Ihandle plot = IupPlot("GRID=YES, MARGINLEFT=50, MARGINBOTTOM=40")
-- (just add ", AXS_YSCALE=LOG10" for a nice log scale)
IupPlotBegin(plot, 0)
for i=1 to length(x) do
IupPlotAdd(plot, x[i], y[i])
end for
{} = IupPlotEnd(plot)
Ihandle dlg = IupDialog(plot)
IupSetAttributes(dlg, "RASTERSIZE=%dx%d", {640, 480})
IupSetAttribute(dlg, "TITLE", "simple plot")
IupShow(dlg)
IupMainLoop()
IupClose()

View file

@ -1,2 +1 @@
(Intercept) x I(x^2)
1 2 3
coef(lm(y ~ poly(x, 2, raw=T)))

View file

@ -1,3 +0,0 @@
x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
coef(lm(y ~ x + I(x^2)))

View file

@ -0,0 +1,51 @@
/* REXX ---------------------------------------------------------------
* Implementation of http://keisan.casio.com/exec/system/14059932254941
*--------------------------------------------------------------------*/
xl='0 1 2 3 4 5 6 7 8 9 10'
yl='1 6 17 34 57 86 121 162 209 262 321'
n=11
Do i=1 To n
Parse Var xl x.i xl
Parse Var yl y.i yl
End
xm=0
ym=0
x2m=0
x3m=0
x4m=0
xym=0
x2ym=0
Do i=1 To n
xm=xm+x.i
ym=ym+y.i
x2m=x2m+x.i**2
x3m=x3m+x.i**3
x4m=x4m+x.i**4
xym=xym+x.i*y.i
x2ym=x2ym+(x.i**2)*y.i
End
xm =xm /n
ym =ym /n
x2m=x2m/n
x3m=x3m/n
x4m=x4m/n
xym=xym/n
x2ym=x2ym/n
Sxx=x2m-xm**2
Sxy=xym-xm*ym
Sxx2=x3m-xm*x2m
Sx2x2=x4m-x2m**2
Sx2y=x2ym-x2m*ym
B=(Sxy*Sx2x2-Sx2y*Sxx2)/(Sxx*Sx2x2-Sxx2**2)
C=(Sx2y*Sxx-Sxy*Sxx2)/(Sxx*Sx2x2-Sxx2**2)
A=ym-B*xm-C*x2m
Say 'y='a'+'||b'*x+'c'*x**2'
Say ' Input "Approximation"'
Say ' x y y1'
Do i=1 To 11
Say right(x.i,2) right(y.i,3) format(fun(x.i),5,3)
End
Exit
fun:
Parse Arg x
Return a+b*x+c*x**2

View file

@ -1,18 +1,18 @@
var Matrix = require('Math::Matrix');
var Matrix = require('Math::Matrix')
func regress(x, y, degree) {
var x_data = x.map { |xi| (0..degree).map { |pow| (xi**pow).to_f } };
var x_data = x.map {|xi| (0..degree).map {|pow| xi**pow } }
var mx = Matrix.new(x_data...);
var my = Matrix.new(y.map{[_]}...);
var mx = Matrix.new(x_data...)
var my = Matrix.new(y.map{[_]}...)
mx.transpose.multiply(mx).invert.multiply(mx.transpose).multiply(my).transpose;
mx.transpose.multiply(mx).invert.multiply(mx.transpose).multiply(my).transpose
}
var betas = regress(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321],
2
);
)
betas.print;
betas.print

View file

@ -0,0 +1,33 @@
. clear
. input x y
0 1
1 6
2 17
3 34
4 57
5 86
6 121
7 162
8 209
9 262
10 321
end
. regress y c.x##c.x
Source | SS df MS Number of obs = 11
-------------+---------------------------------- F(2, 8) = .
Model | 120362 2 60181 Prob > F = .
Residual | 0 8 0 R-squared = 1.0000
-------------+---------------------------------- Adj R-squared = 1.0000
Total | 120362 10 12036.2 Root MSE = 0
------------------------------------------------------------------------------
y | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x | 2 . . . . .
|
c.x#c.x | 3 . . . . .
|
_cons | 1 . . . . .
------------------------------------------------------------------------------

View file

@ -0,0 +1,7 @@
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
xs:=GSL.VectorFromData(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
ys:=GSL.VectorFromData(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321);
v :=GSL.polyFit(xs,ys,2);
v.format().println();
GSL.Helpers.polyString(v).println();
GSL.Helpers.polyEval(v,xs).format().println();

View file

@ -0,0 +1,3 @@
polyfit(T(T(0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0)),
T(T(1.0,6.0,17.0,34.0,57.0,86.0,121.0,162.0,209.0,262.0,321.0)), 2)
.flatten().println();