Message Boards Message Boards

0
|
3554 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Apply a function to a list?

Posted 8 years ago

Hi Mathematica Community!

I am very new to Mathematica and I am trying to apply a function which composed of many different functions to 50 lists of dimension {855 rows,3 columns}. I just need some guide on this and I should be able to do the rest. Thanks for spending your time. Appreciated ^_^

* In Line (1), I imported my data in a csv file (a sample is attached to this post) to be used in "NonlinearModelFit" *  

        syndata = Import["/nsm/home/junhuiye/Documents/Jun/CombinedThermalMelt_h2A.\ csv"];  - (1)

* In Line (2), I generated gaussian white noise to be added to syndata which has a dimension of {50,855,3} => 50 of (855 rows x 3 columns) matrix*


trial = Table[{0, 0, #} & /@ RandomVariate[NormalDistribution[0, i], 855], {i, 0.01, 0.5, 0.01}]; - (2)

* In Line (3), I make 50 copies of syndata to create a {50,855,3} list to add to trial *  

  syndatasum = Table[syndata, 50]; -(3)

* In Line (4), I created noisy data by adding gaussian white noise to my data=> this will give me 50 lists of 855 rows  x 3 columns *

  noisydata = trial + syndatasum; -(4)

This part is where I am struggling with. I am trying to apply function f1 to noisydata, but the tricking part is I want to apply f1 to each of the 50 lists of (855 rows x 3 columns) then run to obtain a set of parameters. For instance, Applying f1 to first list of (855 rows x 3 columns) => obtain the fitted parameters and repeat for the next 49 lists of (855x3) matrix and get a set of fitted parameters for each of the list. In the end, I want to combined all these fitted parameters and make it into a table matrix of size ( 50 rows x 11 columns). I don't want to run all 50 lists at the same time because the "NonlinearModelFit" does not allow me to do that. My question is should I pull out Line (5) from the function f1? Is there any other way to approach this problem? Thanks again for your time.

f1:= {

fullData1 =noisydata;  - (5)

thetaF[a_, b_, c_, temp_, den_] := a + b*temp + c*den; -  (6)

thetaU[d_, e_, f_, g_, temp_, den_] := d + e*temp + f*temp^2 + g*den; - (7)

dG[dH_, dCP_, m_, Tm_, temp_, den_] := dH*(1 - temp/Tm) - dCP*((Tm - temp) + temp*Log[temp/Tm]) + (-m)*den; - (8)

ellip[a_, b_, c_, d_, e_, f_, g_, dH_, dCP_, m_, Tm_, temp_, den_] := (thetaF[a, b, c, temp, den] + thetaU[d, e, f, g, temp, den]* Exp[-dG[dH, dCP, m, Tm, temp, den]/(0.00198*temp)])/(1 + Exp[-dG[dH, dCP, m, Tm, temp, den]/(0.00198*temp)]) - (9)

fullFit = NonlinearModelFit[fullData1, ellip[a, b, c, d, e, f, g, dH, dCP, m, Tm, temp, den], {{a, -49591.2}, {b, 115.462}, {c, -1488.46}, {d, 116936}, {e, -845.923}, {f, 1.37815}, {g, 1800}, {dH, 95}, {dCP, 0.94}, {m, 2}, {Tm, 400}}, {temp, den}] - (10)

fullFit["AdjustedRSquared"] - (11)

fullFit["ParameterTable"] - (12)

fitA = a /. fullFit["BestFitParameters"]; - (13)

fitB = b /. fullFit["BestFitParameters"]; - (14)

fitC = c /. fullFit["BestFitParameters"]; - (15)

fitD = d /. fullFit["BestFitParameters"]; - (16)

fitE = e /. fullFit["BestFitParameters"]; - (17)

fitF = f /. fullFit["BestFitParameters"]; - (18)

fitG = g /. fullFit["BestFitParameters"]; - (19)

fitTm = Tm /. fullFit["BestFitParameters"]; -(20)

TmC = fitTm - 273.15 -(21)

minx = Min[fullData1[[All, {1}]]]; - (22)

maxx = Max[fullData1[[All, {1}]]]; -(23)

miny = Min[fullData1[[All, {2}]]]; - (24)

maxy = Max[fullData1[[All, {2}]]]; - (25)

minz = Min[fullData1[[All, {3}]]]; - (26)

maxz = Max[fullData1[[All, {3}]]]; - (27)

Show[ListPointPlot3D[fullData1], Plot3D[fullFit[x, y], {x, minx, maxx}, {y, miny, maxy}, PlotRange -> {{minx, maxx}, {miny, maxy}, {minz, maxz}}, PlotStyle -> Opacity[0.8], ColorFunction -> (ColorData["TemperatureMap"][#3] &)], MeshStyle -> Gray, PlotRange -> {{minx, maxx}, {miny, maxy},{minz, maxz}}] - (28)

Show[ListPointPlot3D[fullData1],Plot3D[thetaF[fitA, fitB, fitC, x, y], {x, minx, maxx}, {y, miny, maxy}, PlotRange -> {{minx, maxx}, {miny, maxy}, {minz, maxz}}, PlotStyle -> Opacity[0.2]], Plot3D[thetaU[fitD, fitE, fitF, fitG, x, y], {x, minx, maxx}, {y, miny, maxy}, PlotRange -> {{minx, maxx}, {miny, maxy}, {minz, maxz}}, PlotStyle -> Opacity[0.5]], PlotRange -> {{minx, maxx}, {miny, maxy}, {minz, maxz}}] - (29)

}
Attachments:
POSTED BY: Jun Hui Yeoh
2 Replies
Posted 8 years ago

Hi Bill ! Thanks and I really appreciate your help. ^_^

POSTED BY: Jun Hui Yeoh
Posted 8 years ago

If you have a list of lists and you want to apply the same set of functions to each of those lists, then usually you something like this:

First create some very simple concrete example data

In[1]:= listoflists = RandomReal[{-1, 1}, {3, 4}]

Out[1]= {{0.19093, 0.409124, 0.54653, -0.226395},
         {0.550909, -0.904404, 0.766294, 0.874693},
         {-0.153782, 0.0823147, -0.612925, -0.17592}}

Then create a function which returns a list of the functions you want to use. Each of those functions must accept a list and returns a result.

myfuns[v_] := {Plus @@ v, Min @@ v, Max @@ v, Dimensions[v]};

And finally use Map to give your list of functions each of your sublists, one at a time.

In[3]:= Map[myfuns, listoflists]

Out[3]= {{0.920189, -0.226395, 0.54653, {4}},
         {1.28749, -0.904404, 0.874693, {4}},
         {-0.860312, -0.612925, 0.0823147, {4}}}

And that gives you for each of your sublists the total, the minimum, the maximum values and the dimensions of the sublist.

For the individual functions that you want to use, if they accept a list (or a matrix), like Total, then you use Total[v]. On the other hand, if they want individual arguments, like Min, then you use

Min@@v

Try this on simple examples first and see if you can gain confidence in how it works. Once you have that then maybe you can throw it at 50 matricies.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract