{"id":85,"date":"2017-04-05T17:54:00","date_gmt":"2017-04-05T17:54:00","guid":{"rendered":"https:\/\/therenaissanceraven.com\/?p=85"},"modified":"2017-04-05T17:54:00","modified_gmt":"2017-04-05T17:54:00","slug":"solving-laplaces-equation-with-java","status":"publish","type":"post","link":"https:\/\/therenaissanceraven.com\/?p=85","title":{"rendered":"Solving Laplace&#8217;s Equation with Java"},"content":{"rendered":"<p>To solve Laplace&#8217;s equation in Java we are going to use the finite difference method. A very good explanation on how it is derived and the math behind it can be found online [1].<\/p>\n<p>To start this process, we are going to create a pseudo 2D array in Java. This can be envisioned as discretizing our field into certain x-step and y-step elements (called Blocks in this code). To accomplish this, we will create an object called ValueBox where we cans store its value. Then we simply create a 2D array of the objects set to our desired size:<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ValueBox<\/span> <span style=\"color: #333333;\">{<\/span><br \/><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">float<\/span> value <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>f<span style=\"color: #333333;\">;<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">boolean<\/span> boundaryCondition <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">ValueBox<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">float<\/span> value<span style=\"color: #333333;\">){<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> value<span style=\"color: #333333;\">;<\/span><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/><span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">1<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #008800; font-weight: bold;\">private<\/span> ValueBox<span style=\"color: #333333;\">[][]<\/span> valueBoxArray <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> valueBox<span style=\"color: #333333;\">[<\/span>xBlocks<span style=\"color: #333333;\">][<\/span>yBlocks<span style=\"color: #333333;\">];<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Then we can set our boundary conditions for the system. For Dirichlet boundary conditions, the process is quite simple, we only need set the initial values (boundary conditions) of the boxes before we solve the iteration. We also need to flag the boxes as boundary conditions so that their value remains fixed. The following code snippet will set the bottom of the box, y = 0, to BC1 and the top of the box, y = yBlocks to BC2<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> x <span style=\"color: #333333;\">&lt;<\/span> xBlocks<span style=\"color: #333333;\">;<\/span> x<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>            <span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> y <span style=\"color: #333333;\">&lt;<\/span> yBlocks<span style=\"color: #333333;\">;<\/span> y<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                <span style=\"color: #888888;\">\/\/Sets all blocks to an initial value<\/span><br \/>                valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> PathBox<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>f<span style=\"color: #333333;\">);<\/span><br \/><br \/>                <span style=\"color: #888888;\">\/\/at y=0 boundary condition<\/span><br \/>                <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>y<span style=\"color: #333333;\">==<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">){<\/span><br \/>                    <span style=\"color: #888888;\">\/\/sets it as a boundary condition<\/span><br \/>                    valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">boundaryCondition<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">;<\/span><br \/>                    <span style=\"color: #888888;\">\/\/sets these blocks to boundary condition 1 (which can be any integer)<\/span><br \/>                    valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> boundaryCondition1<span style=\"color: #333333;\">;<\/span><br \/>                <span style=\"color: #333333;\">}<\/span><br \/><br \/>                <span style=\"color: #888888;\">\/\/at y = yBlocks boundary condition<\/span><br \/>                <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>y<span style=\"color: #333333;\">==<\/span>yBlocks-<span style=\"color: blue;\"><span style=\"background-color: white;\"><b>1<\/b><\/span><\/span><span style=\"color: #333333;\">){<\/span><br \/>                    <span style=\"color: #888888;\">\/\/sets it as a boundary condition<\/span><br \/>                    valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">boundaryCondition<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">;<\/span><br \/>                    <span style=\"color: #888888;\">\/\/sets these blocks to boundary condition 2 (which can be any integer)<\/span><br \/>                    valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> boundaryCondition2<span style=\"color: #333333;\">;<\/span><br \/>                <span style=\"color: #333333;\">}<\/span><br \/>            <span style=\"color: #333333;\">}<\/span><br \/>        <span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now that we have are blocks initialized and boundary conditions set, here is the for general for-loop design that you will need to iterate over:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> iteration <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> iteration <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">500<\/span><span style=\"color: #333333;\">;<\/span> iteration<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>            <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> x <span style=\"color: #333333;\">&lt;<\/span> xBlocks<span style=\"color: #333333;\">;<\/span> x<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> y <span style=\"color: #333333;\">&lt;<\/span> yBlocks<span style=\"color: #333333;\">;<\/span> y<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                    <br \/>                    <span style=\"color: #888888;\">\/\/If the block is not a boundary condition<\/span><br \/>                    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(!<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">boundaryCondtion<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                        <br \/>                        <span style=\"color: #888888;\">\/\/If we are at the bottom row, but neither of the corners<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>y <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> x <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> x <span style=\"color: #333333;\">!=<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.3333f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">((<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">));<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the top row, but neither of the corners<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>y <span style=\"color: #333333;\">==<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> x <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> x <span style=\"color: #333333;\">!=<\/span> xBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.3333f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">((<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">));<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the right wall but at neither corners<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> xBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">!=<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.3333f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">((<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">));<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the left wall but at neither corners<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">!=<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.3333f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">((<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">));<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the origin<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.5f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">(<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">);<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the bottom right corner<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> xBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.5f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">(<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">);<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the top left corner<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">==<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.5f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">(<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">);<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are at the top right corner<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> xBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> y <span style=\"color: #333333;\">==<\/span> yBlocks <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.5f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">(<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">);<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/><br \/>                        <span style=\"color: #888888;\">\/\/If we are anywhere in the middle<\/span><br \/>                        <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span><br \/>                            valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.25f<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #333333;\">((<\/span>valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">][<\/span>y<span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span> <span style=\"color: #333333;\">+<\/span> valueBoxArray<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">][<\/span>y <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">].<\/span><span style=\"color: #0000cc;\">value<\/span><span style=\"color: #333333;\">));<\/span><br \/>                        <span style=\"color: #333333;\">}<\/span><br \/>                    <span style=\"color: #333333;\">}<\/span><br \/>                <span style=\"color: #333333;\">}<\/span><br \/>            <span style=\"color: #333333;\">}<\/span><br \/>        <span style=\"color: #333333;\">}<\/span><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Once the iterations are complete, you may simply print out the values or graph them from what is the valueBoxArray. 500 iterations is plenty, for smaller systems where the x-step and y-step elements are less than 100. However, if you are doing very large systems, &gt;500 step elements, you may wish to check convergence by comparing the previous iteration value of a certain box after each loop.<\/p>\n<p>Here are some examples of solutions where they are color coded based on value. Green = BC1 and Blue = BC2.<\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-xSgoaq4y8Aw\/WOUHeA8bHtI\/AAAAAAAABmY\/P-PhORwqBJ0uLOcj0aJKKQheEMZW7g8NgCLcB\/s1600\/Capture1.PNG\" style=\"margin-left: auto; margin-right: auto;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"400\" src=\"https:\/\/4.bp.blogspot.com\/-xSgoaq4y8Aw\/WOUHeA8bHtI\/AAAAAAAABmY\/P-PhORwqBJ0uLOcj0aJKKQheEMZW7g8NgCLcB\/s400\/Capture1.PNG\" width=\"380\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">At Y = 0, Value = BC1 and at Y = yBlocks, Value = BC2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-MQCus_LgmlI\/WOUHeJ56yJI\/AAAAAAAABmc\/yWWy_mdUfAgGdWI3ck4w5dTVHgIYVRgYwCLcB\/s1600\/Capture2.PNG\" style=\"margin-left: auto; margin-right: auto;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"400\" src=\"https:\/\/4.bp.blogspot.com\/-MQCus_LgmlI\/WOUHeJ56yJI\/AAAAAAAABmc\/yWWy_mdUfAgGdWI3ck4w5dTVHgIYVRgYwCLcB\/s400\/Capture2.PNG\" width=\"378\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">At Y = 0 &amp; Y = yBlocks, Value = BC1 and at X=0 &amp; X=xBlocks, Value = BC2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Citations:<br \/>http:\/\/www.public.iastate.edu\/~akmitra\/aero361\/design_web\/Laplace.pdf<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To solve Laplace&#8217;s equation in Java we are going to use the finite difference method. A very good explanation on how it is derived and the math behind it can be found online [1]. To start this process, we are going to create a pseudo 2D array in Java. This can be envisioned as discretizing&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-misc"],"_links":{"self":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":0,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}