Trouble in getting analytic and numeric solution for integrating sine wave

1 view (last 30 days)
Hello everyone,
I'm trying to get coordinates for a robot movement from a sine wave. Now I changed the length of one period and the last and most important plot looks aweful. Does anyone can explain what's wrong in my code? I'm using Matlab R2020b for academic use.
clear;
% Distance
t = 0.05; % Frequency
A = 50; %Amplitude
x = 0:t:10*pi;
y = A*sin(x); %It's all good if I leave sin(x), but when I want to change the duration of one period, like 2x
%the plot looks like in the picture.
T = (1:length(x))*t;
figure(1)
plot(T,y,'.')
xlabel('time')
ylabel('distance')
%%
vel = gradient(y)./gradient(x);
figure(2)
plot(T,vel,'.')
xlabel('time')
ylabel('velocity')
%%
acc = gradient(vel)./gradient(x);
figure(3)
plot(T,acc,'.')
xlabel('time')
ylabel('acceleration')
acc = acc';
%xlswrite('Acceleration.xlsx',acc);
acc = acc';
%%
%velocity Cosinus Figure
v = zeros(1,length(acc));
v(1,1) = A * cos(0);
l_v = 1:length(acc);
for i = 2:length(l_v)
v(i) = ((acc(i)+acc(i-1))/2) * t + v(i-1);
end
figure(4)
scatter(T,v,'.')
xlabel('time')
ylabel('velocity')
v = v';
%xlswrite('Velocity.xlsx',v);
v = v';
%%
%distance Sinus Figure
s = zeros(1,length(v));
s(1,1) = sin(0);
for i = 2:length(acc)
s(i) = ((v(i)+v(i-1))/2) * t + s(i-1);
end
figure(5)
scatter(T,s,'.')
xlabel('time')
ylabel('distance')
s = s';
%xlswrite('idealwaves.xlsx',s)
  3 Comments
Bruce Rogers
Bruce Rogers on 8 Jun 2021
Hey Walter,
I just saw the range of the velocity, its starts at 50 and ends at -150 (-‸ლ)
Thanks for the hint, I just changed
v(1,1) = A * cos(0);
to
v(1,1) = 2 * A * cos(0);
and it looks fine for me.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!