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.
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]
Can you explain what Transpose does thanks.
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
And how would you find the residuals?
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"]
For simple functions such as Transpose, the Mathemtica help menu provides excellent examples.

