Second time derivative of function?

조회 수: 10 (최근 30일)
Christian Aranas
Christian Aranas 2018년 7월 25일
댓글: Beau Olafson 2019년 5월 30일
I have defined a model of displacement 'x' and represented its first derivative with 'v' using the code below. However, when I want to differentiate 'v' using the same function it does not work. Is there a different approach to differentiating derivatives in MATLAB? here is the code:
h = 60 ;
b =130 ;
r = 28 ;
w = 2*pi; % angular velocity
t =0:(1/36):1 ;
x = b*(r*sin(w*t))./(h-r*cos(w*t));
v = diff(x)./diff(t) ; % derivative of x w.r.t t
a = diff(v)./diff(t) ; % derivative of v w.r.t t

채택된 답변

Aquatris
Aquatris 2018년 7월 25일
It does not work, because the length of diff(x) and diff(t) are not the same. diff function takes the difference between each element. However returns 1 element less than the original vector since there is no way to find the difference for the last element.
Since you have fixed step size you can use
v = diff(x)./(1/36);
td = t(1:end-1); % time vector for plotting first derivative
a = diff(v)./(1/36) ;tdd = t(1:end-2)
tdd = t(1:end-2); % time vector for plotting second derivative
Alternatively you might try symbolic toolbox to derive the derivative of the expression symbolicly and then plug in numbers.
  댓글 수: 1
Christian Aranas
Christian Aranas 2018년 7월 25일
Thank you so much for the context

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

추가 답변 (1개)

KSSV
KSSV 2018년 7월 25일
diff reduces the dimension of vector by one. You may use gradient
h = 60 ;
b =130 ;
r = 28 ;
w = 2*pi; % angular velocity
t =0:(1/36):1 ;
x = b*(r*sin(w*t))./(h-r*cos(w*t));
v = gradient(x)./gradient(t) ; % derivative of x w.r.t t
a = gradient(v)./gradient(t) ; % derivative of v w.r.t t
Or, reduce the time step by one and use _diff_
h = 60 ;
b =130 ;
r = 28 ;
w = 2*pi; % angular velocity
t =0:(1/36):1 ;
x = b*(r*sin(w*t))./(h-r*cos(w*t));
v = diff(x)./diff(t) ; % derivative of x w.r.t t
a = diff(v)./diff(t(1:end-1)) ; % derivative of v w.r.t t
  댓글 수: 1
Beau Olafson
Beau Olafson 2019년 5월 30일
That gradient code 'literally' saved my life. I was so dang lost until then!

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

카테고리

Help CenterFile Exchange에서 Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by