-2

I am pretty new to Matlab and i have some Current Vs times stored under a structure in a matlab file. What i am trying to plot is current vs time along with the first derivative of it. (di/dt). I used the diff function but the plot seems to be really wierd. I know it simple but can anyone explain it.

THanks in advance.

vittal rao
  • 17
  • 1
  • 8
  • 2
    Add: Data + plot + what you have tried + what you expect, and it might turn out to be a good question =) – Stewie Griffin Jan 14 '15 at 16:59
  • 1
    Take into accoant that to approximately compute the derivative *dy/dx* you should use `diff(y)./diff(x)` (not just `diff(y)`) – Luis Mendo Jan 14 '15 at 17:19

1 Answers1

0

Assume you have a structure S,

S.t is the time vector and S.I is the current vector in each time in S.t. (both should be in the same length N).

Now, if you want to approximate the derivative:

dt = diff(S.t); % dt is the time intervals length, dt is N-1 length. 
dI = diff(S.I);  
derivative = dI./dt; %derivative is memberwise division of dI by dt 
plot(t(1:end-1),derivative); % when you plot both vector should be in the same length:
                             % t(1:end-1) is the same as t except the last coordinate

I think this should work

Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61
  • Thanks i have fixed it now. I knew my mistake. I had not used the plot as you said and it ended up throwing mismatch in length or a weird plot. Seems fine now though. :) – vittal rao Jan 14 '15 at 21:17