Why is the size of my array changing when calculating velocity from the position formula?

3 Ansichten (letzte 30 Tage)
So I am trying to make 3 subplots that plot the calculated values of the postion, velocity, and acceleration of a dropped object. However when I move to plot the velocity I get an error saying the vectors are not the same length. I can't seem to figure out what is going wrong any advice would be apprecieated!
clc;
clear;
clear all;
yn=0; %initial posiion (m)
vn=0; %initial velocity (m/s)
a=9.81; %acceleration from gravity (m/s^2)
t=[0:0.5:5]; %time (s)
y=yn-vn*t+1/2*a*t.^2; %formula to find position in free fall (m)
v=diff(y)./diff(t);
subplot(3,1,1);
plot(t,y)
subplot (3,1,2)
plot(t,v)
  2 Kommentare
Connor Lance
Connor Lance am 25 Mär. 2022
Bearbeitet: Connor Lance am 25 Mär. 2022
I would like to add that I know I could simply just type in the derivative of v as a formula and skip the diff() step but I also want to know what is wrong and keep the code simple.
I'm pretty sure it has something to do with the initial velocity not being included in v's vector but when I force it in with v=[0,v] it makes the graph come out with a strange bend in it that doesn't necessarily mesh with what the graph should represent.
Torsten
Torsten am 25 Mär. 2022
Bearbeitet: Torsten am 25 Mär. 2022
It's obvious that v must have one element less than t.
If y has n elements, then diff(y) has (n-1) elements.
You might want to define
v = [(y(2)-y(1))/(t(2)-t(1)),(y(3:end)-y(1:end-2))./(t(3:end)-t(1:end-2)),(y(end)-y(end-1))/(t(end)-t(end-1))];

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 25 Mär. 2022
Bearbeitet: Voss am 25 Mär. 2022
Each element of v is calculated from two elements from y and two elements from t. Which is to say, each element of v is calculated from an initial time and displacement and a final time and displacement.
So, as you noted in your comment, you get one fewer v value than you had t and y values. That's a consequence of each v requiring 2 t's and 2 y's.
The question then is: Which t should each v be associated with (for plotting or whatever)? Should each v be associated with its initial time or its final time or maybe the time halfway between its initial and final time?
Since here t is a discrete variable (not continuous like we tend to think of it), it is pretty much arbitrary how you decide to associate each v with a t: each velocity is defined over a time interval and each velocity is associated with that whole interval, so how you depict it is up to you.
Here are a few plots, for visual aid (and you can pick the one that makes the most sense and use that code):
clc;
clear;
clear all;
yn=0; %initial posiion (m)
vn=0; %initial velocity (m/s)
a=9.81; %acceleration from gravity (m/s^2)
t=[0:0.5:5]; %time (s)
y=yn-vn*t+1/2*a*t.^2; %formula to find position in free fall (m)
v=diff(y)./diff(t);
subplot(4,1,1)
plot(t(1:end-1),v,'.-')
xlim(t([1 end]))
title('v vs t_{initial}')
subplot(4,1,2)
plot(t(2:end),v,'.-')
xlim(t([1 end]))
title('v vs t_{final}')
subplot(4,1,3)
plot((t(1:end-1)+t(2:end))/2,v,'.-')
xlim(t([1 end]))
title('v vs t_{middle}')
subplot(4,1,4)
plot(t([1 repelem(2:end-1,1,2) end]),repelem(v,1,2))
xlim(t([1 end]))
title('v vs t_{interval}')

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by