<?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>cryptosystem.org &#187; MATLAB</title>
	<atom:link href="http://www.cryptosystem.org/archives/categories/matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cryptosystem.org</link>
	<description></description>
	<lastBuildDate>Sat, 05 Sep 2009 21:05:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using MATLAB for numerical solutions to the discrete Poisson equation</title>
		<link>http://www.cryptosystem.org/archives/2009/09/using-matlab-for-numerical-solutions-to-the-discrete-poisson-equation/</link>
		<comments>http://www.cryptosystem.org/archives/2009/09/using-matlab-for-numerical-solutions-to-the-discrete-poisson-equation/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 21:27:22 +0000</pubDate>
		<dc:creator>ryanfb</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.cryptosystem.org/?p=45</guid>
		<description><![CDATA[Let&#8217;s say you&#8217;ve got a 2D image (or matrix) of the Laplacian of some function, and you want to solve for the function. This is actually relatively easy to do with MATLAB, but searching for a step-by-step example I came up with nothing, so I thought I&#8217;d show how to do it here.

Poisson&#8217;s equation relates [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you&#8217;ve got a 2D image (or matrix) of the Laplacian of some function, and you want to solve for the function. This is actually relatively easy to do with MATLAB, but searching for a step-by-step example I came up with nothing, so I thought I&#8217;d show how to do it here.</p>

<p><a href="http://en.wikipedia.org/wiki/Poisson_equation">Poisson&#8217;s equation</a> relates the <a href="http://en.wikipedia.org/wiki/Laplace_operator">Laplacian</a> (<img src='http://s.wordpress.com/latex.php?latex=%5CDelta%20f%20%3D%20%5Cnabla%5E2%20f%20%3D%20%5Cnabla%20%5Ccdot%20%5Cnabla%20f&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\Delta f = \nabla^2 f = \nabla \cdot \nabla f' title='\Delta f = \nabla^2 f = \nabla \cdot \nabla f' class='latex' />) of some function with its result:</p>

<p><img src='http://s.wordpress.com/latex.php?latex=%5Cleft%28%5Cnabla%5E2%20u%20%5Cright%29_%7Bij%7D%3Dg_%7Bij%7D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\left(\nabla^2 u \right)_{ij}=g_{ij}' title='\left(\nabla^2 u \right)_{ij}=g_{ij}' class='latex' /></p>

<p>So in the <a href="http://en.wikipedia.org/wiki/Discrete_Poisson_equation">discrete Poisson equation</a> we have <img src='http://s.wordpress.com/latex.php?latex=g_%7Bij%7D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='g_{ij}' title='g_{ij}' class='latex' /> defined as the non-boundary subset of a 2-D rectangular matrix  of dimensions <img src='http://s.wordpress.com/latex.php?latex=m%20%5Ctimes%20n&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='m \times n' title='m \times n' class='latex' />. Here <img src='http://s.wordpress.com/latex.php?latex=2%20%5Cle%20i%20%5Cle%20m-1%2C%202%20%5Cle%20j%20%5Cle%20n-1&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='2 \le i \le m-1, 2 \le j \le n-1' title='2 \le i \le m-1, 2 \le j \le n-1' class='latex' /> to allow for boundary conditions.</p>

<p>Transforming <img src='http://s.wordpress.com/latex.php?latex=u_%7Bij%7D%2C%20g_%7Bij%7D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='u_{ij}, g_{ij}' title='u_{ij}, g_{ij}' class='latex' /> into natural ordered vectors <img src='http://s.wordpress.com/latex.php?latex=%5Cleft%5BU%5Cright%5D%2C%20%5Cleft%5Bb%5Cright%5D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\left[U\right], \left[b\right]' title='\left[U\right], \left[b\right]' class='latex' /> allows us to rewrite as the <img src='http://s.wordpress.com/latex.php?latex=%5Cleft%28m-2%5Cright%29%5Cleft%28n-2%5Cright%29%20%5Ctimes%20%5Cleft%28m-2%5Cright%29%5Cleft%28n-2%5Cright%29&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\left(m-2\right)\left(n-2\right) \times \left(m-2\right)\left(n-2\right)' title='\left(m-2\right)\left(n-2\right) \times \left(m-2\right)\left(n-2\right)' class='latex' /> linear system:</p>

<p><img src='http://s.wordpress.com/latex.php?latex=%5Cleft%5BA%5Cright%5D%5Cleft%5BU%5Cright%5D%20%3D%20%5Cleft%5Bb%5Cright%5D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\left[A\right]\left[U\right] = \left[b\right]' title='\left[A\right]\left[U\right] = \left[b\right]' class='latex' /></p>

<p>So, on to the juicy bit, how to express and solve this in MATLAB given you have a <img src='http://s.wordpress.com/latex.php?latex=m%20%5Ctimes%20n&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='m \times n' title='m \times n' class='latex' /> 2-D matrix defining <img src='http://s.wordpress.com/latex.php?latex=g_%7Bij%7D&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='g_{ij}' title='g_{ij}' class='latex' /> (for consistency, we&#8217;ll use variable names equivalent to what&#8217;s in these equations).</p>

<p>First, we need to reshape <img src='http://s.wordpress.com/latex.php?latex=g&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='g' title='g' class='latex' /> into a <img src='http://s.wordpress.com/latex.php?latex=%5Cleft%28m-2%5Cright%29%5Cleft%28n-2%5Cright%29%5Ctimes%201&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='\left(m-2\right)\left(n-2\right)\times 1' title='\left(m-2\right)\left(n-2\right)\times 1' class='latex' /> vector <img src='http://s.wordpress.com/latex.php?latex=b&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='b' title='b' class='latex' /> that skips the boundaries:</p>

<pre><code>b = -reshape(g(2:(m-1),2:(n-1)),(m-2)*(n-2),1);
</code></pre>

<p>If we have uniform Dirichlet boundary conditions <img src='http://s.wordpress.com/latex.php?latex=u%3D0&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='u=0' title='u=0' class='latex' />, <img src='http://s.wordpress.com/latex.php?latex=b&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='b' title='b' class='latex' /> should now be correct. If you want other boundary conditions for <img src='http://s.wordpress.com/latex.php?latex=u&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='u' title='u' class='latex' />, you&#8217;ll need to add them to the appropriate positions in <img src='http://s.wordpress.com/latex.php?latex=b&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='b' title='b' class='latex' />, which I won&#8217;t cover here.</p>

<p>Thankfully, MATLAB has a nice built-in gallery of matrices which includes the sparse tridiagonal matrix we need for <img src='http://s.wordpress.com/latex.php?latex=A&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='A' title='A' class='latex' />:</p>

<pre><code>A = gallery('poisson',m-2);
</code></pre>

<p>We can now solve using the backslash operator:</p>

<pre><code>U = A\b;
</code></pre>

<p>This gives us the result in vector form which we can reshape back to what we want:</p>

<pre><code>u = reshape(U,m-2,n-2);
</code></pre>

<p>You can pad back your <img src='http://s.wordpress.com/latex.php?latex=u%20%3D%200&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='u = 0' title='u = 0' class='latex' /> boundary conditions with:</p>

<pre><code>u = padarray(u,[1 1]);
</code></pre>

<p>And verify the results with:</p>

<pre><code>laplacian = del2(u)*4;
</code></pre>

<p>Which should correspond to <img src='http://s.wordpress.com/latex.php?latex=g&#038;bg=ffffff&#038;fg=1c1c1c&#038;s=0' alt='g' title='g' class='latex' /> (with, of course, border discrepancies). There is, perhaps, some entirely built-in way of doing all this, hidden somewhere in the terse documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cryptosystem.org/archives/2009/09/using-matlab-for-numerical-solutions-to-the-discrete-poisson-equation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
