algorithm - Converting Kinematics Equations to Matlab -
perhaps isn't programming question math question. here goes.
imagine making roller coaster powered motor on vehicle. vehicles have fixed force value can achieve using motor. in section of roller coaster want traverse half loop 1 one of favorite video games: rollercoaster tycoon!
as go around half loop, don't know speed or how long take go around it. however, can figure out based on engine acceleration, mass, , acceleration due gravity maximum possible acceleration @ point along half loop be. let's not muddle discussion numbers, instead assume we've got acceleration vs position curve available. looks this:
i have derived formula velocity function of acceleration vs position curve , initial velocity. kinematic equation
v^2 = 2*a*p
i can derive velocity function of position. v = sqrt(2 * [integral of a=f(p) wrt position])
which in matlab can doing:
v = sqrt(2.*abs(trapz(pos, acc)));
i getting velocity of every point along track following code (acc , pos arrays of acceleration vs position plotted above):
vel = 1; newacc = 0; while ix <= length(acc) pa = acc(ix-1) + (acc(ix)-acc(ix-1))./2; newap = (pos(ix)-pos(ix-1)).*pa; % more time efficient trapz newacc = newacc + abs(2.*newap); vel(ix) = sqrt(newacc); ix = ix + 1; end
now reach dilemma. have acceleration, velocity, , position, , need time. think math correct. since have a/v/p should simple choosing of kinematic equations involving time , rearranging time (i want time @ every position along track can plot a/v/p functions of time).
this should mean can pick of these:
1. p_f = p_i + v_i*t + 1/2*a*t^2 2. v_f = v_i + a*t 3. p = (v_i + v_f)*t/2
equation 1 quadratic. other 2 simpler. equation 2 looks promising. let's try that.
t = (v_f - v_i)/a
converting matlab, think should this:
time = 0; while ix <= length(acc) pa = acc(ix-1) + (acc(ix)-acc(ix-1))./2; newap = (pos(ix)-pos(ix-1)).*pa; % more time efficient trapz newacc = newacc + abs(2.*newap); vel(ix) = sqrt(newacc); dt = (vel(ix)-vel(ix-1))./acc(ix); time(ix) = time(ix-1) + dt; ix = ix + 1; end
but far testing, produces incorrect result! i'm stumped on both math , algorithm. suppose place answer question algorithm. can't see how formula incorrect. converting in matlab incorrectly?
the following code solution. i've been sitting on awhile, forgot answer question.
while p < pf vel(i) = real(sqrt(vel(i-1).^2 + 2 .* ((acc(i)+acc(i-1))./2) .* (pos(i)-pos(i-1)) )); time(i) = time(i-1) + (pos(i)-pos(i-1)) ./ ( (vel(i)+vel(i-1))./2 ); = + 1; end
the time function comes rearrangement of
d = (vi+vf)*t/2
modified array of position values.