From 4cd1ade5d3c430d2cc8316301db3d667747a5648 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 20 Apr 2014 22:02:59 +0200 Subject: [PATCH] print out better messages --- least-squares.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/least-squares.py b/least-squares.py index d0a14b7..a7add51 100755 --- a/least-squares.py +++ b/least-squares.py @@ -17,17 +17,18 @@ yn = [6, 5, 7, 10, 11, 12, 14] # For each data point the vertical error/residual is x*b1 + b2 - y. We want to # minimize the sum of the squared residuals (least squares). S = sum((xn[i] * b1 + b2 - yn[i]) ** 2 for i in range(0, len(xn))) -print(S) +print("Function to minimize: S = {}".format(S)) # Minimize S by setting its partial derivatives to zero. d1 = diff(S, b1) d2 = diff(S, b2) solutions = solve([d1, d2], [b1, b2]) +print("S is minimal for b1 = {}, b2 = {}".format(solutions[b1], solutions[b2])) # Construct fitted line from the solutions x = Symbol('x') fitted_line = solutions[b1] * x + solutions[b2] -print(fitted_line) +print("Fitted line: y = {}".format(fitted_line)) # Construct something we can plot with matplotlib fitted_line_func = lambdify(x, fitted_line, modules=['numpy'])