Help on derivative of a function in discrete time

4 views (last 30 days)
Hello all Please can you help me out calculating derivatives of discrete time functions in matlab r(k+1) from r(k)
Given a discrete signal r(k)=0.5*sin*(pi*k*ts) for k=1:1=100
I found in one book that they used an extrapolation method as follows:
dr(k)=(r(k)-r_1)/ts;
dr_1=(r_1-r_2)/ts;
r1(k)=2*r(k)-r_1;
dr1(k)=2*dr(k)-dr_1;
to calculate first and second derivatives. i didn't understand this method
I tried rather to use a simple formula:
r_1=0;
r_1(k)=r(k)-r_1/ts;
r_1=r(k);
I don't know if this is correct ?
Thank you so much for your precious help
Best regards

Answers (1)

Walter Roberson
Walter Roberson on 14 Jun 2015
You change the same variable in all three lines. You have
r_1=0;
r_1(k)=r(k)-r_1/ts;
r_1=r(k);
You set all of r_1 in the first line. In the second line you overwrite only part of r_1 . But in the third line you overwrite all of it, using a value that was not changed in the previous lines. The effect is going to be as if you had only one line
r_1 = r(k);
I cannot really advise on the correctness of the method you showed from the book as I do not know what r1 is intended to represent that is different from dr. Also we would need to know how r_1 and r_2 are initialized. My suspicion is that the book might also show them changing, possibly like
r_2 = r_1;
r_1 = r(k);
so that the variables represent the two previous values.
  3 Comments
lady bird
lady bird on 15 Jun 2015

Thank you Walter Roberson for your answer

in fact, i did a mistake my code was rather as follows:

    r_1=0;
    r1(k)=r(k)-r_1/ts;
    r_1=r(k); 

where r1(k) is the first derivative, and i dont know if this is correct

In the other hand, yes i just checked in that book , there is an update of the variables as u just stated

   r_2 = r_1;
   r_1 = r(k);

r_1 and r_2 are initialized to zero at first

Walter Roberson
Walter Roberson on 16 Jun 2015
They are using dr() as the first derivative, it appears, so I do not understand why they would also have r1.
The r_1 and r_2 make sense to me now.

Sign in to comment.

Categories

Find more on Variables 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!