<?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; Mathematics</title>
	<atom:link href="http://doswa.com/blog/category/mathematics/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>RK4 in Haskell</title>
		<link>http://doswa.com/blog/2009/07/31/rk4-in-haskell/</link>
		<comments>http://doswa.com/blog/2009/07/31/rk4-in-haskell/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 05:43:42 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=200</guid>
		<description><![CDATA[Ran across this today. It&#8217;s a well written implementation of RK4. 4th order Runge-Kutta (RK4) integration in Haskell]]></description>
			<content:encoded><![CDATA[<p>Ran across this today. It&#8217;s a well written implementation of RK4.<br />
<a href="http://srihari.ramanathan.googlepages.com/runge-kuttawithhaskell">4th order Runge-Kutta (RK4) integration in Haskell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/07/31/rk4-in-haskell/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>Extending derivatives and integrals into fractions</title>
		<link>http://doswa.com/blog/2009/01/03/extending-derivatives-and-integrals-into-fractions/</link>
		<comments>http://doswa.com/blog/2009/01/03/extending-derivatives-and-integrals-into-fractions/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 04:57:40 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=50</guid>
		<description><![CDATA[In the previous post, I wrote about a way to generalize derivatives and integrals into one function. What happens if a number other than an integer is passed to that function? Here is the generalization from the last post: Set &#8230; <a href="http://doswa.com/blog/2009/01/03/extending-derivatives-and-integrals-into-fractions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://doswa.com/blog/?p=32">previous post</a>, I wrote about a way to generalize derivatives and integrals into one function. What happens if a number other than an integer is passed to that function?</p>
<p>Here is the generalization from the last post:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_ba047e66ce55de98be491d24b4de41e0.png" align="absmiddle" class="tex" alt="\frac{\partial^n}{\partial x^n} x^k = \frac{k!}{(k-n)!} x^{k-n}" /></center></p>
<p>Set <em>k=2</em> and <img src="http://doswa.com/blog/wp-content/cache/tex_7095fa2f4548cc26c605212154ff20c6.png" align="absmiddle" class="tex" alt="n=\frac{1}{2}" />:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_1aed5fea3c5b0b3bb7a1c8a133bc0d9d.png" align="absmiddle" class="tex" alt="\frac{\sqrt{\partial}}{\sqrt{\partial x}} x^2 = \frac{2!}{(2-\frac{1}{2})!} x^{2-\frac{1}{2}} = \frac{2!}{1.5!} x^{1.5}" /></center></p>
<p>Now there&#8217;s a problem. Since <em>a!</em> is only defined for integers 0 or greater, a different way of calculating factorials is needed. Luckily, there exists a <a href="http://en.wikipedia.org/wiki/Gamma_function">Gamma function</a> defined for all real and complex numbers such that:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_157b42411cbccaf8e5b1e58775565726.png" align="absmiddle" class="tex" alt="\Gamma(n+1) = n! " /></center></p>
<p>Substitute the Gamma function into the other equation and simplify:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_6cdcd30706760c5d92936f0488ab09a6.png" align="absmiddle" class="tex" alt="\frac{\sqrt{\partial}}{\sqrt{\partial x}} x^2 = \frac{2!}{1.5!} x^{1.5} = \frac{2}{\Gamma(1.5+1)} x^{1.5} = \frac{2}{\Gamma(2.5)} x^{1.5} = \frac{2}{(\frac{3 \sqrt{\pi}}{4})} x^{1.5}" /></center><br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_dca6193b68cd9cb8e784dcdc1a555690.png" align="absmiddle" class="tex" alt=" = \frac{2}{1} (\frac{4}{3 \sqrt{\pi}}) x^{1.5} = \frac{8}{3 \sqrt{\pi}} x^{1.5} = \frac{8 \sqrt{\pi}}{3 \pi} x^{1.5} \approx 1.505 x^{1.5}" /></center></p>
<p><strong>So, the half derivative of <img src="http://doswa.com/blog/wp-content/cache/tex_32f5240d0dbf2ccbe75ef7f8ef2015e0.png" align="absmiddle" class="tex" alt="x^2" /> is approximately equal to <img src="http://doswa.com/blog/wp-content/cache/tex_2d9905519b391cecdd2699651b14dc7d.png" align="absmiddle" class="tex" alt="1.505 x^{1.5}" />.</strong></p>
<p>This can be used for &#8216;fractional integration&#8217; as well, if a negative number is used for <em>n</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/01/03/extending-derivatives-and-integrals-into-fractions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generalizing derivatives and integrals</title>
		<link>http://doswa.com/blog/2009/01/03/generalizing-derivatives-and-integrals/</link>
		<comments>http://doswa.com/blog/2009/01/03/generalizing-derivatives-and-integrals/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 07:43:58 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://doswa.com/?p=32</guid>
		<description><![CDATA[Start with a simple expression, , and take a few derivatives: A pattern is emerging: , where c is the coefficient. Now the hard part is finding the pattern in the coefficient. This needs to be taken out of the &#8230; <a href="http://doswa.com/blog/2009/01/03/generalizing-derivatives-and-integrals/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Start with a simple expression, <img src="http://doswa.com/blog/wp-content/cache/tex_1f31f8c0da2e32b6acaa5b9a0e5154e9.png" align="absmiddle" class="tex" alt="x^k" />, and take a few derivatives:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_300243ce76ebff7fdc3f63403166c8ae.png" align="absmiddle" class="tex" alt="\frac{\partial}{\partial x} x^k = k x^{k-1}" /></center><br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_1d4f229e3b61dcd97bbd52d5b4f5ca4c.png" align="absmiddle" class="tex" alt="\frac{\partial^2}{\partial x^2} x^k = (k-1) k x^{k-2}" /></center><br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_f12bf99661e23a4e9b322090c8a58f54.png" align="absmiddle" class="tex" alt="\frac{\partial^3}{\partial x^3} x^k = (k-2) (k-1) k x^{k-3}" /></center><br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_3b81f9edf2fd9d271910906d917aa3bf.png" align="absmiddle" class="tex" alt="\frac{\partial^4}{\partial x^4} x^k = (k-3) (k-2) (k-1) k x^{k-4}" /></center></p>
<p>A pattern is emerging:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_ffbb793c002598516bc0a69337f853ab.png" align="absmiddle" class="tex" alt="\frac{\partial^n}{\partial x^n} x^k = (k-n+1)...(k-1)k x^{k-n} = c x^{k-n}" /></center>, where c is the coefficient.</p>
<hr />
<p>Now the hard part is finding the pattern in the coefficient. This needs to be taken out of the &#8216;&#8230;&#8217; form. Focus on that:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_1a297e16f826d689ad7ea4d5af60f14a.png" align="absmiddle" class="tex" alt="c = (k-n+1)...(k-1)k" /></center></p>
<p>This is a series of numbers, each one larger than the next. This looks like a factorial, so divide <em>k!</em> by that:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_facf09a45e869963cd54e28162a1f215.png" align="absmiddle" class="tex" alt="\frac{k!}{c} = \frac{k!}{(k-n+1)...(k-1)k} = \frac{1(2)(3)...k}{(k-n+1)...(k-1)k}" /></center></p>
<p>Note how the top goes from <em>1</em> to <em>k</em> and the bottom goes from <em>k-n+1</em> to <em>k</em>. That means that <em>k-n+1</em> to <em>k</em> is a subset of <em><em>1</em> to k</em>, so just divide that part out:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_fdaf96e778ed07c8e9ef1ceab3c7b900.png" align="absmiddle" class="tex" alt="\frac{k!}{c} = \frac{1(2)(3)...k}{(k-n+1)...(k-1)k} = 1(2)(3)...(k-n) = (k-n)! " /></center></p>
<p>Now solve for c:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_40d591f6bb06216561c5c224b261825e.png" align="absmiddle" class="tex" alt="\frac{1}{c} = \frac{(k-n)!}{k!}" /></center>, so <center><img src="http://doswa.com/blog/wp-content/cache/tex_113b5125d07c1490f1a9b6857f923151.png" align="absmiddle" class="tex" alt="c = \frac{k!}{(k-n)!}" /></center></p>
<hr />
<p>Puts this back into the original equation to get:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_31bdb72e48b7f4ee91ca142fe6700d06.png" align="absmiddle" class="tex" alt="\frac{\partial^n f(x)}{\partial x^n} = c x^{k-n} = \frac{k!}{(k-n)!} x^{k-n}" /></center></p>
<p><strong>So, the n<sup>th</sup> derivative of <img src="http://doswa.com/blog/wp-content/cache/tex_1f31f8c0da2e32b6acaa5b9a0e5154e9.png" align="absmiddle" class="tex" alt="x^k" /> is equal to:</strong><br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_c10f536ca743142eaab7f300a8141fe2.png" align="absmiddle" class="tex" alt="\frac{k!}{(k-n)!} x^{k-n}" /></center></p>
<hr />
<p>Verify this with a couple of derivatives:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_c887c436826f2b6a5d2f7e2faceac181.png" align="absmiddle" class="tex" alt="\frac{\partial}{\partial x} x^2 = \frac{k!}{(k-n)!} x^{k-n} = \frac{2!}{(2-1)!} x^{2-1} = \frac{2}{1} x^1 = 2x" /></center></p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_96f216bd31506fd82485bbd1aadcdc2a.png" align="absmiddle" class="tex" alt="\frac{\partial^2}{\partial x^2} 4 x^4 = 4 (\frac{k!}{(k-n)!}) x^{k-n} = 4 (\frac{4!}{(4-2)!}) x^{4-2} = 4 (\frac{24}{2}) x^2 = 4(12) x^2 = 48 x^2" /></center></p>
<p>And a couple of integrals:<br />
<center><img src="http://doswa.com/blog/wp-content/cache/tex_fc1c6cd27c446b24859742e45c6107b5.png" align="absmiddle" class="tex" alt="\frac{\partial^{-1}}{\partial x^{-1}} x^2 = \frac{k!}{(k-n)!} x^{k-n} = \frac{2!}{(2+1)!} x^{2+1} = \frac{2}{6} x^3 = \frac{1}{3} x^3" /></center></p>
<p><center><img src="http://doswa.com/blog/wp-content/cache/tex_940d0993ecbcafc918f854c3031494c2.png" align="absmiddle" class="tex" alt="\frac{\partial^{-7}}{\partial x^{-7}} 3 x^4 = 3 (\frac{k!}{(k-n)!}) x^{k-n} = 3 (\frac{4!}{(4+7)!}) x^{4+7} = 3 (\frac{24}{39916800}) x^{11} = \frac{1}{554400} x^{11}" /></center></p>
]]></content:encoded>
			<wfw:commentRss>http://doswa.com/blog/2009/01/03/generalizing-derivatives-and-integrals/feed/</wfw:commentRss>
		<slash:comments>3</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>
