필터 지우기
필터 지우기

Derivation of sin(x) gives false cos(x)

조회 수: 4 (최근 30일)
Bruce Rogers
Bruce Rogers 2021년 3월 29일
댓글: Robert Brown 2021년 3월 29일
Hello,
I'm trying to derivate the sin(x) two times to get the cos(x) and the -sin(x). The plot shows a cosine figure, but it doesn't start at 1 and the amplitude is false too.
My code is the following:
clear;
t = 0.05;
x = [0:0.05:2*pi];
y = sin(x);
figure(1)
scatter(x,y,'.')
xlabel('time')
ylabel('distance')
%%
vel = diff(y);
g = 1:length(vel);
for i = 1:length(g)
vel_t(i) = t * g(i);
end
figure(2)
scatter(vel_t,vel,'.')
xlabel('time')
ylabel('velocity')
%%
acc = diff(vel);
h = 1:length(acc);
for i = 1:length(h)
acc_t(i) = t * h(i);
end
figure(3)
scatter(acc_t,acc,'.')
xlabel('time')
ylabel('acceleration')
Is it wrong to use x = [0:0.05:2*pi] for defining my period?
Thanks for your help.
  댓글 수: 1
Stephen23
Stephen23 2021년 3월 29일
편집: Stephen23 2021년 3월 29일
"Is it wrong to use x = [0:0.05:2*pi] for defining my period?"
No, but those square brackets are superfluous, get rid of them.
"...it doesn't start at 1 and the amplitude is false too."
Hint: take a look at the definition of differentiation. Where in your code do you divide by the change in the independent variable?

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

채택된 답변

KSSV
KSSV 2021년 3월 29일
t = 0.05;
x = 0:0.05:2*pi;
y = sin(x);
figure(1)
plot(x,y,'r')
xlabel('time')
ylabel('distance')
%%
vel = gradient(y)./gradient(x);
figure(2)
plot(x,vel,'b')
xlabel('time')
ylabel('velocity')
%%
acc = gradient(vel)./gradient(x);
figure(3)
plot(x,acc,'g')
xlabel('time')
ylabel('acceleration')
  댓글 수: 2
Bruce Rogers
Bruce Rogers 2021년 3월 29일
Thank you for your help KSSV, your code gives the right plots. Can you explain why diff() gave me that wrong results?
Stephen23
Stephen23 2021년 3월 29일
"Can you explain why diff() gave me that wrong results?"
The problem is not DIFF, the problem is that you did not divide by the change in the independent variable (in your code x), which is required by the definition of differentiation. KSSV did that division using ./

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

추가 답변 (1개)

Robert Brown
Robert Brown 2021년 3월 29일
Technically, the gradient is extrapolating data!!!
When you have a series of position measurements..... you require 2 position measurements to get to a velocity estimate, and require another position measurement to get to an acceleration measurement.
Assume Time = T1, T2, T3, ...
Position 1 = only know position P1 and T1
Position 2 = know P1 and P2 and T1 and T2
and estimate V1 = (P2 - P1) / (T2-T1) = average velocity getting from P1 to P2,
and corresponding time TV1 = (T2-T1)/2
Position 3 = know P1 and P2 and estimate of V1 (above) and P3 and V2 = (P3 - P2) / (T3 - T2)
and corresponding time TV2 = (T3-T2)/2
and know acceleration 1 A1 = (V2-V1)/(TV2 - TV1) =
((P3 - P2) / (T3 - T2) - (P2 - P1) / (T2-T1)) / ((T3-T2)/2 - (T2-T1)/2)
etc....
  댓글 수: 1
Robert Brown
Robert Brown 2021년 3월 29일
Maybe I should have called it V1.5 meaning the average (estimated) velocity between 1 and 2, and T1.5 meaning the time 1/2 of the way between V1 and V2. Then by using V1.5 and V2.5.... you can determine V2, the (estimated) velocity at T2.
If you look back at your code, the diff function caused the velocity measurements to have 1 less data point since you don't know velocity until you know 2 positions, and the diff function caused the acceleration measurements to have 2 less data points, since you don't know acceleration until you know 3 positions and 2 estimated velocities... :-)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by