<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://srtsolutions.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Anne Marsan: Software for Engineers</title><link>http://srtsolutions.com/blogs/annemarsan/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Design Optimization - Part 1</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/09/13/4566.aspx</link><pubDate>Sat, 13 Sep 2008 23:25:33 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:4566</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=4566</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/09/13/4566.aspx#comments</comments><description>&lt;p&gt;Currently I&amp;#39;m working on three different projects, and the one common component of all three is that they involve design optimization. Design optimization can be used in any discipline to find the &amp;quot;best&amp;quot; design for a particular system (the definition of &amp;quot;best&amp;quot; will vary from discipline to discipline, from system to system, and from one situation to another). Methodical, mathematic approaches to design optimization have been developed over the past 50 years, and their practical implementation, especially for large problems, has been made possible through the use of computers. A basic understanding of optimization is a good thing for software developers to have, especially those who are writing scientific and technical tools. This post is part one of a short primer on design optimization. I perused the book &lt;em&gt;Principles of Optimal Design: Modeling and Computation&lt;/em&gt; by Panos Papalambros and Douglass Wilde before writing this, so their influence cannot be escaped.&lt;/p&gt; &lt;p&gt;Any system that can be modeled mathematically can be optimized. This means that disciplines as varied as engineering, physics, chemistry, biology, statistics, economics, finance, and others can make use of design optimization. Within engineering, systems such as automotive engines, building structures, HVAC systems, integrated circuits, etc. can be optimized. Two very simple systems that could be optimized are shown below. The first is a collection of 2D data points that are to be described by quadratic polynomial (a parabola). The second is a cantilevered beam that has a load applied at its tip. Different aspects of the beam can be described with mathematical equations, such as the geometry of the beam, the loading along the length of the beam, and the stresses in the beam at an point within it.&lt;/p&gt; &lt;p&gt;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_8.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="261" alt="image" src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_thumb_3.png" width="504" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The next requirement for a design optimization problem is to identify a set of &lt;strong&gt;variables&lt;/strong&gt; that describe design alternatives. These are parameters that the designer has control over, things that can be changed during the design process. For our data fitting problem above, the design variables would be the coefficients in the polynomial; the engineer can tweak their values to modify how the polynomial fits the data point. For the cantilevered beam problem, the designer has control over the length, height, and width of the beam (additionally, the beam material could be a design variable, but for this example we&amp;#39;ll say that the material choice has been predetermined and the designer cannot change it).&lt;/p&gt; &lt;p&gt;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_6.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="259" alt="image" src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_thumb_2.png" width="504" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Now we need to choose an &lt;strong&gt;objective function&lt;/strong&gt;, expressed in terms of the design variables, that should be minimized. The objective function is frequently called a cost function. Note that if you ask any manager of a design optimization project what it is they are trying to minimize, they will say cost! It&amp;#39;s up to the designers (and managers) to figure out what contributes to system cost and express it mathematically. In our beam example, clearly the larger the beam is, the more it will cost. But the beam has to perform a function, such as support a floor in a building, and therefore we can&amp;#39;t minimize the size of the beam. Instead, a more typical objective would be to minimize beam deflection at its tip (if this beam is indeed supporting a floor in a building, it will make the building occupants feel a lot better if the floor doesn&amp;#39;t sag when they walk over it). For our data fitting problem, the objective is to find the polynomial that best fits the data. A typical way of measuring the fit is to sum the squares of the distance of the given data points to the polynomial.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_10.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="258" alt="image" src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_thumb_4.png" width="504" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Designers next need to identify a set of &lt;strong&gt;constraints&lt;/strong&gt;, expressed in terms of the design variables, that must be satisfied in order for their system to be valid. These can be practical limits on design variables, or more complicated relationships that describe the function of the system. For instance, in our data fitting problem, perhaps we want to ensure that the peak of our parabola is at x greater than some value d. Typical constraints for a beam design problem might be that the material properties are fixed because a certain material has been selected, or the stresses need to be below the limits of the material.&lt;/p&gt; &lt;p&gt;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_12.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="258" alt="image" src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/DesignOptimizationPart1_11071/image_thumb_5.png" width="504" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Now we have a complete specification for our design optimization problem: variables, objective function, and constraints. The formal way of expressing an optimization problem is:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;minimize f(&lt;strong&gt;x&lt;/strong&gt;)&lt;br /&gt;s.t. (subject to)&lt;br /&gt;g(&lt;strong&gt;x&lt;/strong&gt;) &amp;lt; 0&lt;br /&gt;h(&lt;strong&gt;x&lt;/strong&gt;) = 0&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The final step is to solve the problem, which is of course easier said than done! I will discuss this in the next installment.&lt;/p&gt; &lt;p&gt;But before I close, I want to point out that modeling a system using mathematical equations can itself be a challenging problem. In engineering, we spend much of our bachelors degree education learning how to do this, and it&amp;#39;s something that doesn&amp;#39;t always come easily, even to seasoned practitioners. &lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=4566" width="1" height="1"&gt;</description></item><item><title>Blogging Tools</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/08/15/blogging-tools.aspx</link><pubDate>Fri, 15 Aug 2008 19:06:10 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:4271</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=4271</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/08/15/blogging-tools.aspx#comments</comments><description>&lt;p&gt;Since I&amp;#39;m new to this blogging thing, it has taken me awhile to figure out what tools to use to make writing and publishing posts easy to do. After reading what others have to say on the topic, and talking to my co-workers at SRT, I have hit upon &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=d2baeda0-aa9a-4080-9202-1f23902d1169&amp;amp;displaylang=en"&gt;Windows Live Writer&lt;/a&gt; with the &lt;a href="http://lvildosola.blogspot.com/2007/02/code-snippet-plugin-for-windows-live.html"&gt;Code Snippet&lt;/a&gt; plugin. But it hasn&amp;#39;t been straightforward getting these tools to cooperate with Community Server 2007.1, the server SRT uses for blogs. Here are two tricks to make things work right.&lt;/p&gt; &lt;p&gt;Windows Live Writer makes writing a post and including things such as images and videos and bits of code very easy. One of the big advantages is that it can go to your blog site and grab your template, so that as you compose your post you can see just what it will look like when it is published. I could have used that feature when I wrote my last post on ZedGraph and made the images too wide for the main text column. To get the template, WLW creates a temporary post, publishes it to the server, then goes to that post and downloads the CSS. Seems straightforward...except that it failed the first and second and third and fourth times I asked it to do this. Then I went searching for a solution. What I figured out is that in Community Server Dashboard, under Global Settings -&amp;gt; Default Post Settings, there is an option called &amp;quot;Auto Name Posts&amp;quot;. If set to &amp;quot;Yes&amp;quot;, CS tries to name each post based on the title, rather than use the de facto standard blog post naming convention, which is based on the date the post was created. After I disabled this option, WLW successfully located its post and downloaded my template to my local machine. Success!&lt;/p&gt; &lt;p&gt;My second problem that I&amp;#39;ve been struggling with is coming up with a quick and easy way to insert formatted, colored code into posts. Before I had WLW, I would cut and paste code and then go and manually add &amp;lt;div&amp;gt; and &amp;lt;span&amp;gt; tags with with embedded CSS styles using the horrible Community Server interface with the TinyMCE editor. Death seemed preferable. If I specified colors like this:&lt;/p&gt; &lt;p&gt;&amp;lt;span style=&amp;quot;color: blue;&amp;quot;&amp;gt;&lt;/p&gt; &lt;p&gt;things worked just fine. I would get &lt;span style="color:#0000ff;"&gt;blue text&lt;/span&gt;. But if instead I specified:&lt;/p&gt; &lt;p&gt;&amp;lt;span style=&amp;quot;color: #0000FF;&amp;quot;&amp;gt;&lt;/p&gt; &lt;p&gt;then the TinyMCE editor would modify the tag to look like this:&lt;/p&gt; &lt;p&gt;&amp;lt;span style=&amp;quot;color: rgb(0,0,255);&amp;quot;&amp;gt;, which would then turn into:&lt;/p&gt; &lt;p&gt;&amp;lt;span&amp;gt;&lt;/p&gt; &lt;p&gt;by the time it was published. I would not get blue text. Argh!&lt;/p&gt; &lt;p&gt;By using WLW with the Code Snippet plugin, as long as I never open up the post in the TinyMCE editor before it&amp;#39;s published, everything is fine and my span tags with their color styles are left untouched. Stay away from TinyMCE!&lt;/p&gt; &lt;p&gt;I haven&amp;#39;t worked out all of my blog problems just yet. For instance, Code Snippet supports C# code, but not Matlab. But I got two big problems solved, so I will declare success for the moment.&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=4271" width="1" height="1"&gt;</description></item><item><title>ZedGraph for 2D Plotting</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/08/01/zedgraph-for-2d-plotting.aspx</link><pubDate>Fri, 01 Aug 2008 18:42:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:4114</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=4114</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/08/01/zedgraph-for-2d-plotting.aspx#comments</comments><description>&lt;p&gt;Previously I wrote about choosing ZedGraph as my graphing tool in a C#.Net project for a client. Today I&amp;#39;d like to give an update on how it&amp;#39;s going. I have been very pleased with this graphing library, and it has been relatively straightforward to figure out how to make it do what I want it to do.  &lt;/p&gt;&lt;p&gt;I&amp;#39;m plotting X-Y data that represents the shape of an airfoil on a 2D plot with grid lines, axes, and axes labels. One of my main concerns was having the ability to force the scales of the X and Y axes to be the same value. If they are locked, my plot will show the airfoil with the right proportions. If not, the airfoil appears to be a lot fatter than it really is.  &lt;/p&gt;&lt;p&gt;Figure 1. Airfoil with axis scales locked  &lt;/p&gt;&lt;h3&gt;&lt;/h3&gt; &lt;p&gt;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/ZedGraphfor2DPlotting_A980/airfoil_with_axes_locked_2.gif"&gt;&lt;img src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/ZedGraphfor2DPlotting_A980/airfoil_with_axes_locked_thumb.gif" style="border-width:0px;" alt="airfoil_with_axes_locked" border="0" height="412" width="644" /&gt;&lt;/a&gt;  &lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;/p&gt;&lt;p&gt;Figure 2. Airfoil with axis scales determined independently of one another  &lt;/p&gt;&lt;p&gt;&lt;a href="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/ZedGraphfor2DPlotting_A980/airfoil_with_axes_unlocked_2.gif"&gt;&lt;img src="http://srtsolutions.com/blogs/annemarsan/WindowsLiveWriter/ZedGraphfor2DPlotting_A980/airfoil_with_axes_unlocked_thumb.gif" style="border-width:0px;" alt="airfoil_with_axes_unlocked" border="0" height="412" width="644" /&gt;&lt;/a&gt;  &lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;/p&gt;&lt;p&gt;The ZedGraph library comes with a ZedGraph control, which contains of a collection of one or more GraphPane objects. The GraphPane holds all of the plotting information for a single plot, including settings and options in addition to the actual plotting data. For my particular problem I only need one GraphPane, which is the default. My first step is to initialize my GraphPane object.&lt;/p&gt; &lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;span&gt;public&lt;/span&gt; MainForm()&lt;br /&gt;{&lt;br /&gt;    InitializeComponent();&lt;br /&gt;&lt;br /&gt;     &lt;span&gt;// Initialize ZedGraph&lt;/span&gt;&lt;br /&gt;    GraphPane graphPane = zedGraphControl1.GraphPane;&lt;br /&gt;    graphPane.XAxis.Title.Text = &lt;span&gt;&amp;quot;Chord&amp;quot;&lt;/span&gt;;&lt;br /&gt;    graphPane.YAxis.Title.Text = &lt;span&gt;&amp;quot;Thickness&amp;quot;&lt;/span&gt;;&lt;br /&gt;    graphPane.XAxis.MajorGrid.IsVisible = &lt;span&gt;true&lt;/span&gt;;&lt;br /&gt;    graphPane.YAxis.MajorGrid.IsVisible = &lt;span&gt;true&lt;/span&gt;;&lt;br /&gt;    graphPane.Legend.IsVisible = &lt;span&gt;false&lt;/span&gt;;&lt;br /&gt;    graphPane.Title.IsVisible = &lt;span&gt;false&lt;/span&gt;;&lt;br /&gt;    &lt;span&gt;//graphPane.Title.Text = &amp;quot;ZedGraph&amp;quot;;&lt;/span&gt;&lt;br /&gt;    zedGraphControl1.Resize += &lt;span&gt;new&lt;/span&gt; System.EventHandler(this.zedGraphControl1_Resize);    &lt;br /&gt;    graphPane.AxisChangeEvent += &lt;span&gt;new&lt;/span&gt; GraphPane.AxisChangeEventHandler(graphPane_AxisChangeEvent);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The key here is the last line of initialization, where I add an event handler for the AxisChangeEvent. This event is invoked by calling the AxisChange() method in the ZedGraphControl object. One generally calls this method whenever new plotting data is added to a GraphPane; it takes care of adjusting the minimum and maximum axes values and the grid spacing. AxisChangeEvents are also generated whenever the user zooms or pans a GraphPane. It is not invoked, however, when the user resizes the ZedGraphControl, so I added an event handler for the Resize event that calls the AxisChange() method. The Resize event handler looks like this:&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; zedGraphControl1_Resize(&lt;span&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    zedGraphControl1.AxisChange();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The AxisChange event handler is called after the the ZedGraphControl has recomputed the minimum and maximum axis values and the grid spacing. It adjusts the minimum and maximum values so that the scale in the X and Y directions are equal, and then it recomputes the grid spacing to ensure that the grid does not have too many or too few lines.&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; graphPane_AxisChangeEvent(GraphPane target)&lt;br /&gt;{&lt;br /&gt;    GraphPane graphPane = zedGraphControl1.GraphPane;&lt;br /&gt;&lt;br /&gt;    &lt;span&gt;// Correct the scale so that the two axes are 1:1 aspect ratio&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;double&lt;/span&gt; scalex2 = (graphPane.XAxis.Scale.Max - graphPane.XAxis.Scale.Min) / graphPane.Rect.Width;&lt;br /&gt;    &lt;span&gt;double&lt;/span&gt; scaley2 = (graphPane.YAxis.Scale.Max - graphPane.YAxis.Scale.Min) / graphPane.Rect.Height;&lt;br /&gt;    &lt;span&gt;if&lt;/span&gt; (scalex2 &amp;gt; scaley2)&lt;br /&gt;    {&lt;br /&gt;        &lt;span&gt;double&lt;/span&gt; diff = graphPane.YAxis.Scale.Max - graphPane.YAxis.Scale.Min;&lt;br /&gt;        &lt;span&gt;double&lt;/span&gt; new_diff = graphPane.Rect.Height * scalex2;&lt;br /&gt;        graphPane.YAxis.Scale.Min -= (new_diff - diff) / 2.0;&lt;br /&gt;        graphPane.YAxis.Scale.Max += (new_diff - diff) / 2.0;&lt;br /&gt;    }&lt;br /&gt;    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (scaley2 &amp;gt; scalex2)&lt;br /&gt;    {&lt;br /&gt;        &lt;span&gt;double&lt;/span&gt; diff = graphPane.XAxis.Scale.Max - graphPane.XAxis.Scale.Min;&lt;br /&gt;        &lt;span&gt;double&lt;/span&gt; new_diff = graphPane.Rect.Width * scaley2;&lt;br /&gt;        graphPane.XAxis.Scale.Min -= (new_diff - diff) / 2.0;&lt;br /&gt;        graphPane.XAxis.Scale.Max += (new_diff - diff) / 2.0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span&gt;// Recompute the grid lines&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;float&lt;/span&gt; scaleFactor = graphPane.CalcScaleFactor();&lt;br /&gt;    Graphics g = zedGraphControl1.CreateGraphics();&lt;br /&gt;    graphPane.XAxis.Scale.PickScale(graphPane, g, scaleFactor);&lt;br /&gt;    graphPane.YAxis.Scale.PickScale(graphPane, g, scaleFactor);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Having the ZedGraph source control was extremely useful in figuring all of this out, particularly for resizing the grid after I have adjust the minimum and maximum axes values. In the end, I did not need to modify any of the ZedGraph source code, but because it is fairly clean and readable, I was able to use it in place of documentation when I needed more explanation than what was provided in the basic tutorial and class descriptions. &lt;/p&gt;
&lt;p&gt;So I still feel good about ZedGraph, and my customer is happy, too!&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=4114" width="1" height="1"&gt;</description></item><item><title>Euler Problem #14</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/07/24/euler-problem-14.aspx</link><pubDate>Thu, 24 Jul 2008 23:34:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:4015</guid><dc:creator>amarsan</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=4015</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/07/24/euler-problem-14.aspx#comments</comments><description>
   
   


   

&lt;p&gt;I&amp;#39;m on
  vacation this week in northern Michigan, enjoying the good weather
  and time with my family, and also reading quite a bit, including a
  book called &lt;i&gt;Beautiful Code&lt;/i&gt; published by O&amp;#39;Reilly (more about
  that in another post). One chapter in particular got me fired up to
  revisit Euler Problem 14, which I started to work on back in May,
  but got stuck and have been thinking about it ever since.&lt;/p&gt;

&lt;p&gt;The problem goes like this:&lt;/p&gt;

&lt;div style="margin:10px;"&gt;The following iterative sequence is defined for the set of positive integers:&lt;br /&gt;&amp;nbsp;&lt;br /&gt;
n -&amp;gt; n/2 (n is even)&lt;br /&gt;
n -&amp;gt; 3n + 1 (n is odd)&lt;br /&gt;
&lt;p&gt;Using the rule above and starting with 13, we generate the following sequence:&lt;br /&gt;
13 40 20 10 5 16 8 4 2 1&lt;/p&gt;
&lt;p&gt;It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it
is thought that all starting numbers finish at 1.&lt;/p&gt;
&lt;p&gt;Which starting number, under one million, produces the longest chain?&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#39;m solving the Euler problems in Matlab, and I&amp;#39;m working on a 867 MHz Mac
  PowerBook G4. Sometimes I also limit myself to the time it takes to
  drain the battery, just so that I don&amp;#39;t end up spending days and
  days solving a problem (I have to work once in awhile, and my kids
  get upset if I ignore them too much :). So my goal is generally to
  come up with a fast solution, which in most cases rules out brute
  force.&lt;/p&gt;

&lt;p&gt;With these goals in mind, I thought and thought and thought and
  thought about this problem, trying to come up with a trick that
  would allow me to rule out most numbers between 1 and 1,000,000. The
  most obvious thing to try is only the odd numbers. But I wanted to
  winnow down my search set even further. Not coming up with anything,
  I gave up and abandoned Euler problems altogether. I had plenty of
  other things to work on. But this problem kept gnawing at the edges
  of my brain.&lt;/p&gt;

&lt;p&gt;With so many problems in life, sometimes it&amp;#39;s best to stop
  thinking and start doing. So finally this week I threw in the towel
  and wrote some code to do a brute force search on all odd
  numbers. It definitely occured to me that once a sequence has been
  generated for a number, when that number shows up in a subsequent
  sequence I should reuse what has already been computed. But I got
  hung up in figuring out how big I should preallocate my array to
  store the sequence count for each integer, so I even left off that. I
  just wrote a loop that computes the sequence for every number.&lt;/p&gt;

&lt;p&gt;Imagine my surprise when it took only about 15 or 20 minutes to
  run!&lt;/p&gt;

&lt;p&gt;Sometimes brute force isn&amp;#39;t so bad. Sometimes the only way to know
  is to try it.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s my code:&lt;/p&gt; 

      &lt;div class="content" style="background-color:white;font-family:monospace;font-size:medium;"&gt;&lt;pre class="codeinput"&gt;&lt;span class="keyword"&gt;function&lt;/span&gt; euler14

max_count = 0;
max_nn = 1;
&lt;span class="keyword"&gt;for&lt;/span&gt; nn = 3:2:1000000

    new_nn = nn;
    count = 0;

    &lt;span class="keyword"&gt;while&lt;/span&gt; new_nn ~= 1
        &lt;span class="keyword"&gt;if&lt;/span&gt; mod(new_nn,2) == 0
            new_nn = new_nn/2;
        &lt;span class="keyword"&gt;else&lt;/span&gt;
            new_nn = new_nn*3+1;
        &lt;span class="keyword"&gt;end&lt;/span&gt;
        count = count + 1;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;if&lt;/span&gt; count &amp;gt; max_count
        max_count = count;
        max_nn = nn;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    disp([num2str(nn), &lt;span class="string"&gt;&amp;#39;: &amp;#39;&lt;/span&gt;, num2str(count)]);
&lt;span class="keyword"&gt;end&lt;/span&gt;

disp([&lt;span class="string"&gt;&amp;#39;Max: &amp;#39;&lt;/span&gt;, num2str(max_count), &lt;span class="string"&gt;&amp;#39; for nn = &amp;#39;&lt;/span&gt;, num2str(max_nn)]);

&lt;/pre&gt;
      &lt;/div&gt;
   


&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=4015" width="1" height="1"&gt;</description></item><item><title>On Bad Matlab</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/07/11/on-bad-matlab.aspx</link><pubDate>Fri, 11 Jul 2008 20:15:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3840</guid><dc:creator>amarsan</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3840</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/07/11/on-bad-matlab.aspx#comments</comments><description>&lt;p&gt;In the past year I&amp;#39;ve worked on two projects that involved piecing through someone else&amp;#39;s Matlab code, making sense of it, making it work, and figuring out how to link to it from a Windows desktop application. In both cases, it was clear that the Matlab had been writen by someone who does not normally spend a lot of time writing code. All of the tenets that we developers hold sacred had been broken: global variables abounded; code was copied over and over; large sections of code were commented out for no apparent reason; and in one of the projects, functions were dispensed with all together in favor of scripts, which often relied on variables that had been declared and set elsewhere in another script. In both cases the original author of the code was not available to explain how things were supposed to work, so fixing the code was a lengthy, tedious process.&lt;/p&gt;&lt;p&gt;It got me wondering...why do engineers write bad Matlab code? Most engineers have had at least one programming course in college, and presumably in that course they are taught how to organize code, declare functions, and pass arguements. Now granted, some people have a harder time than others in figuring out a good architecture for a project. But my observation is that engineers writing Matlab seem to have an aversion to declaring functions. They would rather copy and paste code 20 times than make that code into a function.&lt;/p&gt;&lt;p&gt;And then, as I was working on my most recent Matlab project, it occurred to me what the problem is. When I write C# or C++ code with Microsoft Visual Studio 2005 or 2008, I have IntelliSense, which is a godsend because I can never remember the order in which arguements are supposed to be passed to a function. Matlab does not have anything like IntelliSense, and it has lots and lots of functions, most of them overloaded or with a variable number of arguments. I can&amp;#39;t be the only scatter-brained engineer out there who has trouble keeping track of all of my options.&lt;/p&gt;&lt;p&gt;So this blog post is really a plea to The MathWorks folks - give us IntelliSense! &lt;/p&gt;&lt;p&gt;At a minimum, I&amp;#39;d be happy if there were a window that I could have open that would automatically show me the help file for a function as I type its name into an M-file. This would be similar to the context-sensitive help that&amp;#39;s available in Labview, another engineering tool that has lots and lots of functions that are hard (for me) to keep track of. &lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3840" width="1" height="1"&gt;</description></item><item><title>Software Licensing For Mechanical Engineers</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/06/30/software-licensing-for-mechanical-engineers.aspx</link><pubDate>Tue, 01 Jul 2008 02:38:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3763</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3763</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/06/30/software-licensing-for-mechanical-engineers.aspx#comments</comments><description>&lt;p&gt;Earlier this month &lt;a href="http://srtsolutions.com/blogs/billwagner/archive/2008/06/05/on-software-tools-and-pricing-models.aspx"&gt;Bill posted his thoughts on software development tools licensing&lt;/a&gt;. After several busy weeks of programming for three different clients, I&amp;#39;m finally catching up on reading, but I was too late to post a comment over on Bill&amp;#39;s blog, so I&amp;#39;m sharing my thoughts over here.&lt;/p&gt;

&lt;p&gt;I&amp;#39;m surprised that the more expensive, less frequently used software development tools don&amp;#39;t usually come with floating licenses. The big mechanical engineering packages that I&amp;#39;m familiar with (Nastran, ANSYS, Unigraphics, Catia, Ideas, etc.) can be installed on all users&amp;#39; machines, and then you set up a license server and &amp;quot;check out&amp;quot; a license when you use the tool. If all the licenses are checked out, you bug the guy at the next machine to wrap up his work and quit so you can do your thing. Or you work in the middle of the night, as we all did in graduate school at the end of the semester when everyone was wrapping up projects.  At a previous job, when things got particularly busy, we purchased a three month floating license for our finite element software to get us by without us killing one another. In addition, we had the option of checking out the license to a laptop so that we could work when off the company intranet.&lt;/p&gt;

&lt;p&gt;Just as Mike Woelmer has experienced with Compuware (see the comments on Bill&amp;#39;s post), a single floating license for Matlab or ANSYS or other packages is almost twice as much as getting a single seat, so you have to anticipate several occasional users for it to be worth it. Why haven&amp;#39;t the software development tool makers adopted this model, like the mechanical engineering tool makers have?&lt;/p&gt;
&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3763" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/licensing/default.aspx">licensing</category></item><item><title>Euler Problem #12</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/15/euler-problem-12.aspx</link><pubDate>Fri, 16 May 2008 03:14:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3186</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3186</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/15/euler-problem-12.aspx#comments</comments><description>&lt;p&gt;What is the first triangle number to have more than 500 divisors?&lt;/p&gt;

&lt;p&gt;Ok, this one kicked me in the pants, and it kicked my little Mac in the pants as well. I had to relearn how to get divisors quickly and efficiently, which meant seeking the wisdom of others on the Internet. But eventually I got a working program that didn&amp;#39;t take all night to run.&lt;/p&gt;

&lt;div style="background-color:white;font-family:monospace;font-size:medium;"&gt;
function euler12&lt;br /&gt;
&lt;br /&gt;
nn = 3;&lt;br /&gt;
while 1&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;  if mod(nn,2) == 0&lt;br /&gt;
&amp;nbsp; &amp;nbsp;     ndivisors = get_divisors(nn/2)*get_divisors(nn+1);&lt;br /&gt;
 &amp;nbsp;  else&lt;br /&gt;
 &amp;nbsp; &amp;nbsp;    ndivisors = get_divisors((nn+1)/2)*get_divisors(nn);&lt;br /&gt;
 &amp;nbsp;  end&lt;br /&gt;
  &lt;br /&gt;
&amp;nbsp;   if ndivisors &amp;gt; 500&lt;br /&gt;
&amp;nbsp; &amp;nbsp;     break;&lt;br /&gt;
&amp;nbsp;   end&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;   nn = nn+1;&lt;br /&gt;
  &lt;br /&gt;
end&lt;br /&gt;
nn&lt;br /&gt;
triangle_num = nn/2*(nn+1)&lt;br /&gt;
ndivisors&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function ndivisors = get_divisors(nn)&lt;br /&gt;
&lt;br /&gt;
ndivisors = 2; % 1 and nn&lt;br /&gt;
&lt;br /&gt;
% Get the prime factors&lt;br /&gt;
vv = factor(nn);&lt;br /&gt;
nvv = length(vv);&lt;br /&gt;
ndivisors = ndivisors + length(unique(vv));&lt;br /&gt;
&lt;br /&gt;
% Now get all the combinations of the prime factors&lt;br /&gt;
for ii = 2:(nvv-1)&lt;br /&gt;
&amp;nbsp;  aa = nchoosek(vv, ii);&lt;br /&gt;
&amp;nbsp;  bb = unique(aa, &amp;#39;rows&amp;#39;);&lt;br /&gt;
&amp;nbsp;  [mm, nn] = size(bb);&lt;br /&gt;
&amp;nbsp;  ndivisors = ndivisors + mm;&lt;br /&gt;
end&lt;br /&gt;
&lt;/div&gt;

&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3186" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/Euler+problems/default.aspx">Euler problems</category></item><item><title>7 Billion People</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/15/7-billion-people.aspx</link><pubDate>Fri, 16 May 2008 02:42:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3184</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3184</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/15/7-billion-people.aspx#comments</comments><description>&lt;p&gt;For those of you out there doing e-commerce, check out &lt;a href="http://www.7bpeople.com"&gt;7 Billion People&lt;/a&gt;. They specialize in a more personalized e-commerce experience, and they have a hard-working, experienced team behind them. Take a look at their marketing videos:&lt;/p&gt;

&lt;a href="http://www.youtube.com/watch?v=-5xahNGE-lU"&gt;http://www.youtube.com/watch?v=-5xahNGE-lU&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.youtube.com/watch?v=724s9rmfk0g"&gt;http://www.youtube.com/watch?v=724s9rmfk0g&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.youtube.com/watch?v=Z5ZVwf9YXsQ"&gt;http://www.youtube.com/watch?v=Z5ZVwf9YXsQ&lt;/a&gt;&lt;br /&gt;

&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3184" width="1" height="1"&gt;</description></item><item><title>ZedGraph for 2D Graphics Apps</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/12/zedgraph-for-2d-graphics-apps.aspx</link><pubDate>Tue, 13 May 2008 02:58:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3152</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3152</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/12/zedgraph-for-2d-graphics-apps.aspx#comments</comments><description>&lt;p&gt;A customer asked me to write a very small application that performs some operations on 2D parametric curves. While their code base for their primary software is C++ Win32, I thought this would be a great opportunity to show off C#.Net to them, but that meant coming up with a basic graphics library that would allow me to display (x,y) data as a spline curve or polyline, toggle control points display, and perhaps do some zooming and panning. While the customer is willing to pay for a 3rd party library if the price and feature set are right, I thought I&amp;#39;d look into open source libraries to see what&amp;#39;s out there. That&amp;#39;s how I discovered &lt;a href="http://zedgraph.org"&gt;ZedGraph&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;ZedGraph is billed as a charting library (it can do X-Y, bar and pie charts), but it&amp;#39;s X-Y graphing functionality is solid enough to make it appropriate for a basic 2D graphics app. One of my big concerns was the ability to exert control over the scaling along the X and Y axes. I want to be able to force a 1:1 aspect ratio between the X and Y scaling so that the curves that I&amp;#39;m plotting aren&amp;#39;t distorted. ZedGraph allows me to set the minimum and maximum values along an axis, but then it takes care of the rest. If I&amp;#39;m showing the axes and a grid behind my curves, ZedGraph automatically figures out where to put tick marks and major and/or minor grid lines.&lt;/p&gt;&lt;p&gt;Naturally I want to be able to control the color and style of the plotted curves, the type of symbol used to represent curve control points, the axes titles, the chart title,&amp;nbsp;  the gridline color, etc. This is very intuitive in ZedGraph, because the architecture of the library and the names of the properties and methods make sense.&lt;/p&gt;&lt;p&gt;Zooming and panning functionality is available by default in the .Net UserControl that comes along with the library. Best of all, the functionality mimics that of many well-known graphics software apps (like ANSYS and Unigraphics).&lt;/p&gt;&lt;p&gt;Finally, the documentation and examples available at the ZedGraph website are decent enough to get the novice beyond setting up a basic graph. I was able to follow the examples to get the look, feel, and functionality my customer required in place within a few hours.&lt;/p&gt;&lt;p&gt;I haven&amp;#39;t played around with the library long enough to discover its limitations - I&amp;#39;m sure they&amp;#39;re there! But for a very basic 2D graphics app, this library is an excellent choice.&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3152" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/2D+graphics/default.aspx">2D graphics</category></item><item><title>Euler Problem #11</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/07/euler-problem-11.aspx</link><pubDate>Thu, 08 May 2008 01:59:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3090</guid><dc:creator>amarsan</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=3090</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/05/07/euler-problem-11.aspx#comments</comments><description>&lt;p&gt;In this problem we are searching for the largest product of four consecutive numbers in any row, column, or diagonal of a given 20 by 20 matrix. This is a nice sort of problem to do in Matlab because it is so well-suited for working with matrices. I wrote a separate function for scooting along a vector looking for the greatest product. Matlab has a built-in diag function that gives you a matrix diagonal as a vector. The 0th diagonal is the central one, the negative indices give you the lower triangle, and positive indices give you the upper triangle. Without further ado...&lt;/p&gt;

&lt;div style="background-color:white;font-family:monospace;font-size:medium;"&gt;
function euler11&lt;br /&gt;&lt;br /&gt;
global nn;&lt;br /&gt;
global mm;&lt;br /&gt;
global max_product;&lt;br /&gt;
&lt;br /&gt;
grid = [08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08;&lt;br /&gt;
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00;&lt;br /&gt;
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65;&lt;br /&gt;
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91;&lt;br /&gt;
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80;&lt;br /&gt;
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50;&lt;br /&gt;
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70;&lt;br /&gt;
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21;&lt;br /&gt;
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72;&lt;br /&gt;
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95;&lt;br /&gt;
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92;&lt;br /&gt;
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57;&lt;br /&gt;
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58;&lt;br /&gt;
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40;&lt;br /&gt;
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66;&lt;br /&gt;
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69;&lt;br /&gt;
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36;&lt;br /&gt;
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16;&lt;br /&gt;
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54;&lt;br /&gt;
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48];&lt;br /&gt;
&lt;br /&gt;
nn = 20;&lt;br /&gt;
mm = 4;&lt;br /&gt;
max_product = 0;&lt;br /&gt;
&lt;br /&gt;
% Go through rows&lt;br /&gt;
for ii = 1:nn&lt;br /&gt;
&amp;nbsp;  get_product(grid(ii,:));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% Go through columns&lt;br /&gt;
for ii = 1:nn&lt;br /&gt;
&amp;nbsp;  get_product(grid(:,ii));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% Go through upper left lower right diagonals&lt;br /&gt;
for ii = (-nn+mm):(nn-mm)&lt;br /&gt;
&amp;nbsp;  clear vv;&lt;br /&gt;
&amp;nbsp;  vv = diag(grid, ii);&lt;br /&gt;
&amp;nbsp;  get_product(vv);&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
% Go through lower left upper right diagonals&lt;br /&gt;
grid = flipud(grid);&lt;br /&gt;
for ii = (-nn+mm-1):(nn-mm+1)&lt;br /&gt;
&amp;nbsp;  clear vv;&lt;br /&gt;
&amp;nbsp;  vv = diag(grid, ii);&lt;br /&gt;
&amp;nbsp;  get_product(vv);&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
max_product&lt;br /&gt;
&lt;br /&gt;
function get_product(vv)&lt;br /&gt;
&lt;br /&gt;
global mm;&lt;br /&gt;
global max_product;&lt;br /&gt;
&lt;br /&gt;
for ii = 1:(length(vv)-mm+1)&lt;br /&gt;
&amp;nbsp;  product = vv(ii);&lt;br /&gt;
&amp;nbsp;  for jj = 1:mm-1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    product = product * vv(ii+jj);&lt;br /&gt;
&amp;nbsp;  end&lt;br /&gt;
&amp;nbsp;  if product &amp;gt; max_product&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    max_product = product;&lt;br /&gt;
&amp;nbsp;   end&lt;br /&gt;
end&lt;br /&gt;
&lt;/div&gt;


&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3090" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/Euler+problems/default.aspx">Euler problems</category></item><item><title>Euler Problem #10</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/24/euler-problem-10.aspx</link><pubDate>Fri, 25 Apr 2008 02:45:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2962</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=2962</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/24/euler-problem-10.aspx#comments</comments><description>&lt;p&gt;Sum the primes below 2,000,000.&lt;/p&gt;

&lt;p&gt;This is the sort of problem that Matlab handles very well. The code looks pretty, and I got an answer right quick. Now if I type fast, I&amp;#39;ll get my code posted before my battery drains and Baseball Tonight ends.&lt;/p&gt;

&lt;div style="background-color:white;font-family:monospace;font-size:medium;"&gt;
function euler10&lt;br /&gt;
sum(primes(2000000),2)&lt;br /&gt;
&lt;/div&gt;
 &lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2962" width="1" height="1"&gt;</description></item><item><title>Euler Problem #9</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/19/euler-problem-9.aspx</link><pubDate>Sun, 20 Apr 2008 02:54:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2886</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=2886</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/19/euler-problem-9.aspx#comments</comments><description>&lt;p&gt;Now I&amp;#39;m really challenging myself - can I get a solution to a problem without the fan kicking on? This one took a few tries, but I finally found a good algorithm.&lt;/p&gt;

&lt;p&gt;We&amp;#39;re asked to find the pythagorean triplet (a, b, c) that sums to 1000. I made an educated guess as to a good range for c, to limit the search space.&lt;/p&gt; 

&lt;div style="background-color:white;font-family:monospace;font-size:medium;"&gt;
function euler9&lt;br /&gt;
&lt;br /&gt;
&lt;p style="color:red;"&gt;% a &amp;lt; b &amp;lt; c&lt;/p&gt;

for cc = 500:-1:300&lt;br /&gt;
&amp;nbsp;  aa_bb = 1000-cc;&lt;br /&gt;
&amp;nbsp;  for bb = aa_bb-1:-1:1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    aa = aa_bb - bb;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;      if (aa*aa + bb*bb) == cc*cc&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;        disp([num2str(aa), &amp;quot;, &amp;quot;, num2str(bb), &amp;quot;, &amp;quot;, num2str(cc)]);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;        aa*bb*cc&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;        return;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;      end&lt;br /&gt;
&amp;nbsp;    end&lt;br /&gt;
end&lt;br /&gt;
&lt;/div&gt;

&lt;p&gt;This problem reminded me of &lt;a href="http://mae.ucdavis.edu/~farouki/"&gt;Professor Rida Farouki&lt;/a&gt;, who teaches his students the value of conserving computations in CAD systems, and who has written several papers on Pythagorean hodograph curves.&lt;/p&gt;
&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2886" width="1" height="1"&gt;</description></item><item><title>Euler Problem #8</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/19/euler-problem-8.aspx</link><pubDate>Sun, 20 Apr 2008 01:59:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2885</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=2885</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/19/euler-problem-8.aspx#comments</comments><description>&lt;p&gt;This problem asks us to find the greatest product of five consecutive digits in a given 1000 digit number. First the 1000 digit number has to be stored as a string. I wanted to make sure that each digit of the number was converted from a string to an int only once, which complicates the code a bit. Second, it occurred to me that if I&amp;#39;m looking for the greatest product, I&amp;#39;m also looking for the greatest sum. This allows me to save some further compute time.&lt;/p&gt;

&lt;div style="background-color:white;font-family:monospace;font-size:medium;"&gt;
function euler8&lt;br /&gt;
&lt;br /&gt;
big_num = &amp;quot;73167176531330624919225119674426574742355349194934...&lt;br /&gt;
96983520312774506326239578318016984801869478851843...&lt;br /&gt;
85861560789112949495459501737958331952853208805511...&lt;br /&gt;
12540698747158523863050715693290963295227443043557...&lt;br /&gt;
66896648950445244523161731856403098711121722383113...&lt;br /&gt;
62229893423380308135336276614282806444486645238749...&lt;br /&gt;
30358907296290491560440772390713810515859307960866...&lt;br /&gt;
70172427121883998797908792274921901699720888093776...&lt;br /&gt;
65727333001053367881220235421809751254540594752243...&lt;br /&gt;
52584907711670556013604839586446706324415722155397...&lt;br /&gt;
53697817977846174064955149290862569321978468622482...&lt;br /&gt;
83972241375657056057490261407972968652414535100474...&lt;br /&gt;
82166370484403199890008895243450658541227588666881...&lt;br /&gt;
16427171479924442928230863465674813919123162824586...&lt;br /&gt;
17866458359124566529476545682848912883142607690042...&lt;br /&gt;
24219022671055626321111109370544217506941658960408...&lt;br /&gt;
07198403850962455444362981230987879927244284909188...&lt;br /&gt;
84580156166097919133875499200524063689912560717606...&lt;br /&gt;
05886116467109405077541002256983155200055935729725...&lt;br /&gt;
71636269561882670428252483600823257530420752963450&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
sum = 0;&lt;br /&gt;
product = 1;&lt;br /&gt;
for ii = 1:5&lt;br /&gt;
&amp;nbsp;  digit(ii) = str2num(big_num(ii));&lt;br /&gt;
&amp;nbsp;  sum = sum + digit(ii);&lt;br /&gt;
&amp;nbsp;  product = product*digit(ii);&lt;br /&gt;
end&lt;br /&gt;
biggest_product = product;&lt;br /&gt;
biggest_sum = sum;&lt;br /&gt;
&lt;br /&gt;
for ii = 2:996&lt;br /&gt;
&amp;nbsp;  sum = sum - digit(1);&lt;br /&gt;
&amp;nbsp;  for jj = 1:4&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    digit(jj) = digit(jj+1);&lt;br /&gt;
&amp;nbsp;  end&lt;br /&gt;
&amp;nbsp;  digit(5) = str2num(big_num(ii+4));&lt;br /&gt;
&amp;nbsp;  sum = sum + digit(5);&lt;br /&gt;
  &lt;br /&gt;
&amp;nbsp;  if sum &amp;gt; biggest_sum&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    biggest_sum = sum;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;    biggest_product = digit(1)*digit(2)*digit(3)*digit(4)*digit(5);&lt;br /&gt;
&amp;nbsp;  end&lt;br /&gt;
end&lt;br /&gt;
biggest_product&lt;br /&gt;
&lt;/div&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2885" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/Euler+problems/default.aspx">Euler problems</category></item><item><title>Euler Problem #7</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/17/euler-problem-7.aspx</link><pubDate>Fri, 18 Apr 2008 01:46:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2851</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=2851</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/17/euler-problem-7.aspx#comments</comments><description>&lt;p&gt;Is it cheating to use the Matlab built in isprime function? It saves me from going to Google to find a prime number algorithm.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s my code:&lt;/p&gt;

&lt;div style="background-color:white;font-family:monospace;"&gt;
function euler7&lt;br /&gt;
&lt;br /&gt;

ii = 0;&lt;br /&gt;
jj = 2;&lt;br /&gt;
while 1&lt;br /&gt;
&amp;nbsp;  if isprime(jj)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;  ii=ii+1;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;   if ii == 10001&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;      break;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;   end&lt;br /&gt;
&amp;nbsp; end&lt;br /&gt;
&amp;nbsp; jj=jj+1;&lt;br /&gt;
end&lt;br /&gt;
jj&lt;br /&gt;
&lt;/div&gt;

&lt;p&gt;This completed in a reasonable amount of time on my slow Mac, but later I read about only testing odd numbers, and I wish I had thought of that.&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2851" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/annemarsan/archive/tags/Euler+problems/default.aspx">Euler problems</category></item><item><title>Euler Problems - #6</title><link>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/10/euler-problems-6.aspx</link><pubDate>Thu, 10 Apr 2008 04:41:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2712</guid><dc:creator>amarsan</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/annemarsan/rsscomments.aspx?PostID=2712</wfw:commentRss><comments>http://srtsolutions.com/blogs/annemarsan/archive/2008/04/10/euler-problems-6.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m skipping around a bit, but this one shows off Matlab a little better.&lt;/p&gt;

&lt;pre&gt;&lt;p&gt;function euler6&lt;br /&gt;&lt;br /&gt;nat_nums = 1:1:100&lt;br /&gt;sum(nat_nums,2)^2 - sum(nat_nums .* nat_nums, 2) &lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/pre&gt;

&lt;p&gt;.* is the array multiplcation operator (element-wise)&lt;/p&gt;&lt;p&gt;Arrays in Matlab are really 1xn matrices, so in the sum functions, the second argument indicates that I should sum across columns.&lt;/p&gt;&lt;p&gt;Matlab rocks.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2712" width="1" height="1"&gt;</description></item></channel></rss>