Message Boards Message Boards

How would you fit this data in Mathematica?

Anonymous User
Anonymous User
Posted 11 years ago

the x is 1,2,3,4,5. The y is 1.2,2.3,3.6,4.9,5.9. FInd the best-fitting power function. That is, find the best fitting function of the form f(x)=p* x^n for real parameters p and n.

POSTED BY: Anonymous User
6 Replies

Transpose in this case essentially combines the two lists (like using Partition[Riffle[xi, yi], 2]). Nasser made a 2 x 5 matrix (each list as a row) and the transposed it to get the pairs of coordinates as a 5 x 2 matrix. This gets you the XY pairs. You can see this if you run the steps below:

Clear[xi,yi];
yi = {1.2, 2.3, 3.6, 4.9, 5.9};
xi = {1, 2, 3, 4, 5};
{xi, yi} // MatrixForm
Transpose[{xi, yi}] // MatrixForm
POSTED BY: Tim Mayes
Clear[p, x, n];
yi = {1.2, 2.3, 3.6, 4.9, 5.9};
xi = {1, 2, 3, 4, 5};
data = Transpose[{xi, yi}];
nlm = NonlinearModelFit[data, p x^n, {p, n}, x]
p1 = ListPlot[data, PlotStyle -> Red];
p2 = Plot[Normal[nlm], {x, 0, 6}, PlotTheme -> "Detailed",
              FrameLabel -> {{"y(t)", None}, {"x", "non-linear Fit"}}];
Show[p2, p1]

enter image description here

POSTED BY: Nasser M. Abbasi
Anonymous User
Anonymous User
Posted 11 years ago

Can you explain what Transpose does thanks.

POSTED BY: Anonymous User

For simple functions such as Transpose, the Mathemtica help menu provides excellent examples.

POSTED BY: Dominik Hezel
Anonymous User
Anonymous User
Posted 11 years ago

And how would you find the residuals?

POSTED BY: Anonymous User

And how would you find the residuals?

Options are build-in. Help for more information:

yi = {1.2, 2.3, 3.6, 4.9, 5.9};
xi = {1, 2, 3, 4, 5};
data = Transpose[{xi, yi}];
nlm = NonlinearModelFit[data, p x^n, {p, n}, x]
nlm["FitResiduals"]
nlm["StandardizedResiduals"]
nlm["StudentizedResiduals"]
nlm["EstimatedVariance"]

enter image description here

POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract