From d0faaddc911376edd266fbaaa8e2a4b73ed5a56f Mon Sep 17 00:00:00 2001 From: neingeist Date: Mon, 21 Apr 2014 11:28:51 +0200 Subject: [PATCH] simplify S before printing it --- least-squares.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/least-squares.py b/least-squares.py index ea21491..9603523 100755 --- a/least-squares.py +++ b/least-squares.py @@ -1,6 +1,6 @@ #!/usr/bin/python2.7 from __future__ import division, print_function -from sympy import Symbol, diff, solve, lambdify +from sympy import Symbol, diff, solve, lambdify, simplify import matplotlib.pyplot as plt import numpy as np @@ -16,6 +16,7 @@ data = [(1,14), (2, 13), (3, 12), (4, 10), (5,9), (7,8), (9,5)] # 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((p[0] * b1 + b2 - p[1]) ** 2 for p in data) +S = simplify(S) print("Function to minimize: S = {}".format(S)) # Minimize S by setting its partial derivatives to zero.