Given a vector x and a vector y, the numerical first derivative should be gradient(y)./gradient(x) for all points specified by x, right? Is that the case even if the vector x is irregularly spaced? (If not, how do you do so?) Also, which finite differences method does this use? Thanks.

 採用された回答

Matt J
Matt J 2023 年 12 月 11 日
編集済み: Matt J 2023 年 12 月 11 日

0 投票

It seems to work:
t=sort( rand(1,1000)*2*pi );
cos_t=gradient(sin(t))./gradient(t);
I=1:20:1000;
plot(t,cos(t),'b--' , t(I), cos_t(I) ,'o'); legend('True','Finite Difference',Location='southeast')

6 件のコメント

Matt J
Matt J 2023 年 12 月 11 日
Also, which finite differences method does this use?
Paul
Paul 2023 年 12 月 11 日
Based on the algorithm in the provided link it doesn't seem like gradient used this way would provide the correct result for data that is unequally spaced wrt to the independent variable, though it might be ok if the spacing is small enough.
With small random spacing
rng(100)
t=sort( rand(1,1000)*2*pi );
cos_t=gradient(sin(t))./gradient(t);
figure
plot(t,cos(t),'b--' , t, cos_t); legend('True','Finite Difference',Location='southeast')
Increase the random spacing
t=sort( rand(1,100)*2*pi );
cos_t=gradient(sin(t))./gradient(t);
figure
plot(t,cos(t),'b--' , t, cos_t); legend('True','Finite Difference',Location='southeast')
Seems like elements of cos_t would correspond to the midpoint between corresponding elements of t
tnew = filter([0.5 0 0.5],1,t);tnew = [mean(t(1:2)) tnew(3:end) mean(t(end-1:end))];
figure
plot(t,cos(t),'b--' , tnew, cos_t); legend('True','Finite Difference',Location='southeast')
Matt J
Matt J 2023 年 12 月 11 日
though it might be ok if the spacing is small enough.
But that's true of any finite differencing algorithm. They all perform badly if the sampling is coarse.
Walter Roberson
Walter Roberson 2023 年 12 月 11 日
You only need one gradient() call
rng(100)
t=sort( rand(1,1000)*2*pi );
cos_t = gradient(sin(t), t);
plot(t,cos(t),'b--' , t, cos_t); legend('True','Finite Difference',Location='southeast')
Star Strider
Star Strider 2023 年 12 月 11 日
The single gradient call using both vectors is new, and does not appear to be documented as a change.
In earlier versions (perhaps as recently as five years ago), gradient used the first element of the second argument vector (or perhaps the difference between the first and second elements), giving anomalous results. That required dividing the gradient of the dependent variable vector by the gradient of the independent variable vector to get an acceptable result. I just now compared them (using a vector that was not close to being regularly-spaced), and it appears to consider the entire vector, since:
gradient(x,t)
and:
gradient(x) ./ gradient(t)
now give the same result.
I wonder when the change occurred? It would be nice it that were added to the documentation.
Paul
Paul 2023 年 12 月 11 日
The issue I was trying to illustrate wasn't with the accuracy of the estimated gradiient, rather with the pointwise mapping of each gradient estimate to a value of the independent variable.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2023 年 12 月 11 日

コメント済み:

2023 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by