# FindFit for all columns of a matrix

I have a table of dimensions {10,3} say. Each column is the value of some function f(x,a) wrt to x and the parameter $a~ ( =1,2,3$ say) refers to the columns. So for example if we let

$f(x,a) = a \sin(x)$ then the first column of my matrix contains values $$1\cdot \sin(x), (\text{where }x=1,...10)$$ second column $$2\cdot \sin(x)$$

and so on. Obviously one can use FindFit to force Mathematica find a fit to the data point in say column 1. One can repeat this for the other columns. But is there a simple way to use FindFit to find fits to each column?

In the above example suppose I save the data of column 1 in a variable named columnOne: I would write for that particular column

fitPAR =
FindFit[columnOne,
a*Sin[b*x], {a, b}, x];
fitColumnOne[x_] = a*Sin[b*x], {a, b} /. fitPAR;


and as an output I should obtain that

fitColumnOne[x_] = 1*Sin[1*x].


How can one automatize this to apply to each column of a matrix?

data = Table[a Sin[x], {x, 1, 10}, {a, 1, 3}];

rules = FindFit[#, a*Sin[b*x], {a, b}, x]& /@ Transpose @ data


{{a -> 1., b -> 1.}, {a -> 2., b -> 1.}, {a -> 3., b -> 1.}}

ReplaceAll[#][a*Sin[b*x]]& /@ rules


{1. Sin[1. x], 2. Sin[1. x], 3. Sin[1. x]}

• Nice solution, thanks! – Your Majesty Oct 20 '16 at 15:21
• This works very nicely but Mathematica gives a warning regarding ReplaceAll[#][a*Sin[b*x]]& /@ rules that too few arguments in expr/.rules. Anyway to avoid this warning? The code still works though. – Your Majesty Oct 20 '16 at 15:35
• The above example does not give a warning at this step. It has to do something with your data, or with some interfering definitions. – corey979 Oct 20 '16 at 15:43
• OK I'll check again. It says ´too few arguments´. – Your Majesty Oct 20 '16 at 15:47