Effacer les filtres
Effacer les filtres

How to plot the derivative from experimental data

630 vues (au cours des 30 derniers jours)
Sreedhar
Sreedhar le 19 Mai 2014
Hi I have a number of points (experimental data) plotted as an x-y plot. I want to generate the derivative of y w.r.t x from this plot. Is there a function in MATLAB which can do this ?
TIA

Réponse acceptée

Star Strider
Star Strider le 19 Mai 2014
Modifié(e) : Star Strider le 25 Mar 2019
Not a specific MATLAB function, but it’s easy:
dydx = diff(y(:))./diff(x(:));
If you want dydx to be the same length as x and y (so you can plot it against x), ‘zero-pad’ the first value with eps:
dydx = diff([eps; y(:)])./diff([eps; x(:)]);
Both produce a column vector, so you may have to transpose it if x is a row vector in order to plot it with the others.
UPDATE — (24 Mar 2019 00:30)
A much more accurate approach would be:
dydx = gradient(y(:)) ./ gradient(x(:));
See the documentation section on the gradient (link) function for details.
  5 commentaires
Star Strider
Star Strider le 1 Jan 2022
Assuming vector arguments, the diff function takes the differences between successive elements of the vector, so the outputt is one element shorter than the arguments. The gradient function uses a central difference approximation of the derivative (except at the ends, where it calculates a simple difference) so the output has the same number of elements as the argument.
See the documentation on both for details.
.
Shiva Vikram Bhagavatula
Shiva Vikram Bhagavatula le 15 Sep 2023
The function sampling may be poor at some or all points if completely different results are obtained for diff and gradient. For example,let the derivative be calculated at point 2 in a set of three points (p1,p2,p3). Assuming the spacing along the independent variable axis is dx, diff produces (p2-p1)/dx . Gradient produces (p3-p1)/(2dx). For them to be equal, the condition is (p3+p1)/2=p2,i.e; all three points are collinear( lie on the same straight line). The difference between gradient and diff would be a measure of the deviation of the points from the collinear fit.

Connectez-vous pour commenter.

Plus de réponses (1)

Abhinendra Singh
Abhinendra Singh le 27 Nov 2017
Hello, Can any one of you please post a working example of the above problem?
I appreciate the help!
  3 commentaires
Shahlan Ahmad
Shahlan Ahmad le 1 Jan 2022
good work
John D'Errico
John D'Errico le 1 Jan 2022
Um, only one call to gradient needed.
x = 0:0.1:10;
y = sin(x);
plot(x, gradient(y,x));
When gradient is called with TWO arguments, it assumes you have passed in x also as the second argument. Now it computes a derivative estimate at each point. A simple finite difference scheme is used.
help gradient
GRADIENT Approximate gradient. [FX,FY] = GRADIENT(F) returns the numerical gradient of the matrix F. FX corresponds to dF/dx, the differences in x (horizontal) direction. FY corresponds to dF/dy, the differences in y (vertical) direction. The spacing between points in each direction is assumed to be one. When F is a vector, DF = GRADIENT(F) is the 1-D gradient. [FX,FY] = GRADIENT(F,H), where H is a scalar, uses H as the constant spacing between points in each direction. [FX,FY] = GRADIENT(F,HX,HY), when F is 2-D, uses the spacing specified by HX and HY. HX and HY can either be scalars to specify the spacing between coordinates or vectors to specify the coordinates of the points. If HX and HY are vectors, their lengths must match the corresponding dimension of F. [FX,FY,FZ] = GRADIENT(F), when F is a 3-D array, returns the numerical gradient of F. FZ corresponds to dF/dz, the differences in the z direction. GRADIENT(F,H), where H is a scalar, uses H as the constant spacing between points in each direction. [FX,FY,FZ] = GRADIENT(F,HX,HY,HZ) uses the spacing given by HX, HY, HZ. [FX,FY,FZ,...] = GRADIENT(F,...) extends similarly when F is N-D and must be invoked with N outputs and either 2 or N+1 inputs. Note: The first output FX is always the gradient along the first dimension of F, going across columns. The second output FY is always the gradient along the second dimension of F, going across rows. For the third output FZ and the outputs that follow, the Nth output is the gradient along the Nth dimension of F. Examples: x = -2:.2:2; y = (-1:.15:1).'; z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.2); contour(x,y,z), hold on quiver(x,y,px,py), hold off Class support for input F: float: double, single See also DIFF, DEL2. Documentation for gradient doc gradient Other functions named gradient gpuArray/gradient sym/gradient

Connectez-vous pour commenter.

Catégories

En savoir plus sur Descriptive Statistics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by