Karen Bindash Guest
|
Posted: Fri Jul 25, 2008 12:35 pm Post subject: How to fit Log[] data in Mathematica? |
|
|
I have some data consisting of xy pairs {{x1,y1},{x2,y2},{x3,y3}...}
data = {{10, 34.2}, {30, 36.7}, {100, 38.2}, {300, 39}, {1000,
39.6}, {3000, 40}, {10000, 40.4}, {30000, 40.6}, {100000,
408}, {300000, 41.1},{1000000,41.2}}
As you can see, x ranges from 10 to 1000000 as y ranges from 34.3 to
41.2.
1) How can I plot y against Log[10,x] ? i.e. I want the x-axis to go
from 1 to 6, and the y axis from 32 to 42. I think the graph will not
be too far from straight.
2) How can I create a fit on the assumption that y = a + b Log[10,x] +
c*Log[10,x]^2 + d Log[10,x]^3 ?
3) Assuming I can get a Plot of the fit, and a ListPlot of the
original data, would the best way to show them on the same graph be to
use Show[], or is there a better way? I>ve had problems doing this in
the past, where I>ve never managed to work out what the axes will be
or the label, PlotLabel etc.
anyone any concrete examples for thi. |
|
Dave Guest
|
Posted: Fri Jul 25, 2008 6:35 pm Post subject: Re: How to fit Log[] data in Mathematica? |
|
|
Karen Bindash wrote:
[quote]I have some data consisting of xy pairs {{x1,y1},{x2,y2},{x3,y3}...}
[/quote]
I>m not quite sure why you have posted this 3 times, with slight
changes, but:.
[quote]
data = {{10, 34.2}, {30, 36.7}, {100, 38.2}, {300, 39}, {1000,
39.6}, {3000, 40}, {10000, 40.4}, {30000, 40.6}, {100000,
408}, {300000, 41.1},{1000000,41.2}}
As you can see, x ranges from 10 to 1000000 as y ranges from 34.3 to
41.2.
[/quote]
I assume the {100000,408} should be {100000,40.8} then ??? As it is, the
y range is not limited to the range 34.3 to 41.2.
[quote]
1) How can I plot y against Log[10,x] ? i.e. I want the x-axis to go
from 1 to 6, and the y axis from 32 to 42. I think the graph will not
be too far from straight.
[/quote]
It will not be straight with the 408 in there, but if its 40.8, perhaps
it will.
[quote]
2) How can I create a fit on the assumption that y = a + b Log[10,x] +
c*Log[10,x]^2 + d Log[10,x]^3 ?
[/quote]
I doubt very much this is efficient (I never really have understood how
best to manipulate lists in Mathematica, I>m sure I often call Transpose
and similar things far too much), but you could try the following.
In[1]:= data = {{10, 34.2}, {30, 36.7}, {100, 38.2}, {300, 39}, {1000,
39.6}, {3000, 40}, {10000, 40.4}, {30000, 40.6}, {100000,
40.8}, {300000, 41.1}, {1000000, 41.2}}
Out[1]= {{10, 34.2}, {30, 36.7}, {100, 38.2}, {300, 39}, {1000, 39.6},
[quote]{3000, 40}, {10000, 40.4}, {30000, 40.6}, {100000, 40.8},
{300000, 41.1}, {1000000, 41.2}}
[/quote]
In[2]:= logx = Log[10.0, Take[Flatten[data], {1, -1, 2}]]
Out[2]= {1., 1.47712, 2., 2.47712, 3., 3.47712, 4., 4.47712, 5.,
5.47712, 6.}
In[3]:= y = Take[Flatten[data], {2, -1, 2}]
Out[3]= {34.2, 36.7, 38.2, 39, 39.6, 40, 40.4, 40.6, 40.8, 41.1, 41.2}
In[4]:= log10data = Transpose[{logx, y}]
Out[4]= {{1., 34.2}, {1.47712, 36.7}, {2., 38.2}, {2.47712, 39}, {3., 39.6},
[quote]{3.47712, 40}, {4., 40.4}, {4.47712, 40.6}, {5., 40.8}, {5.47712,
41.1},[/quote]
[quote]{6., 41.2}}
[/quote]
In[6]:= fit = Fit[log10data, {1, k, k^2, k^3}, k] /. k -> Log[10, x]
2 3
Out[6]= 28.2703 + 3.34087 Log[x] - 0.312086 Log[x] + 0.0100491 Log[x]
[quote]
3) Assuming I can get a Plot of the fit, and a ListPlot of the
original data, would the best way to show them on the same graph be to
use Show[], or is there a better way? I>ve had problems doing this in
the past, where I>ve never managed to work out what the axes will be
or the label, PlotLabel etc.
[/quote]
This plots the two. The fit is not very good, but ...
Show[Plot[fit, {x, 10, 1000000}], ListPlot[data]]
Dave |
|