0
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$
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]}

$\endgroup$
4
  • $\begingroup$ Nice solution, thanks! $\endgroup$ Oct 20, 2016 at 15:21
  • $\begingroup$ 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. $\endgroup$ Oct 20, 2016 at 15:35
  • $\begingroup$ The above example does not give a warning at this step. It has to do something with your data, or with some interfering definitions. $\endgroup$
    – corey979
    Oct 20, 2016 at 15:43
  • $\begingroup$ OK I'll check again. It says ´too few arguments´. $\endgroup$ Oct 20, 2016 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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