Unrecognized function or variable 'FirstDerivative'

5 views (last 30 days)
Hi guys,
I'm having trouble with a MATLAB script given to me by my honours supervisor and I need this data analysed by Friday for my final presentation.
The error message I receive is the following on line 76 (highlighted in bold below)
Unrecognized function or variable 'FirstDerivative'.
Error in ImpactAnalysis (line 76)
acceleration(:, footwear, site, trial) = FirstDerivative(velocity(:, footwear,
site, trial), 5000, 100, .0004);
When I change the 'FirstDerivative' function to 'diff' I then get this error:
Error using diff
Too many input arguments.
Error in ImpactAnalysis (line 76)
acceleration(:, footwear, site, trial) = diff(velocity(:, footwear, site, trial),
5000, 100, .0004);
Please help, I need this data to pass my Honours unit.
_____________________________________________________________________________________________________
load ..\ImpactTestFiles\Calibration\gain.txt
%load ImpactData.mat
for footwear = 1 %1:4
for site = 1 %:2
for trial = 1:1
XlsFilename = ['C:\Users\Julian\Desktop\ImpactTestFiles\RawData\' char(FootwearName(footwear)) char(ShoeSite(site)) num2str(trial) 'ScaledData.xlsx'];
data = xlsread(XlsFilename);
display(['Calculating for ' char(FootwearName(footwear)) char(ShoeSite(site)) num2str(trial)])
% Find beginning and end of impact data
InitialForce = mean(data(1:100, 2));
InitialForceSD = std(data(1:100, 2));
InitialPosition = mean(data(1:100, 3));
% Find initial
count = 1;
while data(count, 2) < 500
count = count + 1;
end
FirstTry = count - 1;
count = FirstTry;
while data(count, 2) > InitialForce + 20
count = count - 1;
end
InitialStrikeTiming = count - 2;
StartData = InitialStrikeTiming - 100;
EndData = InitialStrikeTiming + 500;
ImpactData = (data(StartData:EndData, :));
force(:, footwear, site, trial) = ImpactData(:, 2);
figure(1);clf;hold on;plot(force(:, footwear, site, trial));plot(100, force(100, footwear, site, trial),'m*');pause
position(:, footwear, site, trial) = smoothit(ImpactData(:, 3), 5000, 100) / 1000;
% [DataDerivatives] = DisVelAcc(position(:, footwear, site, trial), 5000);
% velocity(:, footwear, site, trial) = DataDerivatives.vel;
velocity(:, footwear, site, trial) = smoothit(ImpactData(:, 4), 5000, 400) / 1000;
acceleration(:, footwear, site, trial) = FirstDerivative(velocity(:, footwear, site, trial), 5000, 100, .0004);
_____________________________________________________________________________________________________
  6 Comments
Alessandra Marcelo
Alessandra Marcelo on 18 Sep 2020
Thanks so much for the explanation! That really helps me get started with troubleshooting this. Hopefully I can figure it out.
Jon
Jon on 18 Sep 2020
Using gradient(y)./gradient(x) is a great suggestion for dealing with non-uniform increments! I like the way it ensures the resulting derivative has the same length as the original y vector. I'll remember this, thank you

Sign in to comment.

Accepted Answer

Jon
Jon on 18 Sep 2020
Edited: Jon on 18 Sep 2020
It looks like you are either missing the function FirstDerivative or maybe it is in a directory somewhere but that location is not on your MATLAB path.
I would suggest first browsing through the directories where your code is to see if you can find a FirstDerivative.m file somewhere. There is also a find Find Files button on the Home Tab of the MATLAB menu bar. If you can know the file is there but MATLAB does not seem to find it then you need to add the directory where it is to your path.
You can do this with the "Set Path" button on the Home Tab of the MATLAB menu bar.
If you really are missing the file and you can't get it from your advisor, then you can fairly easily compute the numerical derivative yourself.
Suppose you have some variable x that you want to differentiate, and that the time increment between samples of your variable is given by a variable deltaT, Two ways are
xdot = diff(x)/deltaT
and
xdot = gradient(x,deltaT)
both more or less simply approximate the derivative as xdot(n) = (x(n) - x(n-1))/deltaT
The first way does exactly that. The second way takes a little more care around the end points.
With the first way you end up with an xdot that is one element shorter than the x which is not so nice and needs to be handled with a little care. The second way (gradient) your xdot and x have the same length.
If your time increments are not constant you will have to use the first way though in which case if you have a vector of time increments you could use
xdot = diff(x)./deltaT % perform element by element division with varying deltaT
  3 Comments
Alessandra Marcelo
Alessandra Marcelo on 18 Sep 2020
All good, thank you so so much for your detailed and very patient explanation! This helps a lot, hopefully I can figure it out. I really appreciate your help.
Jon
Jon on 18 Sep 2020
Star Stider's suggestion of differentiating (changing notation slightly to be consitent with my examples above) a variable given by a vector x by a vector t where the time increments are not uniform using:
xdot = gradient(x)./gradient(t)
Is a great suggestion! I'll have to remember this trick too.
It avoids problems with using diff(x)./diff(t) which would result in xdot being shorter by one element than x

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!