<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Programming with M-files: A Modeling Example Using For Loops</name>
  <metadata>
  <md:version>1.4</md:version>
  <md:created>2006/01/26 16:06:42 US/Central</md:created>
  <md:revised>2006/08/21 19:46:05.445 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Engineering modeling</md:keyword>
    <md:keyword>for loop</md:keyword>
    <md:keyword>M-File</md:keyword>
    <md:keyword>MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
  </md:keywordlist>

  <md:abstract>This is an example of using for loops in m-files to solve a simple engineering modeling problem.</md:abstract>
</metadata>
  <content>
<section id="SecProbIntro">
	  <name>A Modeling Problem: Counting Ping Pong Balls</name>
<para id="ParaProbIntro">
Suppose you have a cylinder of height
  <m:math>
    <m:ci> h </m:ci>
  </m:math>
with base diameter 
  <m:math>
    <m:ci> b </m:ci>
  </m:math>
(perhaps an empty pretzel jar), and you wish to know how many ping-pong balls of diameter
  <m:math>
    <m:ci> d </m:ci>
  </m:math>
 have been placed inside the cylinder.  How could you determine this?
<note>This problem, along with the strategy for computing the lower bound on the number of ping-pong balls, is adapted from <cite src="#HTMI"> (Starfield 1994). </cite></note></para>
<para id="ParaP1">A lower bound for this problem is found as follows:
<list type="bulleted" id="VariableList">
<item>
  <m:math>
    <m:ci>
      <m:msub>
        <m:mi>N</m:mi>
        <m:mi>L</m:mi>
      </m:msub>
    </m:ci>
  </m:math>
-Lower bound on the number of balls that fit into the cylinder.
</item>
<item>
  <m:math>
    <m:ci>
      <m:msub>
        <m:mi>V</m:mi>
        <m:mi>cyl</m:mi>
      </m:msub>
    </m:ci>
  </m:math>
-The volume of the cylinder.
  <equation id="EqCylVol"><m:math>
    <m:apply>
      <m:eq/>
        <m:ci>
          <m:msub>
            <m:mi>V</m:mi>
            <m:mi>cyl</m:mi>
          </m:msub>
        </m:ci>
      <m:apply>
        <m:times/>
          <m:ci>
            h
          </m:ci>
          <m:ci>
            π
          </m:ci>
          <m:apply>
            <m:power/>
              <m:apply>
                <m:divide/>
                  <m:ci> b </m:ci>
                  <m:cn> 2 </m:cn>
              </m:apply>
              <m:cn> 2 </m:cn>
          </m:apply>
      </m:apply>
    </m:apply>
  </m:math>
  </equation>  
</item>
<item>
  <m:math>
    <m:ci>
      <m:msub>
        <m:mi>V</m:mi>
        <m:mi>cube</m:mi>
      </m:msub>
    </m:ci>
  </m:math>
-The volume of a cube that encloses a single ball.
  <equation id="EqCubeVol"><m:math>
    <m:apply>
      <m:eq/>
        <m:ci>
          <m:msub>
            <m:mi>V</m:mi>
            <m:mi>cube</m:mi>
          </m:msub>
        </m:ci>
      <m:apply>
        <m:power/>
          <m:ci>
            d
          </m:ci>
          <m:cn>
            3
          </m:cn>
      </m:apply>
    </m:apply>
  </m:math>
  </equation>  

</item></list>
The lower bound is found by dividing the volume of the cylinder by the volume of the cube enclosing a single ball.
<equation id="EqLowerBound"><m:math>
  <m:apply>
    <m:eq/>
      <m:ci>
        <m:msub>
          <m:mi>N</m:mi>
          <m:mi>L</m:mi>
        </m:msub>
      </m:ci>
    <m:apply>
      <m:divide/>
        <m:ci>
          <m:msub>
            <m:mi>V</m:mi>
            <m:mi>cyl</m:mi>
          </m:msub>
        </m:ci>
        <m:ci>
          <m:msub>
            <m:mi>V</m:mi>
            <m:mi>cube</m:mi>
          </m:msub>
        </m:ci>
    </m:apply>
  </m:apply>
</m:math>
</equation>  
</para>
<exercise id="ExByHand">
<problem id="ProbByHand">
<name>The interactive approach</name>
<para id="ParaProbByHand">
You are given the following values:
<list type="bulleted" id="ListByHandVals">
<item>
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> d </m:ci>
        <m:cn> 1.54 in </m:cn>
    </m:apply>
  </m:math>
</item>
<item>
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> b </m:ci>
        <m:cn> 8 in </m:cn>
    </m:apply>
  </m:math>
</item>
<item>
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> h </m:ci>
        <m:cn> 14 in </m:cn>
    </m:apply>
  </m:math>
</item>
</list>
Type commands at the command line prompt to compute
  <m:math>
    <m:ci>
      <m:msub>
        <m:mi>N</m:mi>
        <m:mi>L</m:mi>
      </m:msub>
    </m:ci>
  </m:math>
.
</para>
</problem>
<solution>
<para id="SolByHand">The following shows the commands typed at the &gt;&gt; prompt and the output produced:
<code type="block">
&gt;&gt; d = 1.54

d =

    1.5400

&gt;&gt; b = 8

b =

     8

&gt;&gt; h = 14

h =

    14

&gt;&gt; vcyl = h*pi*(b/2)^2

vcyl =

  703.7168

&gt;&gt; vcube = d^3

vcube =

    3.6523

&gt;&gt; nl = vcyl/vcube

nl =

  192.6796

</code></para>
</solution>
</exercise>

<exercise id="ExMFile">
<problem id="ProbFMile">
<name>Using an M-File</name>
<para id="ParaMFile">
Create an m-file to solve <cnxn target="ProbByHand"/>.
</para>
</problem>
<solution>
<para id="SolMFile">We created the following file named <code>PingPong.m</code>:
<code type="block">
% PingPong.m - computes a lower bound on the number of 
%   ping pong balls that fit into a cylinder
% Note that most lines end with a ";", so they don't print
%   intermediate results

d = 1.54;
h = 14;
b = 8;
vcyl = h*pi*(b/2)^2;
vcube = d^3;
nl = vcyl/vcube
</code>
When run from the command line, this program produces the following output:
<code type="block">
&gt;&gt; PingPong

nl =

  192.6796

</code></para>
</solution>
</exercise> 

<para id="ParaRestOfStory">
To complicate your problem, suppose that you have not been given values for 
<m:math> <m:ci> d </m:ci> </m:math>,
<m:math> <m:ci> b </m:ci> </m:math>, and
<m:math> <m:ci> h </m:ci> </m:math>. Instead you are required to estimate the number of ping pong balls for many different possible  combinations of these variables  (perhaps 50 or more combinations).  How can you automate this computation?
</para>

<para id="element-381">One way to automate the computation of
  <m:math>
    <m:ci>
      <m:msub>
        <m:mi>N</m:mi>
        <m:mi>L</m:mi>
      </m:msub>
    </m:ci>
  </m:math>
for many different combinations of parameter values is to use a for loop. (Read 
<cnxn document="m13258">Programming with M-Files: For Loops</cnxn>
if you are not familiar with the use of for loops.) The following problems ask you to develop several different ways that for loops can be used to automate these computations.</para><exercise id="ExFirstFor">
<problem id="ProbFirstFor">
<name>Use a for loop</name>
<para id="ParaFirstFor">
Add a for loop to your m-file from <cnxn target="ProbFMile"/> to compute   
<m:math>
<m:ci>
  <m:msub>
    <m:mi>N</m:mi>
    <m:mi>L</m:mi>
  </m:msub>
</m:ci>
</m:math>
for 
  <m:math>
    <m:apply>
      <m:eq/>
        <m:mi> b </m:mi>
        <m:cn> 8 in </m:cn>
    </m:apply>
  </m:math>,
<m:math>
<m:apply>
  <m:eq/>
    <m:ci> h </m:ci>
    <m:cn> 14 in </m:cn>
</m:apply>
</m:math>,
and values of <m:math> <m:ci> d </m:ci> </m:math>
ranging from 1.0 in to 2.0 in.
</para>
</problem>
<solution>
<para id="SolFirstFor">
This solution is by BrieAnne Davis.
<code type="block">
for d=1.0:.05:2.0
    b=8;
    h=14;
    vcyl=h*pi*(b/2)^2
    vcube=d^3
    nl=vcyl/vcube
end
</code>

</para>
</solution>
</exercise> 
<exercise id="ExSecFor">
<problem id="ProbSecFor">
<name>Can you still use a for loop?</name>
<para id="ParaSecFor">
Modify your m-file from <cnxn target="ProbFirstFor"/> to <emphasis>plot</emphasis>    
<m:math>
<m:ci>
  <m:msub>
    <m:mi>N</m:mi>
    <m:mi>L</m:mi>
  </m:msub>
</m:ci>
</m:math>
as a function of <m:math> <m:ci> d </m:ci> </m:math>
for 
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> b </m:ci>
        <m:cn> 8 in </m:cn>
    </m:apply>
  </m:math> and
<m:math>
<m:apply>
  <m:eq/>
    <m:ci> h </m:ci>
    <m:cn> 14 in </m:cn>
</m:apply>
</m:math>.
</para>
</problem>
<solution>
<para id="SolSecFor1">
This solution is by Wade Stevens.  Note that it uses the command <code>hold on</code>  to plot  each point individually in the for loop.
<code type="block">
clear all
hold on
for d=1.0:0.1:2.0;
    b=8;
    h=14;
    C=h*pi*(b/2)^2; %volume of cylinder
    c=d^3; %volume of cube
    N=C/c; %Lower bound
    floor(N)
    plot (d,N,'g*')
end
</code>
This solution creates the plot in <cnxn target="FigStevensP3"/>.
<figure id="FigStevensP3">
  <media type="application/postscript" src="StevensPlot.eps">
    <media type="image/png" src="StevensPlot.png"/>
  </media>
  <caption>
    Plot of <m:math>
<m:ci>
  <m:msub>
    <m:mi>N</m:mi>
    <m:mi>L</m:mi>
  </m:msub>
</m:ci>
</m:math>
as a function of <m:math> <m:ci> d </m:ci> </m:math>; each point plotted individually.
  </caption>
</figure>

</para>

<para id="SolSecFor2">
This  different solution is by Christopher Embrey. It uses the index variable <code>j</code> to step through the <code>dv</code> array to compute elements of the <code>nlv</code> array;  the complete <code>nlv</code> array is computed, and then plotted outside the for loop.
<code type="block">
clear
dv=1.0:.05:2.0;
 
[junk,dvsize] = size(dv)
for j=1:dvsize
    d=dv(j)
    b=8; %in
    h=14; %in
    vcyl=h*pi*(b/2)^2;
    vcube=d^3;
    nl=vcyl/vcube; 
    nlv(j)=nl;
end
plot (dv,nlv)
</code>
This solution creates the plot in <cnxn target="FigEmbryP3"/>.
<figure id="FigEmbryP3">
  <media type="application/postscript" src="EmbreyPlot.eps">
    <media type="image/png" src="EmbreyPlot.png"/>
  </media>
  <caption>
    Plot of <m:math>
<m:ci>
  <m:msub>
    <m:mi>N</m:mi>
    <m:mi>L</m:mi>
  </m:msub>
</m:ci>
</m:math>
as a function of <m:math> <m:ci> d </m:ci> </m:math>; points plotted as vectors.
  </caption>
</figure>

</para>

<para id="SolSecFor3">
And finally, this solution by Travis Venson uses vector computations to perform the computation without a for loop.
<code type="block">
%creates a vector for diameter
dv=1:.02:2;
b=5.5;
h=12;
 
%computes volume of cylinder
vcyl=h*pi*(b/2)^2;
 
%computes volume of cube
vcube=dv.^3;
 
%computes lower bound
lowerboundv=vcyl./vcube;
 
%plots results
plot(dv,lowerboundv)
</code>
</para>
</solution>
</exercise> 
<exercise id="ExThirdFor">
<problem id="ProbThirdFor">
<name>More loops?</name>
<para id="ParaThirdFor">
Modify your m-file from <cnxn target="ProbFirstFor"/> to compute    
<m:math>
<m:ci>
  <m:msub>
    <m:mi>N</m:mi>
    <m:mi>L</m:mi>
  </m:msub>
</m:ci>
</m:math>
for 
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> d </m:ci>
        <m:cn> 1.54 in </m:cn>
    </m:apply>
  </m:math>
and various values of <m:math> <m:ci> b </m:ci> </m:math> and <m:math> <m:ci> h </m:ci> </m:math>.
</para>
</problem>
<solution>
<para id="SolThirdFor">
This solution is by AJ Smith.  The height, <code>h</code>, ranges from 12 to 15 and the base, <code>b</code>, ranges from 8 to 12.
<code type="block">
for h=12:15; %ranges of height
    for b=8:12; %ranges of the base
        d=1.54; %diameter of ping pong ball.
        Vcyl=h*pi*(b/2)^2; %Volume of cylinder
        Vcube=d^3; %volume of a cube that encloses a single ball
        Nl=Vcyl/Vcube %lower bound on the number of balls that fit in the cylinder
    end
end
</code>
</para>
</solution>
</exercise> 
</section>

  </content>
<bib:file>
  <bib:entry id="HTMI">
    <bib:book>
      <bib:author>Anthony M. Starfield; Karl A. Smith; Andrew L. Bleloch</bib:author>
      <bib:title>How To Model It: Problem Solving for the Computer Age</bib:title>
      <bib:publisher>Interaction Book Company</bib:publisher>
      <bib:year>1994</bib:year>
      <bib:address>Edina, MN</bib:address>
    </bib:book>
  </bib:entry>
</bib:file>

</document>
