working out acceleration from 2 matrices- HELP

I have 2 matrices: velocity and time
velocity = [0,56.40,97.23,136.25,226.16,403.86,440.44,1265.23]
time = [0,10,15,20,30,59,62,125]
it asks to work out acceleration
This is what i have done:
clc;
clear all;
close all;
time = [0,10,15,20,30,59,62,125];
velocity = [0,56.40,97.23,136.25,226.16,403.86,440.44,1265.23];
for i=1:length(time)
for ii=1:length(velocity)
for x=1:1:7
%%accelertaion = change in velocity / change in time%%
acceleration(x) = (velocity(1,(x+1))-velocity(1,x))/(time(1,(x+1))-time(1,x))
end
end
end
So this gives me:
acceleration = Columns 1 through 6
5.6400 8.1660 7.8040 8.9910 6.1276 12.1933
Column 7
13.0919
So my question is: how do i put a 0 in column 1 of the acceleration matrix and shift everything else? i need 1*8 matrix.
i want to plot a graph of acceleration against time so the size of matrix needs to be the same.

답변 (2개)

Roger Stafford
Roger Stafford 2017년 11월 24일
편집: Roger Stafford 2017년 11월 25일

1 개 추천

Assuming your 'time' and 'velocity' vectors are corresponding row vectors (have only one row,) you can use the following code to produce second order approximations to the accelerations corresponding to points in 'time' that will produce an equal number of acceleration values. You need to have a minimum of three values in 'time' and 'velocity' vectors for this to work.
n = length(time); %Assume velocity vector is same length
te = [time(3),time,time(n-2)];
ve = [velocity(3),velocity,velocity(n-2)];
t1 = te(1:n); t2 = te(2:n+1); t3 = te(3:n+2);
v1 = ve(1:n); v2 = ve(2:n+1); v3 = ve(3:n+2);
t21 = t2-t1; t32 = t3-t2; t31 = t3-t1;
v21 = v2-v1; v32 = v3-v2;
ac = (v21./t21.*t32+v32./t32.*t21)./t31; % Approx. acceleration values
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017년 11월 24일

0 개 추천

It is not correct to put a 0 when you don't know the value of acceleration. Instead, you should define a new vector of time points that correctly reflects the times at which you actually computed your acceleration. This would be the mid-points of the time-intervals. Also, there's a simpler way to compute acceleration from your data without constructing a loop:
acc = diff(velocity)./diff(time);
t_acc = 0.5*(time(1:end-1) + time(2:end));
plot(t_acc,acc);

댓글 수: 5

Thank you.
one more question:
it also asks me for the distance and i used my previous code format and i wrote:
clc; clear all; close all; time = [0,10,15,20,30,59,62,125]; velocity = [0,56.40,97.23,136.25,226.16,403.86,440.44,1265.23];
acceleration = zeros(size(time)-1);
distance = zeros(size(time)-1);
for i=1:length(time)
for ii=1:length(velocity)
for x=1:1:7
%%accelertaion = change in velocity / change in time%%
velocityChange = velocity(1,(x+1))-velocity(1,x);
timeChange = time(1,(x+1))-time(1,x);
acceleration(x) = (velocityChange/timeChange)
distance(x) = trapz(velocityChange,timeChange)
%%distance = velocity*time%%
end
end
end
So i used trapz function and i want the distance as a matrix but i get
distance =
0 0 0 0 0 0 0
Distance =
6.7192e+04
why do i get a 0 matrix and only the sum of distances?
EEEmatlab
EEEmatlab 2017년 11월 24일
ALSO I JUST LOOKED AND I DONT NEED TO DRAW A GRAPH... i dont think i need to use the midpoint thing right?
Thanks a lot
EEEmatlab
EEEmatlab 2017년 11월 24일
also if i use this method this would be an approximation right because acceleration is not constant so you cannot actually use the area under graph equation? is there any other way to get the actual distance?
It does not matter if acceleration is not constant. Displacement is always equal to the time-integral of velocity, so you can use area under the velocity-time graph to calculate displacement (which is equal to distance in your case). To compute the integral, you need to provide the trapz function with the complete data, not just one time point. So you need to use:
distance = trapz(time,velocity);
EEEmatlab
EEEmatlab 2017년 11월 25일
The thing is the question asks me for the distance covered for each time interval. If i use trapz(velocityChange,timeChange) it gives me a zero matrix and only the sum of all the distances.. how do i get the distance for each time interval?
Also i was saying that when u draw the graph the time interval connects with straight lines which is kind of wrong because it could be a curve since the acceleration is not constant and so using area under that graph will only be an approximation of the distance. This is a differentiation and integration topic so i am asking is there not a way to compute the exact area?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2017년 11월 24일

댓글:

2017년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by