0
$\begingroup$

Greetings Mathematica fans,

I am currently trying to build an interactive interface to sift through a large amount of data visually. The data consists of many sets containing 70-80 points, which have to be graphed, along with a line that best fits the points. Since there are multiple datasets, this will be different for each one. My solution was to combine the NonlinearModelFit and Plot into one function, since they will abide by the same variables. Direction corresponds to the two relevant dimensions, which have to be plotted side by side. num is the dataset number, since there are multiple.

Model := If[t <= tCollision, v0i t + x0i, v0f t + x0i + tCollision (v0i - v0f)]

finishedmodel[direction_, num_] :=
(fitmodel = NonlinearModelFit[direction[[num]], {Model, .2 < tCollision < .7}, {v0i, x0i, v0f, 
 tCollision}, t];
Plot[fitmodel[t], {t, 0, direction[[num, -1, 1]]},Epilog -> Point[direction[[num]]], AxesLabel -> {HoldForm[Time], HoldForm[Position]},  PlotLabel -> None, LabelStyle -> {GrayLevel[0], Bold}, PlotStyle -> Red, ImageSize -> Medium])

This works quite well, but with one issue. As well as graphing the points and fit model, i also need the coefficients for other calculations. Since they were grouped together in one function, i don't know how to get this information in an efficient way. I could separate them, but they wouldn't use the same function variables, which will be trouble with the interface.

Insert[Grid[{
{"Trial:", SetterBar[Dynamic@num, {1 :> 1, 3 :> 2, 5 :> 3 }]},
{"Object 1: X direction", "Object 1: Y direction"},
{Dynamic@finishedmodel[X, num], Dynamic@finishedmodel[Y, num]},
{"X parameters", "Y parameters"},
{Dynamic[
  NonlinearModelFit[
    X[[num]], {Model, .2 < tCollision < .7}, {v0i, x0i, v0f, 
     tCollision}, t]["BestFitParameters"]], 
 Dynamic[NonlinearModelFit[
    Y[[num]], {Model, .2 < tCollision < .7}, {v0i, x0i, v0f, 
     tCollision}, t]["BestFitParameters"]]},
{"Object 2: X direction", "Object 2: Y direction"},
{Dynamic@finishedmodel[X, num + 1], 
 Dynamic@finishedmodel[Y, num + 1]},
{"X parameters", "Y parameters"},
{Dynamic[
  NonlinearModelFit[
    X[[num]], {Model, .2 < tCollision < .7}, {v0i, x0i, v0f, 
     tCollision}, t]["BestFitParameters"]], 
 Dynamic[NonlinearModelFit[
    Y[[num]], {Model, .2 < tCollision < .7}, {v0i, x0i, v0f, 
     tCollision}, t]["BestFitParameters"]]}
}], {Dividers -> All, Alignment -> Right, Spacings -> 1` {1, 1}}, 
2];

Apologies for the large block of code. My solution was to copy the bulk of the function to find the best fit line for the data, and use the same "num" variable along with Dynamic to have it update depending on the selected dataset. This left me with A LOT of dynamic variables, and this code takes about a minute to run because of that. I'm sure there is something i'm missing involving functions that could make my life a whole lot easier, and make this code run faster.

If anyone has any ideas, i would sincerely appreciate the help.

Will

$\endgroup$
  • $\begingroup$ Could you provide a minimal example of data to work with? $\endgroup$ – Kuba Dec 6 '18 at 14:19
  • $\begingroup$ Here is some of the data saved to the X variable @Kuba pastebin.com/AwfG9vKP It's a list of datasets, so X[[n]] will yield the nth dataset $\endgroup$ – william tepe Dec 6 '18 at 15:32
  • $\begingroup$ Model is not defined. Please edit the question in a way to allows us to run the code. $\endgroup$ – Kuba Dec 6 '18 at 22:19
  • $\begingroup$ Done! :) @Kuba I appreciate the help $\endgroup$ – william tepe Dec 7 '18 at 16:12

Your Answer

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

Browse other questions tagged or ask your own question.