Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
Bruce Rogers
Bruce Rogers el 29 de Mzo. de 2021
Comentada: Robert Brown el 29 de Mzo. de 2021
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 comentario
Stephen23
Stephen23 el 29 de Mzo. de 2021
Editada: Stephen23 el 29 de Mzo. de 2021
"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?

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 29 de Mzo. de 2021
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 comentarios
Bruce Rogers
Bruce Rogers el 29 de Mzo. de 2021
Thank you for your help KSSV, your code gives the right plots. Can you explain why diff() gave me that wrong results?
Stephen23
Stephen23 el 29 de Mzo. de 2021
"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 ./

Iniciar sesión para comentar.

Más respuestas (1)

Robert Brown
Robert Brown el 29 de Mzo. de 2021
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 comentario
Robert Brown
Robert Brown el 29 de Mzo. de 2021
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... :-)

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by