How to plot the derivative from experimental data
584 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Star Strider
2014 年 5 月 19 日
編集済み: Star Strider
2019 年 3 月 25 日
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(:));
5 件のコメント
Star Strider
2022 年 1 月 1 日
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
2023 年 9 月 15 日
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.
その他の回答 (1 件)
Abhinendra Singh
2017 年 11 月 27 日
Hello, Can any one of you please post a working example of the above problem?
I appreciate the help!
3 件のコメント
John D'Errico
2022 年 1 月 1 日
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
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!