# Return to Answer

 4 Added Chop edited Jun 12 '12 at 9:41 freddieknets 65544 silver badges1414 bronze badges As you can see in the result, there are some values which are almost zero (10^-11 or smaller). These are simply the result of some internal round-off errors, and should of course be equal to zero. You can easily add a piece of code to discard all coefficients which are a factor smaller than the largest coefficient.sols = sols/. x_ /; x < M / 10^6 :> 0 final[x_] := (f[x] /. sols)[] final[x] final EDIT 2 It appears Mathematica has a built-in function to deal with near-zero numerics: Chop. It's al lot more easy to use than the method I proposed in my former edit; just write Chop[sols] and it will automatically thread over the rules. Thanks J.M. for pointing that out! Then the final code would be:ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}] sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]] final[x_] := (f[x] /. Chop[sols])[] final[x] final  As you can see in the result, there are some values which are almost zero (10^-11 or smaller). These are simply the result of some internal round-off errors, and should of course be equal zero. You can easily add a piece of code to discard all coefficients which are a factor smaller than the largest coefficient.sols = sols/. x_ /; x < M / 10^6 :> 0 final[x_] := (f[x] /. sols)[] final[x] final  As you can see in the result, there are some values which are almost zero (10^-11 or smaller). These are simply the result of some internal round-off errors, and should of course be equal to zero. You can easily add a piece of code to discard all coefficients which are a factor smaller than the largest coefficient.sols = sols/. x_ /; x < M / 10^6 :> 0 final[x_] := (f[x] /. sols)[] final[x] final EDIT 2 It appears Mathematica has a built-in function to deal with near-zero numerics: Chop. It's al lot more easy to use than the method I proposed in my former edit; just write Chop[sols] and it will automatically thread over the rules. Thanks J.M. for pointing that out! Then the final code would be:ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}] sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]] final[x_] := (f[x] /. Chop[sols])[] final[x] final  3 Added discarding of almost-zero coefficients edited Jun 11 '12 at 16:55 freddieknets 65544 silver badges1414 bronze badges You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  EDIT As you can see in the result, there are some values which are almost zero (10^-11 or smaller). These are simply the result of some internal round-off errors, and should of course be equal zero. You can easily add a piece of code to discard all coefficients which are a factor smaller than the largest coefficient. The largest coefficient is given by: M = Max[Apply[#2 &, sols, {2}][]  (don't mind the syntax too much yet, it is just a way to select the values from sols in order to be able to use Maxon it) Next we make the replacement for every x which is at least 10^6 times smaller than the maximum: sols = sols/. x_ /; x < M / 10^6 :> 0 final[x_] := (f[x] /. sols)[] final[x] final  You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  EDIT As you can see in the result, there are some values which are almost zero (10^-11 or smaller). These are simply the result of some internal round-off errors, and should of course be equal zero. You can easily add a piece of code to discard all coefficients which are a factor smaller than the largest coefficient. The largest coefficient is given by: M = Max[Apply[#2 &, sols, {2}][]  (don't mind the syntax too much yet, it is just a way to select the values from sols in order to be able to use Maxon it) Next we make the replacement for every x which is at least 10^6 times smaller than the maximum: sols = sols/. x_ /; x < M / 10^6 :> 0 final[x_] := (f[x] /. sols)[] final[x] final  2 added 7 characters in body edited Jun 11 '12 at 16:05 freddieknets 65544 silver badges1414 bronze badges You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  You have some errors in your syntax: you name your lists x_sample and y_sample, but in Mathematica, an underscore is not allowed in names (as it is reserved to patterns). your last sum runs from 0, but in Mathematica, the first element in a List has index 1 your last sum should run until the number of data points, not 4 furthermore, I would advise you to use SetDelayed(:=) in your function definition, as it prevents possible naming conflicts also in your function definition, letting the sum run from 0 will give problems for x=0, as it produces 0^0, which is Indeterminate. This gives: ysample = {0, 1, 2, 3, 4, 5, 6, 7, 8} xsample = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8} f[x_] := Subscript[a,0] + Sum[Subscript[a, i] x^i, {i, 1, 4}] result = Sum[(ysample[[i]] - f[xsample[[i]]])^2, {i, 1, Length[ysample]}]  Next, to calculate the derivative with respect to, say, Subscript[a,0], use D[result,Subscript[a,0]]. Last, to get a solution to the set of linear equations, use Solve: sols = Solve[Table[D[result, Subscript[a, i]] == 0, {i, 0, 4}], Table[Subscript[a, i], {i, 0, 4}]]  To put the values of the coefficients in your polynomial, use Replace(/.): final[x_] := (f[x] /. sols)[] final[x] final  1 answered Jun 11 '12 at 16:00 freddieknets 65544 silver badges1414 bronze badges