What is my problem with integration of velocity - I can't get displacement (s) correct?

8 views (last 30 days)
function [a, v, s] = braking_dynamics(t,T,v0)
% [a, v, s] = braking_dynamics(t)
%
% return acceleration, velocity and displacement
% for given input time t.
% Also works if t is an array.
% formulas found by hand (or using symbolic toolbox)
v = v0.*(1-(t./T).^4).^2
%% Derivative of v, a
h = 0.001;
t1 = t;
t2 = t + h;
v1 = v0.*(1-(t1./T).^4).^2;
v2 = v0.*(1-(t2./T).^4).^2;
a = (v2 - v1) ./ (t2 - t1)
%% Integral of v, s
c = 0
b = T
N = 50 % number of subintervals (strips)
h = (b-c) ./ N % length of each subinterval (width of strips)
s = 0; % running sum
for i = 0:N-1 % for each subinterval
xleft = c + h.*i;
xright = xleft + h;
yleft = v0.*(1-(xleft./T).^4).^2;
yright = v0.*(1-(xright./T).^4).^2;
trap_area = h * (yleft + yright) ./ 2;
s = s + trap_area;
end
s
end
% Do not change definition of T and v0
T = randi([200 600])/10;
v0 = randi([50 100])/10;
% Set t to a specific values or an appropriate array. Note the code checks the solutions with an array.
t = 30
% Call the function
[xddot xdot x] = braking_dynamics(t,T,v0);
  2 Comments
Walter Roberson
Walter Roberson on 9 Dec 2022
Edited: Walter Roberson on 9 Dec 2022
What should we expect to see in the third output if the calculation were correct? What are we looking for?
Is there a reason you are not using trapz() ?
rng(12345)
% Do not change definition of T and v0
T = randi([200 600])/10;
v0 = randi([50 100])/10;
% Set t to a specific values or an appropriate array. Note the code checks the solutions with an array.
t = 30
t = 30
% Call the function
[xddot xdot x] = braking_dynamics(t,T,v0)
v = 5.6390
a = -0.1231
c = 0
b = 57.2000
N = 50
h = 1.1440
s = 268.4586
xddot = -0.1231
xdot = 5.6390
x = 268.4586
function [a, v, s] = braking_dynamics(t,T,v0)
% [a, v, s] = braking_dynamics(t)
%
% return acceleration, velocity and displacement
% for given input time t.
% Also works if t is an array.
% formulas found by hand (or using symbolic toolbox)
v = v0.*(1-(t./T).^4).^2
%% Derivative of v, a
h = 0.001;
t1 = t;
t2 = t + h;
v1 = v0.*(1-(t1./T).^4).^2;
v2 = v0.*(1-(t2./T).^4).^2;
a = (v2 - v1) ./ (t2 - t1)
%% Integral of v, s
c = 0
b = T
N = 50 % number of subintervals (strips)
h = (b-c) ./ N % length of each subinterval (width of strips)
s = 0; % running sum
for i = 0:N-1 % for each subinterval
xleft = c + h.*i;
xright = xleft + h;
yleft = v0.*(1-(xleft./T).^4).^2;
yright = v0.*(1-(xright./T).^4).^2;
trap_area = h * (yleft + yright) ./ 2;
s = s + trap_area;
end
s
end
Torsten
Torsten on 9 Dec 2022
T, t and v0 only have one element each. I wonder how you want to calculate acceleration and displacement from a single velocity value.

Sign in to comment.

Answers (1)

Kartik
Kartik on 21 Mar 2023
Hi,
Based on your MATLAB code, it seems like you are trying to simulate the dynamics of a braking system and compute the acceleration, velocity, and displacement over a given time period.
To calculate displacement, you are using the trapezoidal rule to approximate the definite integral of velocity over time. However, your implementation of the trapezoidal rule seems to be incorrect.
The issue with your implementation is that you are using the midpoint of each subinterval to compute the average height of the trapezoid, instead of using the average of the heights at the left and right endpoints. This is causing an error in the computation of the integral.
To fix this issue, you can modify your code to use the average of the heights at the left and right endpoints to compute the average height of the trapezoid. Here's the modified code:
%% Integral of v, s
c = 0;
b = T;
N = 50; % number of subintervals (strips)
h = (b-c) ./ N; % length of each subinterval (width of strips)
s = 0; % running sum
for i = 1:N % for each subinterval
xleft = c + h.*(i-1);
xright = xleft + h;
yleft = v0.*(1-(xleft./T).^4).^2;
yright = v0.*(1-(xright./T).^4).^2;
trap_area = h * (yleft + yright) ./ 2;
s = s + trap_area;
end
In this modified code, I changed the loop variable i to start from 1 instead of 0, and modified the calculation of xleft to use (i-1) instead of i. This ensures that xleft and xright represent the left and right endpoints of each subinterval, respectively.
I also changed the calculation of trap_area to use the average of yleft and yright at the left and right endpoints, respectively. This ensures that the trapezoid is correctly approximating the area under the curve.
With these modifications, your code should be able to correctly calculate displacement s from velocity v.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!