Trying to identify what algorythm I need to use to predict a battery full time (100%), based on historic data (charge rate is variable).

I suspect it's some form of linear regression of a sort, however I don't really know. It's best if I visualize what i'm trying to do:

enter image description here

Eventually I want to do this in code, but if I can get it working in excel, or understand the maths behind, that would be a good start.

  • 5
    $\begingroup$ Plot your data.. $\endgroup$ Mar 27 at 9:29
  • 3
    $\begingroup$ Welcome to the Mathematica Stack Exchange. This stack site is about the technical computing software called Mathematica and the associated Wolfram Language. This question is technically off-topic unless you want to solve it using Mathematica. As a start, you will have to edit the post to indicate that and also load your copy-paste-able data and any Mathematica code that you have tried so far. Thanks. $\endgroup$
    – Syed
    Mar 27 at 9:56

1 Answer 1


Your question ended up being rather amusing to play with so I ended up with some over-the-top method.

The data

(* the data *)
battery = {10, 20, 30, 45, 48, 50, 55, 60, 77};
time = {
   TimeObject[{9, 10, 0}],
   TimeObject[{9, 35, 0}],
   TimeObject[{10, 30, 0}],
   TimeObject[{11, 0, 0}],
   TimeObject[{11, 35, 0}],
   TimeObject[{12, 0, 0}],
   TimeObject[{12, 30, 0}],
   TimeObject[{13, 0, 0}],
   TimeObject[{13, 45, 0}]

(* moving from times to absolute values *)
diff = Accumulate[{0}~Join~
     Table[time[[i + 1]] - time[[i]], {i, Length@time - 1}]]];
data = {battery, diff}\[Transpose];

(* plotting *)
ListPlot[data, Frame -> {True, True, False, False}, 
 FrameLabel -> {"Batery charge [%]", "Time [minutes]"}, 
 FrameStyle -> Directive[Black, 14], PlotStyle -> {Black}]

As you can see, the charging behaviour changes at around 50%, and the charging rate becomes slower. (Note that I've inverted the axes on purpose.)

enter image description here

Fitting the data

The simplest is to find some asymptotic functions. I'm sure there are better ways using R-square and bla, but I'm too lazy to sit down and write that code. So i'm just using the most basic functions I can think of.

funcs = {a + b/x, a Log[ x] + b, (a x)/Sqrt[1 + x^2] + c, 
   a ArcTan[ x] + c};
stdFits = NonlinearModelFit[data[[-6 ;;]], #, {a, b, c}, x] & /@ funcs;
Show[{Plot[Evaluate[#[x] & /@ stdFits], {x, 0, 100}, 
   PlotRange -> {{0, 105}, {-5, 400}}, PlotLegends -> funcs, 
   Epilog -> {Red, 
     AbsolutePointSize[4], (Point[{100, #[100]}] & /@ stdFits)}], 
   Evaluate@(Callout[{100, #[100]}, #[100], Left] & /@ stdFits)]}]

enter image description here

Using (the more fun) Predict

methods = {"DecisionTree", "GradientBoostedTrees", "NearestNeighbors",
    "NeuralNetwork", "RandomForest", "GaussianProcess"};
assoc = MapThread[#1 -> #2 &, {battery, diff}];
p = Predict[assoc, Method -> #, PerformanceGoal -> "Quality"] & /@ 

   Plot[Evaluate[#[x] & /@ p], {x, 0, 100}, 
    Frame -> {True, True, False, False}, 
    FrameLabel -> {"Batery charge [%]", "Time [minutes]"}, 
    FrameStyle -> Directive[Black, 14], PlotLegends -> methods], 
  ListPlot[data, PlotStyle -> {Black}, PlotMarkers -> "OpenMarkers"],
  ListPlot[{Callout[{100, p[[4]][100]}, p[[4]][100], Left]}]}]

enter image description here

Out of all the models, the "NeuralNetwork" method seems to give the more reasonable answer of about 308 minutes, or about 5 hours or so.

  • $\begingroup$ I mean, you threw a whole bunch of tree based algorithms in there, of course they are going to do a miserable job. This seems to beg for some sort of logistic regression, maybe some other distributional regression for duration like cox proportional hazards... $\endgroup$
    – Ryan
    Mar 27 at 20:22
  • $\begingroup$ Of course! But I wanted to make a 'fair' comparison between all the other methods that were available to Predict. I didn't care in particular about accuracy, I was just playing a bit with the data. :) As I said in the answer, I couldn't be bothered to do it the 'clean' way. But I might revisit it when I'll have more time. $\endgroup$
    – alex
    Mar 27 at 20:49

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