1
$\begingroup$

I have a set of data:

Rawdata={-0.462475, -0.477504, -0.393686, -0.217328, 0.105685, 0.443379, 0.627285, 0.534236, 0.165977, -0.296604, -0.432765, 0.0851257, 0.136757, 0.0293745, -0.411645, -0.322851, 0.046951, 0.492951, 0.59958, 0.294237, -0.182859, -0.398524, 0.0981432, 0.0930264, 0.0744801, -0.335487, -0.211728, 0.162959, 0.499504, 0.520212, 0.16799, -0.234034, -0.319659, 0.0952872, 0.107103, 0.335255, 0.43733, 0.146607, -0.190577, -0.261758, -0.0625729, 0.2634, 0.472639, 0.368261, 0.0979895, 0.0735513, 0.172949, 0.418761, 0.330992, 0.0074911, -0.216614, -0.215464, 0.0696046, 0.329594, 0.408547, 0.0894006, 0.126135, 0.165816, 0.307273, 0.185918, -0.0112422, -0.133738, -0.0724032, 0.101639, 0.292775, 0.301708, 0.0877797, 0.0876761, -0.025518, -0.0306193, 0.0928805, 0.199542, 0.243624, 0.1745, 0.0162858, -0.0430322, -0.0219115, 0.0896729, 0.0959586, 0.110228, 0.179314, 0.172237, 0.0997888, 0.0101023, -0.0105142, 0.0445394, 0.118966, 0.189803, 0.109097, 0.0858361, 0.100032, 0.143901, 0.115334, 0.125749, 0.0604358, 0.0409949, 0.083558, 0.117466, 0.137262, 0.094553, 0.0880441, 0.100834, 0.0929723, 0.0952319, 0.100924, 0.118724, 0.0820776, 0.103751, 0.0865428, 0.0833089, 0.101332, 0.0920292, 0.0639722, 0.0706576, 0.0882164, 0.11293, 0.134228, 0.124789, 0.0925625, 0.0566572, 0.0675977, 0.081165, 0.0899243, 0.117133, 0.0894329, 0.027364, 0.0357121, 0.0987479, 0.149997, 0.148336, 0.11258, 0.0753185, 0.113732, 0.115019, 0.101291, 0.160841, 0.187044, 0.116901, 0.038164, 0.0141272, 0.0476076, 0.123504, 0.170103, 0.0967143, 0.0901756, 0.010208, 0.0165788, 0.055284, 0.128853, 0.178452, 0.177033, 0.0751056, 0.00724638, 0.0312522, 0.0948593, 0.109623, 0.0539488, 0.00278435, 0.0385292, 0.112214, 0.179139, 0.171794, 0.105018, 0.0370275, 0.00582058, 0.116097, 0.0961575, 0.0632425, 0.151793, 0.164298, 0.130958, 0.0701233, 0.00879883, 0.0510383, 0.106122, 0.153237, 0.0852388, 0.0869375, 0.139951, 0.162659, 0.132968, 0.0719273, 0.0519352, 0.0302522, 0.111082, 0.145829, 0.148304, 0.0970358, 0.0906806, 0.0785693, 0.0470446, 0.063254, 0.0882698, 0.101144, 0.132672, 0.111858, 0.0790596, 0.0948081, 0.0792895, 0.094402, 0.0868104, 0.0774563, 0.0849362, 0.116133, 0.13637, 0.109352, 0.103358, 0.0842258, 0.0688512, 0.0901025, 0.119531, 0.0992231, 0.11737, 0.109138, 0.11662, 0.0969882, 0.0769638, 0.0877883, 0.10809, 0.119633, 0.101203, 0.0945034, 0.0861549, 0.0616248, 0.0666033, 0.0918489, 0.102061, 0.138445, 0.137202, 0.111695, 0.0843132, 0.0711831, 0.0577217, 0.0565093, 0.137122, 0.181921, 0.180176, 0.114756, 0.0663453, 0.0190868, 0.0620324, 0.120716, 0.10791, 0.118056, 0.11861, 0.0399656, -0.00972457, 0.0280447, 0.0976789, 0.188359, 0.203561, 0.145988, 0.0572668, 0.0970899, 0.117154, 0.0833913, 0.0192099, -0.0336157, 0.0212292, 0.126909, 0.186489, 0.213497, 0.16576, 0.0464841, 0.0928235, 0.0831726, 0.194656, 0.241966, 0.178094, 0.0685291, -0.0383825, -0.0222282, 0.0421295, 0.167092, 0.226146, 0.0840057, 0.113883, 0.0668346, 0.157052, 0.243934, 0.235909, 0.148691, 0.0298074, -0.0436559, -0.0196858, 0.087831, 0.118569, 0.0808441, 0.1652, 0.238137, 0.120931, 0.0284646, -0.0401501, -0.0219231, 0.0894716, 0.187669, 0.244263, 0.104944, 0.0957649, 0.0447436, 0.103991, 0.217513, 0.239228, 0.201135, 0.0951626, -0.0299545, -0.0548929, 0.031694, 0.107958, 0.150573, 0.18525, 0.080512, -0.00369004, -0.0468405, 0.0360111, 0.132843, 0.226403, 0.222974, 0.125393, 0.11251, 0.126242, 0.233834, 0.163337, 0.0939386, 0.0213852, -0.0165399, 0.0483103, 0.137372, 0.209622, 0.200961, 0.116739, 0.11108, 0.186125, 0.180642, 0.110386, 0.0564929, -0.0132962, 0.0471397, 0.106225, 0.170968, 0.186725, 0.118726, 0.09382, 0.073343, 0.12772, 0.151153, 0.161, 0.121048, 0.0730062, 0.0191693, 0.0229224, 0.103218, 0.101597, 0.101414, 0.127825, 0.160381, 0.123607, 0.077135, 0.0437015, 0.0413213, 0.0884286, 0.126512, 0.14602, 0.114284, 0.104335, 0.100021, 0.101678, 0.0803154, 0.0763461, 0.0535074, 0.0951762, 0.0963413, 0.115596, 0.112413, 0.105004, 0.108572, 0.0846231, 0.110065, 0.0755513, 0.0918242, 0.111067, 0.0843015, 0.0797037, 0.103031, 0.0974963, 0.0873716, 0.0974615, 0.121654, 0.0900736, 0.092349, 0.0899691, 0.0608037, 0.0774372, 0.10331, 0.13613, 0.111823, 0.0789723, 0.082949, 0.140698, 0.0948199, 0.0786218, 0.0472427, 0.0687623, 0.0749091, 0.118445, 0.141406, 0.124413, 0.10868, 0.11092, 0.118747, 0.121574, 0.10521, 0.073375, 0.053598, 0.0856549, 0.124248, 0.132137, 0.132778, 0.0954054, 0.107204, 0.0761265, 0.0843766, 0.10071, 0.135022, 0.147052, 0.114702, 0.0732644, 0.058628, 0.0827127, 0.110792, 0.109074, 0.0928544, 0.146601, 0.135908, 0.118201, 0.0717454, 0.0492275, 0.0608672, 0.0884621, 0.137157, 0.0785544, 0.0999297, 0.0866411, 0.0978298, 0.151161, 0.132314, 0.12448, 0.0741268, 0.0321637, 0.0712278, 0.117593, 0.109783, 0.0825315, 0.125198, 0.0847295, 0.0653004, 0.0783939, 0.0863342, 0.122781, 0.137911, 0.124709, 0.0834643, 0.106041, 0.0995328, 0.115609, 0.141324, 0.119285, 0.113149, 0.0841362, 0.0960893, 0.0908578, 0.119849, 0.136478, 0.106098, 0.08756, 0.101115, 0.0889186, 0.117936, 0.114559, 0.10105, 0.0935653, 0.0832678, 0.0925567, 0.102049, 0.0961035, 0.075863, 0.106154, 0.10837, 0.0950285, 0.104923, 0.0991505, 0.088163, 0.0930724, 0.103383, 0.107164, 0.10216, 0.0919158, 0.0920803, 0.0792764, 0.0870697, 0.107748, 0.116857, 0.143839, 0.133452, 0.0939433, 0.085644, 0.117072, 0.0934091, 0.0770135, 0.0878632, 0.0976638, 0.119022, 0.111126, 0.119448, 0.0959723, 0.0828836, 0.0531737, 0.0795022, 0.08512, 0.118981, 0.0838361, 0.0402141, 0.0434374, 0.0486867, 0.116763, 0.165252, 0.15056, 0.12431, 0.0964232, 0.119511, 0.132874, 0.103763, 0.0634226, 0.0648683, 0.0189996, 0.0839863, 0.127542, 0.158943, 0.146704}

and I apply the For loop:

For[(i = 1; j = 11), i < 540 && j < 540, (i += 11; j += 11), 
Print[nlm =  NonlinearModelFit[Part[Rawdata, i ;; j], (a*Sin[b t + c]) + 
 d, {{a, 1}, {b, 0.3}, {c, 8}, {d, -0.5}}, t], Max[Table[nlm[t], {t, 0, 10}]]]]

This gives me two outputs, one is the equation of the fitted model and the other is the peak value of the sine wave that has been fitted to the data. I want to output these peak values as a table. Is there a way of doing this using, for example, PutAppend (it isn't necessary for the data to be stored to a separate file)?

$\endgroup$
1
$\begingroup$

I used your data, and I'm not overly familiar with For loops so I rewrote the code using a Do loop. They're largely similar as far as I understand, but for what you need (outputting values to a table) a Do loop is very intuitive.

A For loop takes the initial value (i=1), checks that the value is below a threshold (i<540) and as long as that's true it runs the code and applies an increment. I messed around with it but couldn't find exactly when the increment is applied - this is where Do loops come in.

 output = {}; incr = 11;
 Do[
 j = i + (incr-1);

 nlm = Quiet@
   NonlinearModelFit[
    Rawdata[[i ;; j]], (a*Sin[b t + c]) + 
     d, {{a, 1}, {b, 0.3}, {c, 8}, {d, -0.5}}, t];

 xmax = Max[Table[nlm[t], {t, 0, 10}]];

 AppendTo[output, xmax];

 , {i, 1, 539 - (incr - 1), incr}];

Since i and j in your code are related I replaced j with i+(incr-1), with the increment you specified as incr=11. the code

output={}; incr=11;

Defines not only the list we're going to output values to (it doesn't have to be empty) and the incr we're going to apply.

What happens: the Do loop opens, and i is defined as i=1 (bottom line). j is then defined as i+10 (so j=11 for the first round). Then your code is unchanged, the NLMF is run (I added a Quiet@ to remove annoying errors) and I changed your

Part[Rawdata,i;;j]

to

Rawdata[[i;;j]]

because they're equivalent but one takes less typing, I defined a new variable

xmax = Max[Table[nlm[t], {t, 0, 10}]];

as the maximum value of the fit, and the AppendTo argument puts the value of xmax into the empty table output. The Do loop then restarts, with a new value of

i = i + incr 

and it will continue to loop until

j = 539 or i = j - 10 = 539 - (incr-1)

When the loop finishes, the list output should have the required data.

The result I got using your data was:

ListPlot[output,Joined->True, PlotRange->All]

Plot of output Data

| improve this answer | |
$\endgroup$
1
$\begingroup$

Using your version Append would work.

    output = {};
For[(i = 1; j = 11), i < 540 && j < 540, (i += 11; j += 11), 
 nlm = NonlinearModelFit[
   Part[Rawdata, i ;; j], (a*Sin[b t + c]) + 
    d, {{a, 1}, {b, 0.3}, {c, 8}, {d, -0.5}}, t]; 
 output = Append[output, Max[Table[nlm[t], {t, 0, 10}]]];
 ]

ListPlot[output, Joined -> True, PlotRange -> All]

enter image description here

However using a For loop is not really the Mathematica way to solve a problem. If you look at the excellent answers here examples of good mathematica programming you can learn more about functional programming. A possible solution using Map would be:

Interv = Table[{i, i + 10}, {i, 1, Length@Rawdata, 11}];
Map[(nlm = 
      NonlinearModelFit[
       Part[Rawdata, #[[1]] ;; #[[2]]], (a*Sin[b t + c]) + 
        d, {{a, 1}, {b, 0.3}, {c, 8}, {d, -0.5}}, t];
     Max[Table[nlm[t], {t, 0, 10}]]
     ) &, Interv] // Set[output, #] &;

The list output is identical for both solutions.

| improve this answer | |
$\endgroup$

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.