1
$\begingroup$

I have a data set of $10^4$ entries. I want to plot the last $2000$ data with the same argument at $x$ axis and fit a curve. That means the range of $x$ axis will be $8001$ to $10000$ incremented by $1$.

I wrote code like the following to know the coefficient of fitting curve:

d = Import["ss.dat"];
T = Table[d[[1, i]], {i, 8001, 10000}];
FindFit[T, a*E^(-b*x), {a, b}, x]

FindFit returns the values of $a$ and $b$ considering $x$ to range from $1$ to $2000$ instead of $8001$ to $10000$.

If anyone can tell me how to deal with this problem that will be great help.

I am attaching data in the following link:

data as DAT file in Google Drive

$\endgroup$
2
  • 2
    $\begingroup$ You could try T = Table[{i, d[[1, i]]}, {i, 8001, 10000}];. This will allow you to give FindFit 2D data with the specific x values you want. $\endgroup$
    – MassDefect
    Mar 18, 2019 at 16:11
  • 1
    $\begingroup$ Your first problem has been pointed out by @MassDefect. But you should linearize your model with the following: lmf = LinearModelFit[Transpose[{T[[All, 1]], Log[T[[All, 2]]]}], x, x] Show[LogPlot[Exp[lmf[x]], {x, 8001, 10000}, PlotStyle -> {LightGray, Thickness[0.03]}], ListLogPlot[T, PlotStyle -> Red]]. $\endgroup$
    – JimB
    Mar 18, 2019 at 16:33

1 Answer 1

2
$\begingroup$

Generally, you don't need nearly so many points to get a nice fit. Regardless, we can use NonlinearModelFit[] to get what you want:

You can use Drop[list,n] to drop the first n elements of a list.

dir = NotebookDirectory[];
SetDirectory[dir];
file = "ss.dat";

d = Import[file];
T = Drop[Flatten[d], 8000];
St = Drop[Take[T, {1, -1, 1}], 0];
Stt = Table[{i, T[[i]]}, {i, 1, Length[St]}];
e = NonlinearModelFit[Stt, a*E^(-b*x), {a, b}, x]
e["BestFitParameters"]


GraphicsGrid[{{ListPlot[Stt, ImageSize -> Medium, PlotRange -> All, PlotTheme -> "Scientific"], Plot[{Evaluate[a*E^(-b*x) /. e["BestFitParameters"]]}, {x, 0,Length[Stt]}, PlotTheme -> "Scientific", Epilog -> {Red, Point[Stt]}]}}]

plots

Using the code will drop every x elements from your entire list, drastically reducing your plot points. 2000, or even 10000 are just not needed.

St = Drop[Take[T, {1, -1, 1}], 0];

and changing to:

St = Drop[Take[T, {1, -1, 40}], 0];

Gives us:

plot2

A nice visual that the points lay directly on our function, and a good fit for a and b.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.