# How to fit a data set in an iterative way?

I am going to fit a data set in the following way. I have a data set $y$ vs. $x$. $y$ is again a function of $t$ i.e. $y=f(t)$. With varying $t$, I can get different $y$ and can plot $y$ vs. $x$, and fit each plot to find the fitting parameters. I need all the fitting parameters with varying $t$. I have used code like this:

dataf = Table[FindFit[data, a + b*i, {a, b}, i], {i, 1, 5}]


This does not work. I don't understand how to express it? Please help.

• Hi Partha. It's not entirely clear to me what you are trying to achieve there. Could you please provide an example data set for data? Do you mean that your model looks like y[[i]] = f(x[[i]],t) with lists x and y prescribed and t being a further parameter? – Henrik Schumacher Jun 2 '18 at 13:08
• Err... sorry for using incorrect syntax. I meant $y_i = f(x_i,t)$ (or y[[i]] == f[x[[i]], t]). – Henrik Schumacher Jun 2 '18 at 13:20
• You have i serving two different roles here. You're telling FindFit that it's the independent variable in the fitting formula, but then you're trying to use it as a Table index, too. I can't understand what you're attempting. – John Doty Jun 2 '18 at 13:43
• My entire equation looks like: fit = Table[ FindFit[Transpose[{[Nu], (NormalisedIntensity/ avglifetime)*([Tau][[1]]*Exp[-t/[Tau][[2]]] + [Tau][[3]]* Exp[-t/[Tau][[4]]] + [Tau][[5]]*Exp[-t/[Tau][[6]]])}], h*Exp[ -(0.693)*( Log[1 + 2*a*([Nu] - n)/d]/a )^2] , {a, d, n, h}, t], {t, 0, 2}] – P Pyne Jun 2 '18 at 13:44
• – kglr Jun 2 '18 at 14:19

## 1 Answer

maybe something like this:

SeedRandom[1]
data = {#, #^2 RandomReal[{.9, 1.1}]} & /@ Range[100];
f[x_, t_] := a + b x^t;

fitf[t_] := FindFit[data, f[x, t], {a, b}, x]
fittedmodels = Table[With[{t = t}, Evaluate[f[x, t] /. fitf[t]]], {t, 1, 5}];
Show[Plot[Evaluate@fittedmodels, {x, 0, 100}, PlotStyle -> Thick,
PlotLegends -> ("t = " <> ToString[#] & /@ Range[5])],
ListPlot[data, PlotStyle -> PointSize[Medium]]]