Hi there,
I have the acceleration vs. time data that is attached. When I plot it, I get the following:
I want to integrate it to get the Velocity vs. Time graph but I do not know how to do this. I have tried trapz() but it only gives some weird linear graph.
If someone could let me know how to do this that would be greatly appreciated.
Cheers.

 Akzeptierte Antwort

Alan Stevens
Alan Stevens am 20 Nov. 2020

0 Stimmen

Try
v = cumtrapz(X2(:,1),X2(:,2));
plot(X2(:,1),v)

7 Kommentare

Timo Mayer
Timo Mayer am 20 Nov. 2020
Hi Alan,
thanks for the reply, I get the following:
It seems a bit off since it is only going into the negative, and only going up to 4. did you scale the units?
Cheers.
Alan Stevens
Alan Stevens am 20 Nov. 2020
Bearbeitet: Alan Stevens am 20 Nov. 2020
Have a look at the help file for cumtrapz. In the form cumtrapz(x,y) it calculates the cumulative integral of y with respect to x using trapezoidal integration.
I suspect you just used
plot(X2(:,2))
whereas if you'd used
plot(X2(:,1),X2(:,2))
you would have had the x-axis going from 0 to 4.
Also, look closely at your original plot. You can see that the dense part of the plot lies slightly below 0, so the acceleration tends to be mostly negative.
Timo Mayer
Timo Mayer am 20 Nov. 2020
Bearbeitet: Timo Mayer am 20 Nov. 2020
Thanks, I got it working now with cumtrapz as can be seen below but it seems the exponential line is adding everything together with absolute values but the other line is actually what I want, as it adds what is above the line and substracts what is below. Any idea how I can get only this bottom line and not the exponential one?
My code is below:
close all
z = cumtrapz(X2);
h = cumtrapz(z);
figure
plot(X2)
title('Acceleration (m/s^2) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Acceleration (m/s^2)')
figure
plot(z,'r')
title('Velocity (m/s) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Velocity (m/s)')
Cheers.
Your "exponential" curve is simply the cumulative integral of the time!!
You need to use, say
t = X2(:,1);
accn = X2(:,2);
vel = cumtrapz(t,accn);
to be clear.
The time intervals are not constant, so technically
cumtrapz(accn)
on its own, is not velocity
.
Ok thanks, I understand now that I need to include time when using the trapezoidal rule. I now have the following results. Do those look correct to you?
How can I remove the second blue line from the original data?
Here is my code:
t = X2(:,1);
accn = X2(:,2);
vel = cumtrapz(t,accn);
dis = cumtrapz(t,vel);
figure
plot(X2)
title('Acceleration (m/s^2) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Acceleration (m/s^2)')
figure
plot(vel,'r')
title('Velocity (m/s) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Velocity (m/s)')
figure
plot(dis,'k')
title('Displacement (m) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Displacement (m)')
Cheers.
Instead of
figure
plot(X2)
title('Acceleration (m/s^2) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Acceleration (m/s^2)')
use
figure
plot(t,accn)
title('Acceleration (m/s^2) vs. Time (ms)')
xlabel('Time (ms)')
ylabel('Acceleration (m/s^2)')
Similarly, I would use
plot(t,vel)
and
plot(t,dis)
for clarity.
Timo Mayer
Timo Mayer am 20 Nov. 2020
Great, thank you very much, now it looks much better!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by