Finding the Velocity at Different intervals

tryng to write a code to display the velocity and displacement given a time interval and acceleration at those intervals using integration. getting an error in my for loop what might be the reason
clear all
close all
% Acceleration Vector
a = [0 2 4 7 11 17 24 32 41 48 51];
% define time interval
k =1;
%define time vector
t = 0:k:10;
%initialize velocity
for ii = 1:11
v(ii) = trapz(a(1:ii),k);
end
% calculate the displacement d at each time interval
for jj = 1:11
d(jj) = trapz(v(1:jj),k);
end
% display a table of the velocity values
v = table(t,v,'VariableNames',{'Time(Sec)' 'Velocity(m/s)'})
% display a table of the displacement values
d = table(t',d','VariableNames',{'Time(Sec)' 'Displacement(m)'})

 Réponse acceptée

Star Strider
Star Strider le 24 Août 2019
You can solve the problem of varying numbers of elements in each iteration by using cell arrays for ‘v’ and ‘d’:
for ii = 1:11
v{ii} = trapz(a(1:ii),k);
end
% calculate the displacement d at each time interval
for jj = 1:11
d{jj} = trapz(v{jj},k);
end
The best solution would likely be to use the cumtrapz function instead, and avoid the loops entirely.

10 commentaires

Thank you. still learning Matlab. so using cumtrapz how do I simplify my code?
As always, my pleasure.
I would do this:
v = cumtrapz(t, a);
d = cumtrapz(t, v);
instead of the loops.
That should do what you want.
Note that for your table constructions, you need to use column vectors, so:
% display a table of the velocity values
v = table(t(:),v(:),'VariableNames',{'Time(Sec)' 'Velocity(m/s)'})
% display a table of the displacement values
d = table(t(:),d(:),'VariableNames',{'Time(Sec)' 'Displacement(m)'})
Your ‘d’ table construction is essentially correct, and using the complex-conjugate transpose operator here is appropriate, since your values are all real. I changed it to use the column-vector notation ‘(:)’ for consistency with the ‘v’ table construction.
okay now getting an error on trying to place those table headers.
Error using table (line 322)
'Time(Sec)' is not a valid table variable name. See the documentation for isvarname or
matlab.lang.makeValidName for more information.
Error in vf (line 17)
v = table(t(:),v(:),'VariableNames',{'Time(Sec)' 'Velocity(m/s)'})
I didn’t catch that.
Try this:
% display a table of the velocity values
v = table(t(:),v(:),'VariableNames',{'Time_Sec' 'Velocity_mps'})
% display a table of the displacement values
d = table(t(:),d(:),'VariableNames',{'Time_Sec' 'Displacement_m'})
The table variable names have to be valid MATLAB variable names. Those should work.
clear all
close all
clc
% define time vector
t = 0:10;
% define acceleration vector
a = [0 2 4 7 11 17 24 32 41 48 51];
% calculate the velocity
v = cumtrapz(a,t);
d = cumtrapz(v,t);
% display a table of the velocity values
v = table(t(:),v(:),'VariableNames',{'Time_Sec' 'Velocity_mps'})
% display a table of the displacement values
d = table(t(:),d(:),'VariableNames',{'Time_Sec' 'Displacement_m'})
This code is working but looking for the error final velocity is supposed to be 211.5m/s but its output is 298.5 and even the displacement from manual calculation it is coming out incorrectly. now unsure of how to correct that actual displacement is supposed to be
Time Displacement
____ ____________
0 0
1 0.5
2 3
3 9.75
4 23.75
5 49.25
6 92
7 159
8 258.25
9 398
10 584.75
but I am getting
Time_Sec Displacement_m
________ ______________
0 0
1 0.5
2 5
3 23.75
4 72.75
5 194.25
6 406
7 744
8 1250.3
9 1756
10 2026.8
Star Strider
Star Strider le 24 Août 2019
That seems to work correctly.
Are you having problems with it?
The only change I would make is to call the tables ‘Tv’ and ‘Td’ or something to avoid overwriting the origina ‘v’ and ‘d’ vectors.
The code works yes but it is producing the wrong results after the second iteration. look at the two tables I sent above. on time = 2secs the value I am getting for displacement is 5 and not 3. I already have the answer to the question but this code is not producing the correct response. so not sure what needs to be editted inorder for it to work correctly.
@Star Strider thank you for your assistance finally noticed that when using the function trapz or cumtrapz interchanging what starts within the bracket affects the results it is (t,v) and not (v,t) and (t,a) not (a,t) that was my main error
Star Strider
Star Strider le 25 Août 2019
As always, my pleasure.
I posted the correct code, and didn’t take a close look at yours. I just ran it to see if there were problems, and it ran without error.

Connectez-vous pour commenter.

Plus de réponses (1)

After abit of research have come up with these 3 codes that produce the desired results
%Matlab Code (Third Variation)
clear all
close all
% Acceleration Vector
a = [0 2 4 7 11 17 24 32 41 48 51];
%define time vector
t = 0:10;
%initialize velocity
v(1)=0;
for ii = 1:10
v(ii+1) = trapz(t(ii:ii+1),a(ii:ii+1))+v(ii);
end
% calculate the displacement d at each time interval
d(1)= 0;
for jj = 1:10
d(jj+1) = trapz(t(jj:jj+1),v(jj:jj+1))+d(jj);
end
% display a table of the velocity values
tv = table(t(:),v(:),'VariableNames',{'Time_sec' 'Velocity_mps'})
% display a table of the displacement values
td = table(t(:),d(:),'VariableNames',{'Time' 'Displacement'})
%plot graph of the change of the acceleration with time
plot(t,a)
title('Acceleration against time Graph')
xlabel('Time(Sec)')
ylabel('Acceleration(m/s^2)')
%plot graph of the change of the velocity with time
figure(2)
plot(t,v)
title('Velocity against time Graph')
xlabel('Time(Sec)')
ylabel('Velocity(m/s)')
%plot graph of the change of the displacement with time
figure(3)
plot(t,d)
title('Displacement against time Graph')
xlabel('Time(Sec)')
ylabel('Displacement(m)')
%Matlab Code (Second Variation)
clear all
close all
% Acceleration Vector
a = [0 2 4 7 11 17 24 32 41 48 51];
%define time vector
t = 0:10;
%initialize velocity
for ii = 2:11
v(ii) = trapz(t(1:ii),a(1:ii));
end
% calculate the displacement d at each time interval
for jj = 2:11
d(jj) = trapz(t(1:jj),v(1:jj));
end
% display a table of the velocity values
tv = table(t(:),v(:),'VariableNames',{'Time_sec' 'Velocity_mps'})
% display a table of the displacement values
td = table(t(:),d(:),'VariableNames',{'Time' 'Displacement'})
%plot graph of the change of the acceleration with time
plot(t,a)
title('Acceleration against time Graph')
xlabel('Time(Sec)')
ylabel('Acceleration(m/s^2)')
%plot graph of the change of the velocity with time
figure(2)
plot(t,v)
title('Velocity against time Graph')
xlabel('Time(Sec)')
ylabel('Velocity(m/s)')
%plot graph of the change of the displacement with time
figure(3)
plot(t,d)
title('Displacement against time Graph')
xlabel('Time(Sec)')
ylabel('Displacement(m)')
%Matlab Code (Third Variation)
clear all
close all
clc
% initialize time interval
k = 1;
% define time vector
t = 0:k:10;
% define acceleration vector
a = [0,2,4,7,11,17,24,32,41,48,51];
% calculate the velocity
v = cumtrapz(t,a);
d = cumtrapz(t,v);
% display a table of the velocity values
tv = table(t(:),v(:),'VariableNames',{'Time_Sec' 'Velocity_mps'})
% display a table of the displacement values
td = table(t(:),d(:),'VariableNames',{'Time_Sec' 'Displacement_m'})
%plot graph of the change of the acceleration with time
plot(t,a)
title('Acceleration against time Graph')
xlabel('Time(Sec)')
ylabel('Acceleration(m/s^2)')
%plot graph of the change of the velocity with time
figure(2)
plot(t,v)
title('Velocity against time Graph')
xlabel('Time(Sec)')
ylabel('Velocity(m/s)')
%plot graph of the change of the displacement with time
figure(3)
plot(t,d)
title('Displacement against time Graph')
xlabel('Time(Sec)')
ylabel('Displacement(m)')

3 commentaires

Star Strider
Star Strider le 25 Août 2019
I don’t understand the need for a loop with trapz, since (if I understand what you are doing), cumtrapz should do what you want, without any loops.
Yes cumtrapz works just fine thanx. and even a loop works. just tried both out and ultimately it is the same result
Star Strider
Star Strider le 26 Août 2019
The cumtrapz function is more efficient. I would use it and eliminate the loops.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by