<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Doswa &#187; Physics</title>
	<atom:link href="http://doswa.com/blog/category/physics/feed/" rel="self" type="application/rss+xml" />
	<link>http://doswa.com/blog</link>
	<description>Programming, physics, mathematics</description>
	<lastBuildDate>Sat, 28 Aug 2010 16:20:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Relation between motor&#8217;s torque/speed and its voltage/current.</title>
		<link>http://doswa.com/blog/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent/</link>
		<comments>http://doswa.com/blog/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 17:18:47 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://doswa.com/blog/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent/</guid>
		<description><![CDATA[The torque of a motor is proportional to the current flowing through it (). The angular velocity of a motor is proportional to the voltage across it (). Details and derivation here: motor.pdf.]]></description>
			<content:encoded><![CDATA[<ol>
<li>The <strong>torque</strong> of a motor is proportional to the <strong>current</strong> flowing through it (<img src="http://doswa.com/blog/wp-content/cache/tex_d27da2b4f2d4739eb17c39193b862401.png" align="absmiddle" class="tex" alt="\tau \propto i" />).</li>
<li>The <strong>angular velocity</strong> of a motor is proportional to the <strong>voltage</strong> across it (<img src="http://doswa.com/blog/wp-content/cache/tex_04ec16e4ae1ed7f25751dbb2258811b8.png" align="absmiddle" class="tex" alt="\omega \propto v" />).</li>
</ol>
<p>Details and derivation here: <a href="http://doswa.com/projects/motor_torque_speed/motor.pdf">motor.pdf</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2010/01/23/relation-between-motors-torquespeed-and-its-voltagecurrent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Line Segment to Circle Collision/Intersection Detection</title>
		<link>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/</link>
		<comments>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 18:15:08 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=158</guid>
		<description><![CDATA[Style and Notation All variables are represented in italics except in the diagrams and Python code. Vectors are shown in bold except in the diagrams and Python code. The notation &#124;a&#124; is used to represent the length of a vector &#8230; <a href="http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Style and Notation</h2>
<ul>
<li>All variables are represented in italics except in the diagrams and Python code.</li>
<li>Vectors are shown in bold except in the diagrams and Python code.</li>
<li>The notation |<em>a</em>| is used to represent the length of a vector named <strong><em>a</em></strong>.</li>
</ul>
<h2>Introduction to the problem</h2>
<p>Start by defining a few points (vectors from the world origin):<br />
<a href="http://doswa.com/blog/wp-content/uploads/2009/07/2.png"><img class="alignnone size-full wp-image-162" title="2" src="http://doswa.com/blog/wp-content/uploads/2009/07/2.png" alt="2" width="275" height="192" /></a></p>
<p>Now, from these points, you can easily calculate two useful values, the segment vector, <strong><em>seg_v</em></strong> (from <strong><em>seg_a</em></strong> to <strong><em>seg_b</em></strong>) and the position of <strong><em>circ_pos</em></strong> relative to <strong><em>seg_a</em></strong>, <strong><em>pt_v</em></strong>.</p>
<p><a href="http://doswa.com/blog/wp-content/uploads/2009/07/3.png"></a><a href="http://doswa.com/blog/wp-content/uploads/2009/07/31.png"><img class="alignnone size-full wp-image-166" title="31" src="http://doswa.com/blog/wp-content/uploads/2009/07/31.png" alt="31" width="275" height="192" /></a></p>
<p><strong><em>seg_v</em></strong> = <strong><em>seg_b</em></strong> &#8211; <strong><em>seg_a</em></strong></p>
<p><strong><em>pt_v</em></strong> = <strong><em>circ_pos</em></strong> &#8211; <strong><em>seg_a</em></strong></p>
<h2>Closest point to the circle&#8217;s center on the segment<em><br />
</em></h2>
<p>The next step is to find the closest point to the circle&#8217;s center on the segment (labeled &#8220;<strong><em>closest</em></strong>&#8221; on the diagram). To do this, we must project <strong><em>pt_v</em></strong> onto <strong><em>seg_v</em></strong>:</p>
<p><a href="http://doswa.com/blog/wp-content/uploads/2009/07/4.png"><img class="alignnone size-full wp-image-167" title="4" src="http://doswa.com/blog/wp-content/uploads/2009/07/4.png" alt="4" width="275" height="192" /></a></p>
<p>To project on vector onto another, take the dot product of the vector and the unit vector of the projection target. That is:</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_d317daa71d17b12f8188376628d58ba8.png" align="absmiddle" class="tex" alt="|proj\_v| = \textbf{pt\_v} \cdot \frac{\textbf{seg\_v}}{|seg\_v|}" /></center></p>
<p>Note that this dot product returns a scalar value (the length of <strong><em>proj_v</em></strong>), not a vector.</p>
<p>&#8211;</p>
<p>We now need to take a small break from the main goal and look into a special case: the ends of the segment. If |<em>proj_v</em>| is less than <em>0</em> or greater than |<em>seg_v</em>|, the closest point to <strong><em>circ_pos</em></strong> on the segment will be one of the segment&#8217;s endpoints. That is:<br />
<code>if |<em>proj_v</em>| &lt; 0: <strong><em>closest</em></strong> = <strong><em>seg_a</em></strong><br />
if |<em>proj_v</em>| &gt; |<em>seg_v</em>|: <em><strong>c</strong><strong>losest</strong></em> = <strong><em>seg_b</em></strong></code></p>
<p>&#8211;</p>
<p>Next, calculate the actual <strong><em>proj_v</em></strong> vector, rather than just its length (|<em>proj_v</em>|). To do this, simply multiply by the <strong><em>seg_v</em></strong> unit vector:</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_2bc551d3368e2e85233c77ffd0ab9665.png" align="absmiddle" class="tex" alt="\textbf{proj\_v} = |proj\_v| \frac{\textbf{seg\_v}}{|seg\_v|}" /></center></p>
<p>The only thing left to do is to convert this into world coordinates (rather than coordinates relative to <strong><em>seg_a</em></strong>) to get the closest point:</p>
<p><strong><em>closest</em></strong> = <strong><em>seg_a</em></strong> + <strong><em>proj_v</em></strong></p>
<h2>Checking for a intersection</h2>
<p>Now that we&#8217;ve calculated the closest point on the segment (the variable &#8220;<strong><em>closest</em></strong>&#8220;), we can check if the circle and segment intersect. To do this, first find the vector from <strong><em>closest</em></strong> to <strong><em>circ_pos</em></strong>:</p>
<p><strong><em>dist_v</em></strong> = <strong><em>circ_pos</em></strong> &#8211; <strong><em>closest</em></strong></p>
<p>If the length of this vector is less than the circle&#8217;s radius, the circle and segment are intersecting:<br />
<code>if |<em>dist_v</em>| &lt; <em>circ_rad: </em>They are intersecting<br />
else: They are not intersecting</code></p>
<h2>Collision response</h2>
<p>A useful bit of data in collision response is the amount of overlap between two shapes and the direction a shape must be moved in order to resolve the collision. This can be represented as a vector that points in the same direction as <strong><em>dist_v</em></strong> whose length is equal to the difference between <em>circ_rad</em> and |<em>dist_v</em>|.</p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_8339fdad8e95d59472dda12890ebb6ab.png" align="absmiddle" class="tex" alt="\textbf{offset} = (circ\_rad - |dist\_v|) \frac{\textbf{dist\_v}}{|dist\_v|}" /></center></p>
<h2>Python implementation</h2>
<p>Here is a Python implementation of everything in this post. For the sake of clarity, I avoided optimizations. <tt>vec</tt> is assumed to be a fully implemented 2d vector class.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> closest_point_on_seg<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos<span style="color: black;">&#41;</span>:
	seg_v = seg_b - seg_a
	pt_v = circ_pos - seg_a
	<span style="color: #ff7700;font-weight:bold;">if</span> seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>, <span style="color: #483d8b;">&quot;Invalid segment length&quot;</span>
	seg_v_unit = seg_v / seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	proj = pt_v.<span style="color: black;">dot</span><span style="color: black;">&#40;</span>seg_v_unit<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> proj <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> seg_a.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> proj <span style="color: #66cc66;">&gt;</span>= seg_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> seg_b.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	proj_v = seg_v_unit <span style="color: #66cc66;">*</span> proj
	closest = proj_v + seg_a
	<span style="color: #ff7700;font-weight:bold;">return</span> closest
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> segment_circle<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos, circ_rad<span style="color: black;">&#41;</span>:
	closest = closest_point_on_seg<span style="color: black;">&#40;</span>seg_a, seg_b, circ_pos<span style="color: black;">&#41;</span>
	dist_v = circ_pos - closest
	<span style="color: #ff7700;font-weight:bold;">if</span> dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> circ_rad:
		<span style="color: #ff7700;font-weight:bold;">return</span> vec<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>, <span style="color: #483d8b;">&quot;Circle's center is exactly on segment&quot;</span>
	offset = dist_v / dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>circ_rad - dist_v.<span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> offset</pre></div></div>

<h2>Similar algorithms</h2>
<p>Collisions involving <a href="http://mathworld.wolfram.com/Stadium.html">stadiums</a> (a type of rounded rectangle) can be calculated in a similar manner. A stadium is essentially a line segment with a radius.</p>
<p>A stadium-point collision is the same as a segment-circle collision with a circle whose radius is equal to the stadium&#8217;s radius.</p>
<p>A stadium-circle collision is the same as a segment-circle collision with a circle whose radius is equal to the sum of the original circle&#8217;s radius and the stadium&#8217;s radius.</p>
<h2>Download</h2>
<p><a href="http://doswa.com/projects/segment_circle/segment_circle.zip">segment_circle.zip</a> &#8211; An implementation in Python and <a href="http://cython.org">Cython</a>.</p>
<h2>Conclusion</h2>
<p>If you find this post useful or have any questions, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/07/13/circle-segment-intersectioncollision/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Improved RK4 Implementation</title>
		<link>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/</link>
		<comments>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 07:02:49 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=102</guid>
		<description><![CDATA[This is a Python implementation of the RK4 numerical integrator that works with differential functions of all orders. That is, any function in the form F(x, y, y&#8217;, y&#8221;, &#8230;, ). If you&#8217;re new to numerical integration or even RK4 &#8230; <a href="http://doswa.com/blog/2009/04/21/improved-rk4-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://doswa.com/blog/wp-content/uploads/2009/04/sage0.png"><img class="aligncenter size-full wp-image-119" title="Damped harmonic motion" src="http://doswa.com/blog/wp-content/uploads/2009/04/sage0.png" alt="Damped harmonic motion" width="600" height="370" /></a></p>
<p>This is a Python implementation of the RK4 numerical integrator that works with differential functions of all orders. That is, any function in the form F(x, y, y&#8217;, y&#8221;, &#8230;, <img src="http://doswa.com/blog/wp-content/cache/tex_0d171d6a16ab48052105c6921f8e78d4.png" align="absmiddle" class="tex" alt="y^{(n)}" />).</p>
<p>If you&#8217;re new to numerical integration or even RK4 integration, please <a href="http://doswa.com/blog/?p=16">read my other post</a> first. It&#8217;s easier to understand because it&#8217;s a less generalized function.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> rk4<span style="color: black;">&#40;</span>t0, h, s0, f<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;RK4 implementation.
	t = current value of the independent variable
	h = amount to increase the independent variable (step size)
	s0 = initial state as a list. ex.: [initial_position, initial_velocity]
	f = function(state, t) to integrate&quot;&quot;&quot;</span>
	r = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>s0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	s1 = s0 + <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0, s0<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s2 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>s1<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s2 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+<span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>h, s2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s3 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>s2<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s3 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+<span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>h, s3<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	s4 = <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + s3<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>h <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span>
	s4 += <span style="color: black;">&#91;</span>f<span style="color: black;">&#40;</span>t0+h, s4<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> t+h, <span style="color: black;">&#91;</span>s0<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> + <span style="color: black;">&#40;</span>s1<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>s2<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+s3<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + s4<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>h/<span style="color: #ff4500;">6.0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> r<span style="color: black;">&#93;</span></pre></div></div>

<p>An example usage of this function is:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> damped_spring_accel<span style="color: black;">&#40;</span>t, state<span style="color: black;">&#41;</span>:
	x = state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
	v = state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
	stiffness = <span style="color: #ff4500;">1</span>
	damping = <span style="color: #ff4500;">0.005</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> -stiffness<span style="color: #66cc66;">*</span>x - damping<span style="color: #66cc66;">*</span>v
&nbsp;
t = <span style="color: #ff4500;">0</span>
h = <span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">40.0</span>
s = <span style="color: black;">&#91;</span><span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> t<span style="color: #66cc66;">&lt;</span><span style="color: #ff4500;">100</span>:
	t, s = rk4<span style="color: black;">&#40;</span>t, h, s, damped_spring_accel<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Final state: t=%.2f x=%.2f v=%.2f&quot;</span><span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>t,s<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,s<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>On a side note, I recently found out about Sage (<a href="http://sagemath.org">sagemath.org</a> and <a href="http://sagenb.org">sagenb.org</a>). Its plotting capabilities and convenient mathematical notation are especially useful. Plus, it uses Python!</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/04/21/improved-rk4-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chain</title>
		<link>http://doswa.com/blog/2009/01/24/chain/</link>
		<comments>http://doswa.com/blog/2009/01/24/chain/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 08:22:25 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=76</guid>
		<description><![CDATA[I made a little Processing sketch based on the same physics as JavaScript physics. (View the full post to try it out.) Source code: Chain Link]]></description>
			<content:encoded><![CDATA[<p>I made a little <a href="http://processing.org/">Processing</a> sketch based on the same physics as <i><a href="http://doswa.com/blog/?p=68">JavaScript physics</a></i>.</p>
<img src="http://doswa.com/blog/wp-content/uploads/2009/01/screenshot.png" alt="Screenshot of &quot;Chain&quot;" title="Screenshot of &quot;Chain&quot;" width="347" height="347" class="size-full wp-image-86" />
<p>(View the full post to try it out.)</p>
<p><span id="more-76"></span></p>
<p><iframe width="400" height="400" src="/projects/p5/Chain/iframe.html" style="overflow:hidden;border:none"></iframe></p>
<p>Source code: <a href="http://doswa.com/projects/p5/Chain/Chain.pde">Chain</a> <a href="http://doswa.com/projects/p5/Chain/Link.pde">Link</a></p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/01/24/chain/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript physics</title>
		<link>http://doswa.com/blog/2009/01/16/javascript-physics/</link>
		<comments>http://doswa.com/blog/2009/01/16/javascript-physics/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 02:26:17 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=68</guid>
		<description><![CDATA[I made a very basic physics demo in JavaScript to practice using the MooTools library. It uses Verlet integration as outlined in Thomas Jakobsen&#8217;s paper. To try out the demo, go to http://doswa.com/projects/physics_js/]]></description>
			<content:encoded><![CDATA[<p>I made a very basic physics demo in JavaScript to practice using the <a href="http://mootools.net">MooTools library</a>. It uses Verlet integration as outlined in <a href="http://teknikus.dk/tj/gdc2001.htm">Thomas Jakobsen&#8217;s paper</a>.</p>
<p>To try out the demo, go to <a href="http://doswa.com/projects/physics_js/">http://doswa.com/projects/physics_js/</a></p>
<div id="attachment_72" class="wp-caption aligncenter" style="width: 166px"><img src="http://doswa.com/blog/wp-content/uploads/2009/01/screenshot1.png" alt="Chain of particles attached by joints" title="Preview of JavaScript physics" width="156" height="223" class="size-full wp-image-72" /><p class="wp-caption-text">Chain of particles attached by joints</p></div>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/01/16/javascript-physics/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Fourth order Runge-Kutta numerical integration</title>
		<link>http://doswa.com/blog/2009/01/02/fourth-order-runge-kutta-numerical-integration/</link>
		<comments>http://doswa.com/blog/2009/01/02/fourth-order-runge-kutta-numerical-integration/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 18:33:26 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=16</guid>
		<description><![CDATA[Here&#8217;s a Python implementation of RK4, hardcoded for double-integrating the second derivative (acceleration up to position). For a more generalized solution, see my other implementation. I tried to keep this as simple as I could, so people can easily see &#8230; <a href="http://doswa.com/blog/2009/01/02/fourth-order-runge-kutta-numerical-integration/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a Python implementation of <a href="http://en.wikipedia.org/wiki/RK4">RK4</a>, hardcoded for double-integrating the second derivative (acceleration up to position). For a more generalized solution, see <a href="http://doswa.com/blog/?p=102">my other implementation</a>. I tried to keep this as simple as I could, so people can easily see the relation between the &#8216;math form&#8217; and &#8216;code form&#8217; of the algorithm.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> rk4<span style="color: black;">&#40;</span>x, v, a, dt<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;Returns final (position, velocity) tuple after
	time dt has passed.
&nbsp;
	x: initial position (number-like object)
	v: initial velocity (number-like object)
	a: acceleration function a(x,v,dt) (must be callable)
	dt: timestep (number)&quot;&quot;&quot;</span>
	x1 = x
	v1 = v
	a1 = a<span style="color: black;">&#40;</span>x1, v1, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
	x2 = x + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>v1<span style="color: #66cc66;">*</span>dt
	v2 = v + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>a1<span style="color: #66cc66;">*</span>dt
	a2 = a<span style="color: black;">&#40;</span>x2, v2, dt/<span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span>
&nbsp;
	x3 = x + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>v2<span style="color: #66cc66;">*</span>dt
	v3 = v + <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">*</span>a2<span style="color: #66cc66;">*</span>dt
	a3 = a<span style="color: black;">&#40;</span>x3, v3, dt/<span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span>
&nbsp;
	x4 = x + v3<span style="color: #66cc66;">*</span>dt
	v4 = v + a3<span style="color: #66cc66;">*</span>dt
	a4 = a<span style="color: black;">&#40;</span>x4, v4, dt<span style="color: black;">&#41;</span>
&nbsp;
	xf = x + <span style="color: black;">&#40;</span>dt/<span style="color: #ff4500;">6.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>v1 + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>v2 + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>v3 + v4<span style="color: black;">&#41;</span>
	vf = v + <span style="color: black;">&#40;</span>dt/<span style="color: #ff4500;">6.0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>a1 + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>a2 + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>a3 + a4<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> xf, vf</pre></div></div>

<p>Here is an example usage of the function and a comparison to Euler integration:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> accel<span style="color: black;">&#40;</span>x, v, dt<span style="color: black;">&#41;</span>:
	<span style="color: #483d8b;">&quot;&quot;&quot;Determines acceleration from current position,
	velocity, and timestep. This particular acceleration
	function models a spring.&quot;&quot;&quot;</span>
	stiffness = <span style="color: #ff4500;">1</span>
	damping = -<span style="color: #ff4500;">0.005</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> -stiffness<span style="color: #66cc66;">*</span>x - damping<span style="color: #66cc66;">*</span>v
&nbsp;
t = <span style="color: #ff4500;">0</span>
dt = <span style="color: #ff4500;">1.0</span>/<span style="color: #ff4500;">40</span> <span style="color: #808080; font-style: italic;"># Timestep of 1/40 second</span>
state = <span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">5</span> <span style="color: #808080; font-style: italic;"># Position, velocity</span>
euler = <span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">5</span> <span style="color: #808080; font-style: italic;"># For comparison with Euler integration</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Initial    -position: %6.2f, velocity: %6.2f&quot;</span><span style="color: #66cc66;">%</span>state
&nbsp;
<span style="color: #808080; font-style: italic;"># Run for 100 seconds</span>
<span style="color: #ff7700;font-weight:bold;">while</span> t <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">100</span>:
	t += dt
	state = rk4<span style="color: black;">&#40;</span>state<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, state<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, accel, dt<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Integrate using Euler's method</span>
	euler = <span style="color: black;">&#40;</span>
		euler<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + euler<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>dt,
		euler<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + accel<span style="color: black;">&#40;</span>euler<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,euler<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,dt<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>dt
	<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Final RK4  -position: %6.2f, velocity: %6.2f&quot;</span><span style="color: #66cc66;">%</span>state
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Final Euler-position: %6.2f, velocity: %6.2f&quot;</span><span style="color: #66cc66;">%</span>euler</pre></div></div>

<p>The output of this really shows how much more accurate RK4 integration can be:</p>
<pre>Initial    -position:  50.00, velocity:   5.00
Final RK4  -position:  52.18, velocity:  38.05
Final Euler-position: 178.38, velocity: 137.62</pre>
<p>As the timestep is decreased (meaning more computation), Euler approaches RK4 (shown at timestep of 1/400 seconds):</p>
<pre>Initial    -position:  50.00, velocity:   5.00
Final RK4  -position:  52.28, velocity:  37.92
Final Euler-position:  59.20, velocity:  43.02</pre>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/01/02/fourth-order-runge-kutta-numerical-integration/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
