Central numerical differentiation - Error

조회 수: 2 (최근 30일)
Gennaro Arguzzi
Gennaro Arguzzi 2018년 12월 9일
댓글: Gennaro Arguzzi 2018년 12월 9일
Hello everyone,
I'm trying to implement the numerical differentiation by using central method:
% x[k+1] - x[k-1]
% x_dot [k] = ---------------- = (x[k+1] - x[k-1])*sr/2
% 2*Ts
I wrote the code:
sr = 0.1;
a = 0:0.001:10;
t = a;
b = ( a(2:end) - a(1:end) ) * sr/2;
plot(t,a)
hold on
plot(t,b)
but it does not work; in particular, the statement "b = ( a(2:end) - a(1:end) ) * sr/2;" is wrong.
Is it possible to reach my goal by fixing the above statement? I prefer to avoid to insert additional code lines.
Thank you so much in advance.

답변 (1개)

Image Analyst
Image Analyst 2018년 12월 9일
Instead of that, use the diff() function
b = diff(a) * sr/2;
  댓글 수: 3
Image Analyst
Image Analyst 2018년 12월 9일
Try a simple for loop
for k = 2 : length(a)-1
theDiff = a(k+1) - a(k-1);
etc.
Gennaro Arguzzi
Gennaro Arguzzi 2018년 12월 9일
Thank you @ImageAnalyst.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by