how do i find the derivative dT/dx at certain coordinates?

4 views (last 30 days)
I have a data set, T, and I need to find the derivative dT/dx at coordinate (0,0.74) where several plot lines overlap. how should i do this?

Answers (2)

John D'Errico
John D'Errico on 24 Jul 2023
Edited: John D'Errico on 24 Jul 2023
  1. Use a tool to interpolate the data.
  2. Differentiate the interpolant.
  3. Evaluate the derivative at that point.
For example...
x = 0:0.5:10;
T = sin(x);
plot(x,T,'o')
xder = 7.4;
xline(xder)
So just made up data. Then I picked a point where I want to infer the approximate derivative. It will be no better than my interpolant of course. And if your data is noisy, then expect complete trash but this scheme, since differentiation will amplify any noise. In that case, you would perhaps be best off using a smoothing spline.
spl = spline(x,T); % a simple interpolant, good if the data is smooth
spld = fnder(spl); % differentiate the spline interpolant
fnval(spld,xder)
ans = 0.4394
How well did we do? We know the true derivative, as just cos(x)
cos(xder)
ans = 0.4385
So not too bad. About as well as I would expect. Again, remember that this was a coarsely sampled set of points, and any such scheme will be at best an approximation. The virtue of this scheme is it will work for ANY value of x. Not only values that were in your original list of points.
Again, if your data is noisy, then you NEED to perform smoothing FIRST. So you might use a smoothing spline. Or you might want to try some sort of smoothing operator on the data. That would be your choice, but a smoothing spline makes more sense to me, since it performs both the curve fit AND the smoothing in one step.

Star Strider
Star Strider on 24 Jul 2023
Use the gradient function to calculate the numerical derivative. It works with matrices as well as vectors, and getting the derivative of an irregularly-spaced vecttor or matrix can be accomplished with element-wise division —
x = linspace(0, 1, 250).';
T = sin(2*pi*x) + randn(size(x))/100;
dTdx = gradient(T) ./ gradient(x);
figure
plot(x, T, 'DisplayName','T(x)')
hold on
plot(x, dTdx, 'DisplayName','dT(x)/dx')
hold off
legend('Location','best')
Using it with matrices requires a bit of care, because it will calculate the numerical gradient across both rows and columns, so you need to determine which output to use for what you want to calculate. (If I had your data and a bit more information about what you want to do, I could provide a more precise result. This will work as a 1D illustration in their absence.)
.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!