Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Ein Rechenbeispiel für die Methode der kleinsten Quadrate

Project: LA2
Views: 60
x = var('x') P = plot(2 -x/5 +x^2/10, (x,0,10), aspect_ratio=1) P
X = [RR.random_element(0,10) for i in range(15)] X Y = [ 2-x/5+x^2/10 + RR.random_element(-0.5,0.5) for x in X] Y
[0.329850278556738, 2.18363235954866, 3.00653862263458, 7.82509499039911, 2.41295582756792, 8.82096155640997, 6.29335269264018, 4.56005152687637, 8.41752828741785, 1.43422452587682, 9.43870534236926, 8.30786618253789, 9.74705801496926, 6.75530497116745, 0.891837439866543] [2.20721770196852, 2.07161724874923, 2.34767033216582, 6.33305700677551, 2.23446401049651, 7.78840687443978, 5.19839139096079, 3.48555351235931, 7.69350336701707, 1.47605476077739, 8.61092973518972, 6.80790264033335, 9.76822184268289, 5.36627395326383, 2.28524285589942]
P0 = plot([], aspect_ratio=1) for i in range(len(X)): P0 += point((X[i],Y[i])) P0
A1 = matrix(RR, 15, 2) for i in range(15): A1[i,0] = 1 A1[i,1] = X[i] A1
[ 1.00000000000000 0.329850278556738] [ 1.00000000000000 2.18363235954866] [ 1.00000000000000 3.00653862263458] [ 1.00000000000000 7.82509499039911] [ 1.00000000000000 2.41295582756792] [ 1.00000000000000 8.82096155640997] [ 1.00000000000000 6.29335269264018] [ 1.00000000000000 4.56005152687637] [ 1.00000000000000 8.41752828741785] [ 1.00000000000000 1.43422452587682] [ 1.00000000000000 9.43870534236926] [ 1.00000000000000 8.30786618253789] [ 1.00000000000000 9.74705801496926] [ 1.00000000000000 6.75530497116745] [ 1.00000000000000 0.891837439866543]
B1 = A1.transpose()*A1 B1
[15.0000000000000 80.4249626188386] [80.4249626188386 591.636754960217]
c = B1^(-1)*A1.transpose()*matrix(RR,15,1,Y)
x = var('x') c[1,0]*x+c[0,0] P1 = P0 + plot(c[1,0]*x+c[0,0], (x,0,10), color="green") P1
0.796409847503090*x + 0.641551667891212
A2 = matrix(RR, 15, 3) for i in range(15): A2[i,0] = 1 A2[i,1] = X[i] A2[i,2] = X[i]^2 A2
[ 1.00000000000000 0.329850278556738 0.108801206263958] [ 1.00000000000000 2.18363235954866 4.76825028166807] [ 1.00000000000000 3.00653862263458 9.03927448939343] [ 1.00000000000000 7.82509499039911 61.2321116087693] [ 1.00000000000000 2.41295582756792 5.82235582579398] [ 1.00000000000000 8.82096155640997 77.8093627796626] [ 1.00000000000000 6.29335269264018 39.6062881139614] [ 1.00000000000000 4.56005152687637 20.7940699277676] [ 1.00000000000000 8.41752828741785 70.8547824694797] [ 1.00000000000000 1.43422452587682 2.05699999062658] [ 1.00000000000000 9.43870534236926 89.0891585400700] [ 1.00000000000000 8.30786618253789 69.0206405069567] [ 1.00000000000000 9.74705801496926 95.0051399471765] [ 1.00000000000000 6.75530497116745 45.6341452534797] [ 1.00000000000000 0.891837439866543 0.795374019147710]
B2 = A2.transpose()*A2 B2
[15.0000000000000 80.4249626188386 591.636754960217] [80.4249626188386 591.636754960217 4809.92828769207] [591.636754960217 4809.92828769207 40777.5190720364]
c = B2^(-1)*A2.transpose()*matrix(RR,15,1,Y) c x = var('x') P2 = P1 + plot(c[2,0]*x^2+c[1,0]*x+c[0,0], (x,0,10), color="red") P2
[ 2.04372953078724] [-0.139533269106505] [0.0916787568867184]
[(round(X[i],2),round(Y[i],2)) for i in range(len(X))]
[(0.33, 2.21), (2.18, 2.07), (3.01, 2.35), (7.83, 6.33), (2.41, 2.23), (8.82, 7.79), (6.29, 5.2), (4.56, 3.49), (8.42, 7.69), (1.43, 1.48), (9.44, 8.61), (8.31, 6.81), (9.75, 9.77), (6.76, 5.37), (0.89, 2.29)]