Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69483
| References | <mailman.8744.1396276875.18130.python-list@python.org> <533a3fd8$0$2909$c3e8da3$76491128@news.astraweb.com> <mailman.8767.1396328509.18130.python-list@python.org> <533a68c3$0$2909$c3e8da3$76491128@news.astraweb.com> |
|---|---|
| Date | 2014-04-01 18:35 +1100 |
| Subject | Re: Code style query: multiple assignments in if/elif tree |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8778.1396337762.18130.python-list@python.org> (permalink) |
On Tue, Apr 1, 2014 at 6:20 PM, Steven D'Aprano <steve@pearwood.info> wrote: > On Tue, 01 Apr 2014 16:01:40 +1100, Chris Angelico wrote: > > [...] >>> The scenario you describe has (effectively) infinite rate-of-change-of- >>> acceleration, often called "jerk". (A jerk is a rapid change in >>> acceleration.) Human comfort is (within reasonable limits) more >>> affected by jerk than acceleration. The passengers will feel three >>> quite distinctive jerks, one when the brakes are first applied (which >>> is probably reasonable), then one at 1s, then again at 2s. That's not >>> comfortable by any stretch of the imagination. >> >> It actually is a smooth increase in deceleration, but I'm operating the >> simulator on a 1s period, so it's actually an average across the first >> second, and an average across the next second... > > Hmmm. A 1-second resolution doesn't really sound too great to me. > > Suppose the deceleration increases linearly from 0 to 0.85 m/s over two > seconds. Averaging it in the way you suggested, we get a total distance > of: > > 2*u - 0.5125 > > (see my previous post for details) where u is measured in metres per > second. Multiply by seconds to get the units right. For a Japanese bullet > train where u = 320 km/hr that corresponds to > > py> 2*88.888889 - 0.5125 > 177.26527800000002 > > metres. > > Now let's do it properly! Given our assumption that the deceleration is > linear, the jerk will be constant: > > j = Δa/Δt > = (0.85 - 0)/2 > = 0.425 m/s^3 > > Let's start integrating! > > py> from sympy import integrate > py> from sympy.abc import t > py> j = '0.425' > py> a = integrate(j, t) > py> v = 88.888889 - integrate(a, t) > py> s = integrate(v, (t, 0, 2)) > py> s > 177.211111333333 > > compared to 177.26527800000002 calculated the rough way. That's not bad, > only about 5cm off! Effectively, your rough calculation was accurate to > one decimal place. 5cm when we're dealing with track distances measured in meters and usually hundreds of them? Sounds fine to me! > Of course, if the equation for acceleration was more complex, the > approximation may not be anywhere near as good. And that's the bit I can't know. I've added a comment to the code and will toss it back to my brother - hopefully he'll either know the answer or know where to look it up / who to ask. > [...] >>> This becomes a simple question for the four standard equations of >>> motion: >>> >>> (1) v = u + at >>> (2) s = 1/2(u + v)t >>> (3) s = ut + 1/2(at^2) >>> (4) v^2 = u^2 + 2as >>> >>> Only (1) and (3) are needed. >> >> Okay, what's u here? Heh. > > They're *standard* equations of motion. Weren't you paying attention > through the, oh, three years of high school where they teach this? :-P They're certainly the standard equations, but I learned that: d = v₀t + at²/2 which is the same as you had, except for the names: d for distance, v₀ (V zero, that's a subscript zero if encodings or fonts kill you here) for initial velocity (ie velocity at time zero - vₜ (that's a subscript t, U+209C, but it isn't working in this font) is velocity at time t, which is v₀+at), and a and t are the same as you have. > s = displacement (distance) > t = time > u = initial velocity > v = final velocity > a = acceleration I have to say, the one-letter variables are a lot easier to work with. Guess I learned them the hard way, heh. Although remembering that v is velocity is easier than remembering which of u and v is initial and which is final. >> Fair enough. :) That's why I asked. Is it naughty enough to break into >> two statements, or is it better to combine it into a single multiple >> assignment? > > Since they are unrelated assignments, of two different variables, they > should be written as two separate assignments, not one using sequence > unpacking. Change made. Yeah, that looks a bit cleaner. Although I would prefer a simple formula to what I have there, and I'm still not perfectly sure it's accurate. Advice appreciated. :) ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 01:33 +1100
Re: Code style query: multiple assignments in if/elif tree Marko Rauhamaa <marko@pacujo.net> - 2014-03-31 18:40 +0300
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 03:03 +1100
Re: Code style query: multiple assignments in if/elif tree Rustom Mody <rustompmody@gmail.com> - 2014-03-31 09:20 -0700
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 03:29 +1100
Re: Code style query: multiple assignments in if/elif tree "Rhodri James" <rhodri@wildebst.org.uk> - 2014-03-31 21:31 +0100
Re: Code style query: multiple assignments in if/elif tree Ned Batchelder <ned@nedbatchelder.com> - 2014-03-31 17:42 -0400
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 09:50 +1100
Re: Code style query: multiple assignments in if/elif tree Ben Finney <ben+python@benfinney.id.au> - 2014-04-01 09:57 +1100
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 10:12 +1100
Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-01 00:57 +0000
Re: Code style query: multiple assignments in if/elif tree Ethan Furman <ethan@stoneleaf.us> - 2014-03-31 17:30 -0700
Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 04:26 +0000
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 16:01 +1100
Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 07:20 +0000
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:35 +1100
Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 08:07 +0000
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 19:12 +1100
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:18 -0600
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:24 -0600
Re: Code style query: multiple assignments in if/elif tree Rustom Mody <rustompmody@gmail.com> - 2014-03-31 22:45 -0700
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 02:05 -0400
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:28 -0600
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:13 -0600
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 02:24 -0400
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 00:39 -0600
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 17:55 +1100
Re: Code style query: multiple assignments in if/elif tree Steven D'Aprano <steve@pearwood.info> - 2014-04-01 07:29 +0000
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:49 +1100
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:29 -0600
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 19:56 +1100
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:21 -0400
Re: Code style query: multiple assignments in if/elif tree Chris Angelico <rosuav@gmail.com> - 2014-04-01 18:26 +1100
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:34 -0400
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:39 -0400
Re: Code style query: multiple assignments in if/elif tree David Hutto <dwightdhutto@gmail.com> - 2014-04-01 03:46 -0400
Re: Code style query: multiple assignments in if/elif tree Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-01 02:55 -0600
csiph-web