Why is there a diiference in state-space velocity and derivative of state space displacement?

2 views (last 30 days)
I stumbled on this matlab code online from mathworks. However, I am surprised that the velocity (yvector(:,1)) is different from the velocity obtained from diff(yvector(:,2))./diff(t). Why des this occur? https://www.mathworks.com/matlabcentral/answers/1613980-how-can-i-obtain-the-derivative-of-a-vector-displacement-velocity-but-what-about-acceleration-i-am
clc; %command window
clear;%clear workspace
close all; % close figures
global m k
m = 80; %Defining mass of SDOF system
k = 10000;%Defining stiffness of SDOF system
dt = .02; %Defining Time Step:
t = 0:dt:4;% Defining time vector.
y0 = [0.085; 0.1]; %initial vel and disp [vel disp] %velocity.
[tsol, yvectorl] = ode45(@testode1,t,y0);
%%plot
figure()
plot(tsol,yvectorl(:,2)), title("Displacement") %Disp.(2)
figure()
plot(tsol,yvectorl(:,1)), title("Velocity") %Vel. (1)
% THIS NEEDS TO GO AT THE END OF THE FILE
%%function statespace
function dy = testode1(t,y)
global m k
dy=[-k*y(2)/m;y(1)];
end

Answers (1)

Mathieu NOE
Mathieu NOE on 11 Jan 2023
hello
where do you see a difference ?
diff or gradient based dispkacement derivatives do pretty well overlay with computed velocity
clc; %command window
clear;%clear workspace
close all; % close figures
global m k
m = 80; %Defining mass of SDOF system
k = 10000;%Defining stiffness of SDOF system
dt = .02; %Defining Time Step:
t = (0:dt:4)';% Defining time vector.
y0 = [0.085; 0.1]; %initial vel and disp [vel disp] %velocity.
[tsol, yvectorl] = ode45(@testode1,t,y0);
%%plot
figure()
plot(tsol,yvectorl(:,2)), title("Displacement") %Disp.(2)
% derivative of disp
% ddisp = [0;diff(yvectorl(:,2))./diff(t)];
ddisp = gradient(yvectorl(:,2),dt);
figure()
plot(tsol,yvectorl(:,1),tsol,ddisp,'*r'), title("Velocity") %Vel. (1)
% THIS NEEDS TO GO AT THE END OF THE FILE
%%function statespace
function dy = testode1(t,y)
global m k
dy=[-k*y(2)/m;y(1)];
end
  4 Comments

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!