0

I'm simulating car cruise system and my results are not making any sense. I have been told that my force formula is wrong but I don’t understand why it is wrong and how I can fix this. Does anybody know how I can fix this problem?

1 Answers1

1

If I understand your code correctly, your total force $F$ is given by an variable throttle force $F_T$ and a drag term whose sign is the same as the sign of the velocity (i.e. the factor (m_speed > 0.0 ? 1.0 : -1.0), which I believe equals $\text{sgn}(v)$. There is a problem, I think, in the velocity dependence of that drag term. You are currently implementing an equation of the form $$ F=F_T+\text{sgn}(v)\gamma\sqrt{|v|},$$ where $\gamma=0.1$ is your drag coefficient. The dependence on $\sqrt{|v|}$ is incorrect; instead, the drag should be proportional to either $v$ itself (for linear drag at low velocities) or on $v^2$ (for quadratic drag at higher velocities).

I should also point out that the update step for the distance, $$ d(t+\delta t)=d(t)+v(t)\,t+\tfrac12a\,\delta t^2, $$ is more elaborate than it needs to be. Specifically, you can more or less cut out the quadratic term, $\tfrac12a\,\delta t^2$, as it is quadratic in time_step and will therefore be less and less important as your time step goes down to zero. The reason it is not needed is that the velocity itself gets updated at each time step. In essence, what you are doing is performing a numerical solution to a system of coupled ordinary differential equations, \begin{align} \frac{d}{dt}x(t)=v(t),\\ \frac{d}{dt}v(t)=a(t), \end{align} and a simple linear time step is quite effective for that. If you want to be more accurate for the same length of time step, then you should go to higher order methods, of which the one you have is a naive implementation. The formal way to do it, however, is to use a Runge-Kutta method, which can provide very high accuracy for much larger time steps, at the slight expense of depending on the last few time steps (instead of just the previous one).

Emilio Pisanty
  • 132,859
  • 33
  • 351
  • 666