- Use a tool to interpolate the data.
- Differentiate the interpolant.
- Evaluate the derivative at that point.
how do i find the derivative dT/dx at certain coordinates?
4 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
John D'Errico
on 24 Jul 2023
Edited: John D'Errico
on 24 Jul 2023
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)
How well did we do? We know the true derivative, as just cos(x)
cos(xder)
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.
0 Comments
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.)
.
0 Comments
See Also
Categories
Find more on Splines 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!
