# Computing least squares error [closed]

I have a mathematica subroutine setup that computes the least squares approximation as follows

uniform[n_, knot_, data_] := (m = Length[data] - 1;
vandermonde =
Table[BernsteinBasis[n, j - 1, knot[[i]]], {i, 1, m + 1}, {j, 1,
n + 1}];
bez = LinearSolve[Transpose[vandermonde].vandermonde,
Transpose[vandermonde].data]; dplot = ListPlot[data];
points = Graphics[{PointSize[0.015], Point[bez]}];
bplot = ListLinePlot[bez];
curve[t_] := Sum[BernsteinBasis[n, i, t]*bez[[i + 1]], {i, 0, n}];
cplot = ParametricPlot[curve[t], {t, 0, 1},
PlotStyle -> {Thick, Green}];
Show[{cplot, points, bplot, dplot}]);


I now need to compute the least squares error for the set of equations and return the total,min,max and average error.

I went through this example in order to understand the concept - https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Motivational_example

I am unsure as to how to proceed with performing this computation in mathematica as I'm still a newbie.

## closed as unclear what you're asking by dr.blochwave, MarcoB, RunnyKine, Dr. belisarius, Andy RossMar 7 '16 at 0:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Same code found here: mathematica.stackexchange.com/questions/108633/… – Michael E2 Mar 6 '16 at 17:41
• (1) What do all the plots have to do with your question? (2) Re "how to proceed with performing this computation" -- what computation, exactly? There is probably enough information to figure it out, but you're asking everybody to do a lot of work just to understand what you're asking. – Michael E2 Mar 6 '16 at 17:47
• – Dr. belisarius Mar 7 '16 at 0:31

## 1 Answer

Here's how to calculate the errors in a simple case. a is a matrix and b is a vector. You find the x that minimizes the least square error and then calculate the errors err. Then find the Total, Min, Max, Mean.

a = RandomReal[{-1, 1}, {3, 2}];
b = RandomReal[{-1, 1}, 3];
x = LeastSquares[a, b]
err = a.x - b;
{Total[err], Min[err], Max[err], Mean[err]}