<?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>MATLAB &#8211; Henry Poon&#039;s Blog</title>
	<atom:link href="https://blog.henrypoon.com/blog/tag/matlab/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.henrypoon.com</link>
	<description></description>
	<lastBuildDate>Tue, 04 Oct 2022 22:11:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">83044199</site>	<item>
		<title>Numerical Methods for Non-Linear Optimization</title>
		<link>https://blog.henrypoon.com/blog/2011/01/29/numerical-methods-for-non-linear-optimization/</link>
					<comments>https://blog.henrypoon.com/blog/2011/01/29/numerical-methods-for-non-linear-optimization/#comments</comments>
		
		<dc:creator><![CDATA[hp]]></dc:creator>
		<pubDate>Sat, 29 Jan 2011 17:55:33 +0000</pubDate>
				<category><![CDATA[computer stuff]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Calculus]]></category>
		<category><![CDATA[Chan]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Contents]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[Dime]]></category>
		<category><![CDATA[EAR]]></category>
		<category><![CDATA[Equation solving]]></category>
		<category><![CDATA[Equations]]></category>
		<category><![CDATA[Fields of mathematics]]></category>
		<category><![CDATA[Final]]></category>
		<category><![CDATA[Find]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Gradient descent]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[IME]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[It]]></category>
		<category><![CDATA[Lie]]></category>
		<category><![CDATA[Mathematical optimization]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[Motivation]]></category>
		<category><![CDATA[Numerical analysis]]></category>
		<category><![CDATA[Numerical partial differential equations]]></category>
		<category><![CDATA[Partial differential equation]]></category>
		<category><![CDATA[Plan]]></category>
		<category><![CDATA[Plug]]></category>
		<category><![CDATA[Rolling]]></category>
		<category><![CDATA[STR]]></category>
		<category><![CDATA[Thou]]></category>
		<category><![CDATA[Time]]></category>
		<category><![CDATA[Travel]]></category>
		<category><![CDATA[tuna]]></category>
		<category><![CDATA[UNC]]></category>
		<category><![CDATA[Unification]]></category>
		<category><![CDATA[Variable]]></category>
		<category><![CDATA[Vol]]></category>
		<category><![CDATA[Z]]></category>
		<guid isPermaLink="false">http://henrypoon.mooo.com/blog/numerical-methods-for-non-linear-optimization</guid>

					<description><![CDATA[Contents Introduction Motivation Newton’s Method Steepest Descent Method Limitation Sources Introduction In many sciences, the problem of non-linear optimization appears quite often.&#160; What happens if they’re non-linear?&#160; Sometimes, the system of equations remains easy to solve even given non-linearity, but other times, the possibility of solving the problem analytically may not exist if the algebra [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Contents</h1>
<ul>
<li>Introduction</li>
<li>Motivation</li>
<li>Newton’s Method</li>
<li>Steepest Descent Method</li>
<li>Limitation</li>
<li>Sources</li>
</ul>
<h1>Introduction</h1>
<p>In many sciences, the problem of non-linear optimization appears quite often.&nbsp; What happens if they’re non-linear?&nbsp; Sometimes, the system of equations remains easy to solve even given non-linearity, but other times, the possibility of solving the problem analytically may not exist if the algebra gets too complicated.&nbsp; The solution lies in using numerical methods (approximations).&nbsp; Here, I present two common methods: Newton’s Method, and the Steepest Descent Method.&nbsp;</p>
<p>Each method has its own limitations and the choice of which method to use depends on the problem.&nbsp; These methods involve using iterative computation in order to reach a solution.&nbsp; Luckily, computers can prove useful in providing solutions very quickly due to its quick processing power for numerical computation.</p>
<p>In order to understand some of the concepts and terms used in this article, the reader has knowledge in multivariable calculus.&nbsp; Also, equations in picture form made the line spacing look funny, so I’m going to do my best write the equations as text.&nbsp; I should also point out that I couldn’t write the vector arrow on top of the variables in text form so vectors are written in bold.</p>
<h1>Motivation</h1>
<p>In calculus, the idea of optimization involves taking the derivative of the function and equating it to zero.&nbsp; In multivariable calculus, the same idea applies except, we set all the partial derivatives to zero and solve the system.</p>
<p>So in general, if we are given a function:</p>
<p align="center"><em>ƒ(<strong>x</strong>)</em>, where <strong>x</strong> = [x<sub>1</sub>, x<sub>2</sub>, … x<sub>n</sub>]<sup>T</sup></p>
<p>The maximum or minimum can be found by taking a partial derivative with respect to the vector <strong><em>x</em></strong>.&nbsp; This gives the following equations:</p>
<p align="center"><em>∂<em>ƒ</em>/dx<sub>1</sub> = 0</em></p>
<p align="center"><em>∂<em>ƒ</em>/dx<sub>2</sub> = 0</em></p>
<p align="center">…</p>
<p align="center"><em>∂<em>ƒ</em>/dx<sub>n</sub> = 0</em></p>
<p>A short-hand way of expressing the above equations is by using the gradient vector, giving<em>∇ƒ(<strong>x</strong>) = <strong>0</strong></em>.&nbsp; By definition, the gradient is defined by:</p>
<p align="center"><em>∇ƒ(<strong>x</strong>) = [<em>∂<em>ƒ</em>/dx<sub>1</sub>, ∂<em>ƒ</em>/dx<sub>2</sub></em>, … , <em>∂<em>ƒ</em>/dx<sub>n</sub></em>].</em></p>
<p>An analytical solution is not always possible, hence the motivation for approximating the solution using numerical methods.</p>
<h1>Newton’s Method</h1>
<p>In general, this method is written below.&nbsp; Explanation of each step will follow.</p>
<ol>
<li>Given the equations to solve, derive equations for the approximating line or plane, depending on whether the problem is 2D, 3D, nD, etc.</li>
<li>Using the linearized equations, solve the system of equations</li>
<li>Using the results from step 2, generate new equations for a new approximating line or plane, and repeat step 2.</li>
</ol>
<p>This method also involves starting with a guess, denoted by a variable with a subscript of 0.&nbsp; A guess is required, because this is the starting point for this method.&nbsp; Using this guess, each successive solution <em>should</em> converge (more on that later).</p>
<h2>Step 1</h2>
<p>In order to solve the equations <em>∇ƒ(<strong>x</strong>) = <strong>0</strong></em> (see Motivation for more information), we can apply multivariate Newton’s method to approximate the solution.&nbsp; Like the single-variate (two-dimensional) version, a linear approximation is required.&nbsp; However, instead of having an approximation in the form of a line, the approximation is a plane instead.</p>
<p align="center">2D (line): <em>ƒ(x) ≈ ƒ(x<sub>0</sub>) + ƒ’(x<sub>0</sub>)(x &#8211; x<sub>0</sub>)</em></p>
<p align="center">3D (plane): <em>ƒ(x, y) ≈ ƒ(x<sub>0</sub>, y<sub>0</sub>) + ƒ<sub>x</sub>(x<sub>0</sub>, y<sub>0</sub>)(x &#8211; x<sub>0</sub>) + ƒ<sub>y</sub>(x<sub>0</sub>, y<sub>0</sub>)(y &#8211; y<sub>0</sub>)</em></p>
<p align="center">nD (nD plane): <em>ƒ(<strong>x</strong>) ≈ ƒ(<strong>x<sub>0</sub></strong>) + <em>∇ƒ(<strong>x<sub>0</sub></strong>)(<strong>x</strong> &#8211; <strong>x<sub>0</sub></strong>)</em></em></p>
<p>Since we are trying to optimize, we have the partial derivatives of the function we are trying to optimize, but in order to apply Newton’s Method, we need a linear approximation of the partial derivatives.&nbsp; This means that in order to approximate the partial derivatives, we need to take another derivative in order to get the slopes of the approximating planes.&nbsp; If that description didn’t make sense, try the following.&nbsp; For an equation with two independent variables, there will be two equations for the partial derivatives, one for x and one for y.&nbsp; Taking partial derivatives from each of those equations again will give the slopes of the plane that approximate the first partial derivative.&nbsp; So if we have two independent variables, we end up getting four equations: ƒ<sub>xx</sub>, ƒ<sub>xy</sub>,ƒ<sub>yy</sub>, and ƒ<sub>yx</sub> (ƒ<sub>xy </sub>and ƒ<sub>yx</sub> should be the same).&nbsp; In general, for n partial derivatives, we would have <em>n²</em> equations if we took another set of derivatives.&nbsp; The approximations for both of the partial derivatives would be:</p>
<p align="center"><em>ƒ<sub>x</sub>(x, y) ≈ <em>ƒ<sub>x</sub></em>(x<sub>0</sub>, y<sub>0</sub>) + ƒ<sub>xx</sub>(x<sub>0</sub>, y<sub>0</sub>)(x &#8211; x<sub>0</sub>) + ƒ<sub>xy</sub>(x<sub>0</sub>, y<sub>0</sub>)(y &#8211; y<sub>0</sub>)</em></p>
<p align="center"><em>ƒ<sub>y</sub>(x, y) ≈ <em>ƒ<sub>y</sub></em>(x<sub>0</sub>, y<sub>0</sub>) + ƒ<sub>yy</sub>(x<sub>0</sub>, y<sub>0</sub>)(x &#8211; x<sub>0</sub>) + ƒ<sub>yx</sub>(x<sub>0</sub>, y<sub>0</sub>)(y &#8211; y<sub>0</sub>)</em></p>
<p align="left">Plug in the values for the second derivatives, and the values from the initial guess (the x y values and what function value you get using those values).</p>
<h2>Step 2</h2>
<p>Although the equations generated from step 1 is yet another system of equations, the main difference is that the equations from step 1 are <strong>linear</strong>.&nbsp; This means that the system can be solved analytically.&nbsp; Solve for the solution of the system using <a href="http://en.wikibooks.org/wiki/Linear_Algebra/Row_Reduction_and_Echelon_Forms" target="_blank" rel="noopener noreferrer">row reduction methods</a>, or other methods.&nbsp; The solution from solving the system will be the values used for the next iteration and will replace the guessed values.&nbsp; The solution may or may not be close to the actual solution.&nbsp; More iterations need to be done to see if the answer changes with each iteration.&nbsp; Usually if the guess is really far away, then it takes a lot of iterations to get it really close.&nbsp; However, sometimes, a bad guess may cause the solution to diverge, meaning that the solution will be further and further off.</p>
<h2>Step 3</h2>
<p>Generate new equations for the approximating planes using the solution from step 2.&nbsp; Using these equations, go back to step 2 and solve it again.&nbsp; This method is super tedious if done on paper.&nbsp; Matlab can help, but then again, Matlab can do the entire approximation with just a few commands, but not everyone has access to it unfortunately.</p>
<p>Hopefully after a few iterations, the solution stops changing.&nbsp; When that happens, you can substitute the final solution back into the original equation to see if it works!</p>
<h1>Steepest Descent Method</h1>
<p>Here is the general method:</p>
<ol>
<li>Find the direction of the maximum or minimum directional derivative</li>
<li>Follow it until a maximum or minimum is reached</li>
</ol>
<p>Newton’s Method is sufficient in a lot of cases, but sometimes it may be too hard to go beyond first derivatives.&nbsp; This main idea of this method is to use the first derivative and follow the direction in which the function gets larger/smaller.&nbsp; A fundamental concept used in this method is the directional derivative.&nbsp; Since in 3D (or higher dimensions), we are not restricted to movement along the x axis.&nbsp; This means we could move in any random direction, and in each direction the rate of change is going to be different.&nbsp; To calculate the max or min, we need to calculate where the directional derivative is largest or smallest.&nbsp; Just like a boulder rolling down the steepest slope on the hill.</p>
<p>Starting with a guess, we can find out the value of the function at the point and the direction to travel in.&nbsp; Take a step in that direction and see how the value of the function changed.&nbsp; Keep doing that until a max or min is reached.</p>
<h2>Step 1</h2>
<p>The definition of the directional derivative is the dot product of the gradient and the unit vector specifying the direction.&nbsp; Here is the definition of the directional derivative:</p>
<p align="center"><em>D<sub>u</sub>ƒ = ∇ƒ(<strong>x</strong>)<strong>·u</strong></em></p>
<p align="left">Since the definition involves a dot product, it is true that if the two vectors are orthogonal, the resulting dot product is zero.&nbsp; The direction that this derivative is in is called the <em>level direction</em>.&nbsp; But if the two vectors are parallel, we get the highest value of the directional derivative.&nbsp; For a vector that is parallel in the same direction, the directional derivative will be a maximum, but if the vector is antiparallel (parallel but both pointing opposite ways), the directional derivative will be a minimum.</p>
<p align="left">Since the maximum value of the directional derivative is parallel to the gradient, we can calculate the unit vector of the gradient and use it as the unit vector <strong><em>u.&nbsp; </em></strong>In order to get the minimum, simply flip the direction of the vector by multiplying it by –1.</p>
<p align="center"><em><strong>u = </strong>∇ƒ(<strong>x</strong>)/|∇ƒ(<strong>x</strong>)|</em></p>
<p align="left">Although not necessarily required, substituting this value into the definition of the directional derivative, and doing some simplification with algebra, the maximum and minimum directional derivative is given by:</p>
<p align="center"><em>Max D<sub>u</sub>ƒ = |∇ƒ(<strong>x</strong>)|</em></p>
<p align="center"><em>Min D<sub>u</sub>ƒ = -|∇ƒ(<strong>x</strong>)|</em></p>
<p align="left">Using the initial guess, calculate the direction of to travel in at that point.</p>
<h2>Step 2</h2>
<p>Knowing the direction to travel choose a desired step size “h”.&nbsp; This means that if the equation will be traversed in a particular direction, we need to know exactly <em>how</em> far to go.&nbsp; Choosing smaller step sizes gives better computational accuracy but increases computation time.</p>
<p>To get to the maximum, use the direction for the maximum directional derivative.&nbsp; To get the minimum, use the direction for the smallest directional derivative.</p>
<p>Starting with a guess, calculate the function value at that point.&nbsp; Then take a step in the direction given by Step 1 using the step size.&nbsp; The variable “u” here is a component of the unit vector</p>
<p align="center"><em>x<sub>n+1</sub> = x<sub>n</sub> + u<sub>x</sub>h</em></p>
<p align="center">y<em><sub>n+1</sub> = y<sub>n</sub> + u<sub>y</sub>h</em></p>
<p align="left">At this new value, calculate the function value again.&nbsp; Keep repeating this until the maximum or minimum is reached.</p>
<h1>Limitation</h1>
<p>The approximation is generally only as good as the initial guess.&nbsp; Sometimes, a bad guess can lead to a solution that never converges.&nbsp; In that case, a different value must be used in order to find a converging solution.&nbsp; For example, if we have a fourth order polynomial function, a local extreme will exist.&nbsp; As x approaches –∞ or ∞, y will do the same.&nbsp; If I use the steepest descent and give a bad guess, my solution will go to ∞ instead of the local extreme.</p>
<h1>Sources</h1>
<ol>
<li>Calculus by Gilbert Strang</li>
<li>Applied Regression Analysis by Norman Draper and Harry Smith</li>
</ol>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.henrypoon.com/blog/2011/01/29/numerical-methods-for-non-linear-optimization/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">763</post-id>	</item>
		<item>
		<title>Installing RXTX for Serial Communication with Java</title>
		<link>https://blog.henrypoon.com/blog/2010/12/25/installing-rxtx-for-serial-communication-with-java/</link>
					<comments>https://blog.henrypoon.com/blog/2010/12/25/installing-rxtx-for-serial-communication-with-java/#comments</comments>
		
		<dc:creator><![CDATA[hp]]></dc:creator>
		<pubDate>Sun, 26 Dec 2010 05:41:03 +0000</pubDate>
				<category><![CDATA[computer stuff]]></category>
		<category><![CDATA[64-bit computers]]></category>
		<category><![CDATA[64-bit computing]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Comment]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Computing platforms]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[Digital technology]]></category>
		<category><![CDATA[Directory]]></category>
		<category><![CDATA[Download]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[Installation]]></category>
		<category><![CDATA[ise]]></category>
		<category><![CDATA[It]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java platform]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[Opera]]></category>
		<category><![CDATA[Operating systems]]></category>
		<category><![CDATA[Page]]></category>
		<category><![CDATA[Port]]></category>
		<category><![CDATA[Program Files]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming languages]]></category>
		<category><![CDATA[RAR]]></category>
		<category><![CDATA[Serial]]></category>
		<category><![CDATA[Serial communication]]></category>
		<category><![CDATA[Solaris]]></category>
		<category><![CDATA[STR]]></category>
		<category><![CDATA[Struct]]></category>
		<category><![CDATA[Thou]]></category>
		<category><![CDATA[tuna]]></category>
		<category><![CDATA[UNIX System V]]></category>
		<category><![CDATA[Wiki]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[X86]]></category>
		<category><![CDATA[Z]]></category>
		<guid isPermaLink="false">http://henrypoon.mooo.com/blog/installing-rxtx-for-serial-communication-with-java</guid>

					<description><![CDATA[Java supports communication to serial ports, but not with its default installation.&#160; It requires an installation of an external library.&#160; Currently, two options exist for achieving serial communication: RXTX JavaComm Unfortunately, the current version of JavaComm does not support Windows, and only supports Solaris SPARC, Solaris x86, and Linux x86.&#160; Since I use none of [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Java supports communication to serial ports, but not with its default installation.&nbsp; It requires an installation of an external library.&nbsp; Currently, two options exist for achieving serial communication:</p>



<ul class="wp-block-list"><li><a href="http://rxtx.qbang.org/wiki/index.php/Main_Page" target="_blank" rel="noreferrer noopener">RXTX</a></li><li><a rel="noopener noreferrer" href="http://www.oracle.com/technetwork/java/index-jsp-141752.html" target="_blank">JavaComm</a></li></ul>



<p class="wp-block-paragraph">Unfortunately, the current version of JavaComm does not support Windows, and only supports Solaris SPARC, Solaris x86, and Linux x86.&nbsp; Since I use none of those operating systems, this article will only discuss how to install RXTX in Windows.</p>



<p class="wp-block-paragraph">UPDATE: 12 Sept 2011 – Thanks to a comment from Kurt Zoglmann, there are instructions on how to do this with a Mac!  I never tried this myself because I don’t have a Mac, but the procedures stated there look good to me (see <a href="#comments">comments</a>).</p>



<p class="wp-block-paragraph">A 32 and 64 bit version of the library exists. &nbsp; I&#8217;ve updated this article from when it was first published to include the new locations for where to download the libraries, but I have not gone through the installation of the libraries again, so I&#8217;m not surprised if the rest of the content on this page is outdated too.</p>



<p class="wp-block-paragraph">32-bit version: <a rel="noopener noreferrer" href="http://rxtx.qbang.org/wiki/index.php/Download" target="_blank">http://rxtx.qbang.org/wiki/index.php/Download</a><br>64-bit version: <a rel="noopener noreferrer" href="http://fizzed.com/oss/rxtx-for-java" target="_blank">http://fizzed.com/oss/rxtx-for-java</a></p>



<p class="wp-block-paragraph">To install the libraries (instructions from <a href="http://www.jcontrol.org/download/readme_rxtx_en.html" target="_blank" rel="noopener noreferrer">JControl</a>):</p>



<ol class="wp-block-list"><li>Copy rxtxSerial.dll to %JAVA_HOME%\bin, (%JAVA_HOME% is the folder where JRE is installed on your system; e.g. c:\Program Files\Java\j2re1.4.1_01)</li><li>Copy RXTXcomm.jar to %JAVA_HOME%\lib\ext</li></ol>



<p class="wp-block-paragraph">Once installed, the IDE will need to know where to look for these installed files.&nbsp; Even though the files exist in the JRE directory, each project needs to know about these files.&nbsp; More information on <a href="http://rxtx.qbang.org/wiki/index.php/Using_RXTX" target="_blank" rel="noopener noreferrer">this website</a>.</p>



<p class="wp-block-paragraph">In order to start coding with this library, import gnu.io.*.  For more information about Java Serial Programming, look <a href="http://henrypoon.wordpress.com/2010/10/11/java-serial-programming/" target="_blank" rel="noreferrer noopener">here</a>.</p>



<p class="wp-block-paragraph">Happy coding!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.henrypoon.com/blog/2010/12/25/installing-rxtx-for-serial-communication-with-java/feed/</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">543</post-id>	</item>
	</channel>
</rss>
