0

I am working on some problems with curve fitting ect, and need to find the derivative of a cubic fit of 50 points. The questions asks to find the growth rate of bacteria given data. My current code is

time = [1,2,4,5,7,9];
bacteria = [2000,4500,7500,15000,31000,64000];

rcubic = polyfit(time,bacteria,3);
newTime = linspace(1,7,50);
vrcubic = polyval(rcubic,newTime);

growthRate = [diff(vrcubic)./diff(newTime)];
derivative = diff(vrcubic)

i am wondering whether growthRate or derivative is correct for this problem, or if they are both wrong. as they given markedly different values. Also as the length of the vector is shortened.

Cheers

Vladamir
  • 247
  • 1
  • 3
  • 11
  • `growthRate` would be representative of the 'physical' growth rate. However, I think it may be best to replace `newTime = linspace(1,7,50);` with `newTime = linspace(time(1), time(end), 50);` –  Apr 30 '16 at 07:48

1 Answers1

0

The derivative of vrcubic with respect to time is indeed given by

growthRate = [diff(vrcubic)./diff(newTime)];

Your derivative expression is just the differences between neighboring datapoints.

You can also use analytical differentiation here

growthRate = polyval( polyder(rcubic), newTime );

I would always prefer the latter as it is the correct derivative even in case the newTime points are very sparsely sampled.

Andreas H.
  • 5,557
  • 23
  • 32
  • Thanks for the reply, i am applying this to a second question involving velocity and acceleration. It works fine for velocity, however applying the same function to the velocity vector in an attempt to get the second derivative causes values to be in the order of power 110 (1x10^100). the code is velocity = polyval(polyder(rcubic),newTime) acceleration = polyder((velocity),newTime) – Vladamir Apr 30 '16 at 12:12