<?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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Programming with M-files: A Modeling Example Using For Loops</name>
  <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <md:version xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">1.4</md:version>
  <md:created xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">2006/01/26 16:06:42 US/Central</md:created>
  <md:revised xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">2006/08/21 19:46:05.445 GMT-5</md:revised>
  <md:authorlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
      <md:author xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="morrell">
      <md:firstname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Darryl </md:firstname>
      
      <md:surname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Morrell</md:surname>
      <md:email xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    <md:maintainer xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="morrell">
      <md:firstname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Darryl </md:firstname>
      
      <md:surname xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Morrell</md:surname>
      <md:email xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Engineering modeling</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">for loop</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">M-File</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">MathScript</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">MATLAB</md:keyword>
    <md:keyword xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Octave</md:keyword>
  </md:keywordlist>

  <md:abstract xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">This is an example of using for loops in m-files to solve a simple engineering modeling problem.</md:abstract>
</metadata>
  <content xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SecProbIntro">
	  <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">A Modeling Problem: Counting Ping Pong Balls</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">This problem, along with the strategy for computing the lower bound on the number of ping-pong balls, is adapted from <cite xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" src="#HTMI"> (Starfield 1994). </cite></note></para>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaP1">A lower bound for this problem is found as follows:
<list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="bulleted" id="VariableList">
<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ExByHand">
<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ProbByHand">
<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">The interactive approach</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaProbByHand">
You are given the following values:
<list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="bulleted" id="ListByHandVals">
<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> d </m:ci>
        <m:cn> 1.54 in </m:cn>
    </m:apply>
  </m:math>
</item>
<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <m:math>
    <m:apply>
      <m:eq/>
        <m:ci> b </m:ci>
        <m:cn> 8 in </m:cn>
    </m:apply>
  </m:math>
</item>
<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
  <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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolByHand">The following shows the commands typed at the &gt;&gt; prompt and the output produced:
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ExMFile">
<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ProbFMile">
<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Using an M-File</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaMFile">
Create an m-file to solve <cnxn xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" target="ProbByHand"/>.
</para>
</problem>
<solution xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolMFile">We created the following file named <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">PingPong.m</code>:
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="block">
&gt;&gt; PingPong

nl =

  192.6796

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

<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ExFirstFor">
<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ProbFirstFor">
<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Use a for loop</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaFirstFor">
Add a for loop to your m-file from <cnxn xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolFirstFor">
This solution is by BrieAnne Davis.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ExSecFor">
<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ProbSecFor">
<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">Can you still use a for loop?</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaSecFor">
Modify your m-file from <cnxn xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" target="ProbFirstFor"/> to <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolSecFor1">
This solution is by Wade Stevens.  Note that it uses the command <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">hold on</code>  to plot  each point individually in the for loop.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" target="FigStevensP3"/>.
<figure xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="FigStevensP3">
  <media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="application/postscript" src="StevensPlot.eps">
    <media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="image/png" src="StevensPlot.png"/>
  </media>
  <caption xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolSecFor2">
This  different solution is by Christopher Embrey. It uses the index variable <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">j</code> to step through the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">dv</code> array to compute elements of the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">nlv</code> array;  the complete <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">nlv</code> array is computed, and then plotted outside the for loop.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" target="FigEmbryP3"/>.
<figure xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="FigEmbryP3">
  <media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="application/postscript" src="EmbreyPlot.eps">
    <media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" type="image/png" src="EmbreyPlot.png"/>
  </media>
  <caption xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
    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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolSecFor3">
And finally, this solution by Travis Venson uses vector computations to perform the computation without a for loop.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ExThirdFor">
<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ProbThirdFor">
<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">More loops?</name>
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="ParaThirdFor">
Modify your m-file from <cnxn xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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 xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">
<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="SolThirdFor">
This solution is by AJ Smith.  The height, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">h</code>, ranges from 12 to 15 and the base, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/">b</code>, ranges from 8 to 12.
<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" 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>
