> evalf(2*100000*ln(100000)); 7 .2302585092 10 > f := proc(x) 2*x + 1; end; f := proc(x) 2*x + 1 end > rando := rand(-10^5 .. 10^5); rando := proc() local t; global _seed; _seed := irem(427419669081*_seed, 999999999989); t := _seed; irem(t, 200001) - 100000 end > for i from 1 to 10 do > x[i] := i; > y[i] := f(i) + i*rando()/(10.0^5)/10; > od; > x[1] := 1 y[1] := 3.031994000 x[2] := 2 y[2] := 4.975450000 x[3] := 3 y[3] := 7.166623000 x[4] := 4 y[4] := 9.289180000 x[5] := 5 y[5] := 11.13348500 x[6] := 6 y[6] := 12.98072800 x[7] := 7 y[7] := 14.73242500 x[8] := 8 y[8] := 17.05532800 x[9] := 9 y[9] := 19.83168100 x[10] := 10 y[10] := 21.09528000 > points := []:for i from 1 to 10 do > points := [op(points), [x[i], y[i]]]; > od: > > points; [[1, 3.031994000], [2, 4.975450000], [3, 7.166623000], [4, 9.289180000], [5, 11.13348500], [6, 12.98072800], [7, 14.73242500], [8, 17.05532800], [9, 19.83168100], [10, 21.09528000]] > A := linalg[matrix](10,2); b := linalg[vector](10); A := array(1 .. 10, 1 .. 2, []) b := array(1 .. 10, []) > x_coords := []: y_coords:=[]: yw_coords:=[]: > for i from 1 to 10 do > x_coords := [op(x_coords), x[i]]; A[i,1] := 1; A[i,2] := x[i]; > y_coords := [op(y_coords), y[i]]; b[i] := y[i]; > yw_coords := [op(yw_coords), Weight(y[i],1/i)]; b[i] := y[i]; > od: > > print(A); [1 1] [ ] [1 2] [ ] [1 3] [ ] [1 4] [ ] [1 5] [ ] [1 6] [ ] [1 7] [ ] [1 8] [ ] [1 9] [ ] [1 10] > print(b); [3.031994000, 4.975450000, 7.166623000, 9.289180000, 11.13348500, 12.98072800, 14.73242500, 17.05532800, 19.83168100, 21.09528000] > x_coords; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > y_coords; [3.031994000, 4.975450000, 7.166623000, 9.289180000, 11.13348500, 12.98072800, 14.73242500, 17.05532800, 19.83168100, 21.09528000] > model := stats[fit,leastsquare[[x,y],y=a*x + b]]([x_coords, > y_coords]); model := y = 2.025355721 x + .9897609333 > with(linalg): > W := diag(seq(1/i,i=1..10)); [1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0] [ ] [0 , 1/2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0] [ ] [0 , 0 , 1/3 , 0 , 0 , 0 , 0 , 0 , 0 , 0] [ ] [0 , 0 , 0 , 1/4 , 0 , 0 , 0 , 0 , 0 , 0] [ ] [0 , 0 , 0 , 0 , 1/5 , 0 , 0 , 0 , 0 , 0] W := [ ] [0 , 0 , 0 , 0 , 0 , 1/6 , 0 , 0 , 0 , 0] [ ] [0 , 0 , 0 , 0 , 0 , 0 , 1/7 , 0 , 0 , 0] [ ] [0 , 0 , 0 , 0 , 0 , 0 , 0 , 1/8 , 0 , 0] [ ] [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1/9 , 0] [ ] [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1/10] > weighted_normal := linsolve(evalm(transpose(A) &* W &* A), > evalm(transpose(A) &* W &* b)); weighted_normal := [1.006056316, 2.022392924] > yw_coords; [Weight(3.031994000, 1), Weight(4.975450000, 1/2), Weight(7.166623000, 1/3), Weight(9.289180000, 1/4), Weight(11.13348500, 1/5), Weight(12.98072800, 1/6), Weight(14.73242500, 1/7), Weight(17.05532800, 1/8), Weight(19.83168100, 1/9), Weight(21.09528000, 1/10)] > newmodel := stats[fit,leastsquare[[x,y],y=a*x + b]]([x_coords, > yw_coords]); newmodel := y = 2.022392924 x + 1.006056316 > print(weighted_normal); [1.006056316, 2.022392924] > model; y = 2.025355721 x + .9897609333