# Fit line similar to power of trendline in Excel

Is there a way to fit a line similar to power trendline in Excel?

Something in this fashion: Fit[data, {1, x, x^(-n)}, x]

EDIT:

modelFit = NonlinearModelFit[data, a*x^n, {a, n}, x];

Show[
ListPlot[data, PlotStyle -> Red],
Plot[modelFit[x], {x, 10, 300}]
]


Gives a result which is a little off:

Data:

data = {{10, 0.229456252}, {11, 0.197084485}, {12, 0.190385018}, {13,
0.167211837}, {14, 0.162024048}, {15, 0.146360843}, {16,
0.141582714}, {17, 0.128658408}, {18, 0.12634757}, {19,
0.115664973}, {20, 0.112934492}, {21, 0.103436493}, {22,
0.101525578}, {23, 0.094280256}, {24, 0.093998465}, {25,
0.087612133}, {26, 0.085961964}, {27, 0.081235224}, {28,
0.079490311}, {29, 0.075953893}, {30, 0.073722495}, {31,
0.070194375}, {32, 0.069373963}, {33, 0.066115294}, {34,
0.064971653}, {35, 0.061982956}};

• Use the same function if you want the same result: something like NonlinearModelFit[data, a + b x + c x^n, {a,b,c,n}, x]; May 5, 2013 at 13:43
• Out of curiosity, what does your data represent? May 5, 2013 at 20:48
• @Szabolcs, variance of scores in a computer boardgame. May 5, 2013 at 21:35
• can you explain in what way the data is a little off? May 6, 2013 at 7:26
• @jensen, im not sure if my screenshot is clear enough, but the line does not go through the first points in the upper bound of the graph. May 6, 2013 at 12:59

Sure. Try e.g. data = Transpose[{Range[10], Range[10]^3}] and then use NonlinearModelFit[data, x^n, {n}, x]. As bill s said, read for more details Mathematicas help regarding NonlinearModelFit.

For your data you can use:

fitFkt = NonlinearModelFit[data, a*x^n, {a, n}, x]
fitFkt["ParameterConfidenceIntervalTable", ConfidenceLevel -> .95]


The result is the same as within Excel (y = 2.3409x^-1.018; you only forgot the factor a) as you can see from:

Show[ListPlot[data], Plot[fitFkt[x], {x, 10, 35}]]


• I have updated my post with an example im working on. The fitted line is a bit off. May 5, 2013 at 13:32
• Could you please give us the data too? Then we could try to get a better fit. May 5, 2013 at 14:04
• Sure, i can give a small sample. Excel fits it perfectly to a power trendline. May 5, 2013 at 14:14
• Thanks for the great example. What i dont understand is that my fitted line does not begin from 10 as my points. I need to enter something like {-2000,300,x} which seems ok. I have updated the example with my issue. May 5, 2013 at 20:22
• Ok, just to summarize: The fit is ok, just the plot not. Do you agree? That is because you plot over a big range. If you use Show[ListPlot[data, PlotStyle -> Red, PlotRange -> All], Plot[modelFit[x], {x, 10, 300}, PlotRange -> All]] you will see that the function fits to the data (without choosing such a big range as above). By the way: If you do not trust your fit, you can use something like Table[{i, modelFit[i]}, {i, 10, 30}]. If you compare the result with your data (or calculate the residual), you will see that the fit is well. May 6, 2013 at 7:10

Plot automatically reduces the Plot range depending on the function that is plotted. Adding PlotRange->All shows the function completely in the Plot plot. Show uses the data range from the first plot that is shown, so you eventually need to add a PlotRange->All inside the Show command (depending on your data).

modelFit = NonlinearModelFit[data, a*x^n, {a, n}, x];

Show[
ListPlot[data, PlotStyle -> Red],
Plot[modelFit[x], {x, 10, 300}, PlotRange->All]
]


### An advice for plotting and fitting power laws:

Often it is better to take the logarithm of the function and the data, or to use a logarithmic scale (LogLogPlot or ListLogLogPlot).

Let's say you have $f(x)=a\cdot x^n$, taking the logarithm leads to: $$\log(f(x)) = \log(a) + n \cdot \log(x)$$ so you basically get a straight line if you plot $\log(f(x))$ vs. $\log(x)$ and the slope is the exponent $n$. This makes it easier to see deviations.

Show[ListPlot[Log@data, PlotStyle -> Red],
Plot[Log@modelFit[Exp@x], {x, 1, 4}], Frame -> True,
FrameLabel -> {"log(score)", "log(Var)"},
BaseStyle -> {14, FontFamily -> "Helvetica"}]


If the model is linear, you can use LinearModelFit. If the model is nonlinear, then NonlinearModelFit. To see how to apply these, check out the help (F1 on the word LinearModelFit) where there are plenty of examples.

To mimic the function you've included, you could try something like:

NonlinearModelFit[data, a + b x + c x^n, {a,b,c,n}, x];
`

Though it also looks like an exponential decay, so you might want to see how that fits as well.