# Connexions

You are here: Home » Content » Programming in LabVIEW MathScript-A Modeling Example Using For Loops

### Lenses

What is a lens?

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

#### Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
• NSF Partnership

This module is included inLens: NSF Partnership in Signal Processing
By: Sidney BurrusAs a part of collection: "Introduction to LabVIEW MathScript"

Click the "NSF Partnership" link to see all content affiliated with them.

Click the tag icon to display tags associated with this content.

• National Instruments

This module is included in aLens by: National InstrumentsAs a part of collection: "Introduction to LabVIEW MathScript"

"This course provides a brief introduction to LabVIEW MathScript, the textual math componenet of LabVIEW. The modules for this course include typical syntax and programming methods commonly used […]"

Click the "National Instruments" link to see all content affiliated with them.

Click the tag icon to display tags associated with this content.

#### Also in these lenses

• Lens for Engineering

This module is included inLens: Lens for Engineering
By: Sidney Burrus

Click the "Lens for Engineering" link to see all content selected in this lens.

• NI Signal Processing

This module is included inLens: Digital Signal Processing with NI LabVIEW and the National Instruments Platform
By: Sam ShearmanAs a part of collection: "Introduction to LabVIEW MathScript"

"Tutorial / Introduction to LabVIEW MathScript, a text-based component of National Instruments LabVIEW that allows you to run your .m file scripts in LabVIEW Virtual Instruments."

Click the "NI Signal Processing" link to see all content selected in this lens.

Click the tag icon to display tags associated with this content.

### Recently Viewed

This feature requires Javascript to be enabled.

### Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

# Programming in LabVIEW MathScript-A Modeling Example Using For Loops

Summary: This is an example of using LabVIEW MathScript to solve a simple engineering modeling problem.

## A Modeling Problem: Counting Ping Pong Balls

Suppose you have a cylinder of height h h with base diameter b b (perhaps an empty pretzel jar), and you wish to know how many ping-pong balls of diameter d d 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 (Starfield 1994).

A lower bound for this problem is found as follows:

• N L N L -Lower bound on the number of balls that fit into the cylinder.
• V cyl V cyl -The volume of the cylinder.
V cyl = h π b 2 2 V cyl h π b 2 2
(1)
• V cube V cube -The volume of a cube that encloses a single ball.
V cube = d 3 V cube d 3
(2)
The lower bound is found by dividing the volume of the cylinder by the volume of the cube enclosing a single ball.
N L = V cyl V cube N L V cyl V cube
(3)

### Exercise 1

#### The interactive approach

You are given the following values:

• d = 1.54 in d 1.54 in
• b = 8 in b 8 in
• h = 14 in h 14 in
Use LABVIEW MATHSCRIPT interactively (that is, type commands at the command line prompt) to compute N L N L .

#### Solution

The following shows commands typed to LABVIEW MATHSCRIPT (at the >> command window) and the output produced by LABVIEW MATHSCRIPT:

>>d = 1.54
d =

1.54

>>b = 8
b =

8

>>h = 14
h =

14

>>vcyl = h*pi*(b/2)^2
vcyl =

703.72

>>vcube = d^3
vcube =

3.6523

>>nl = vcyl/vcube
nl =

192.68


### Exercise 2

#### Using an M-File

Create an M-File to solve Exercise 1.

#### Solution

We used the LABVIEW MATHSCRIPT editor (or any text editor) to create the following file named PingPong.m:


% 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

When run from the LABVIEW MATHSCRIPT command line, this program produces the following output:

>> PingPong

nl =

192.6796



To complicate your problem, suppose that you have not been given values for d d , b b , and h h . 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?

In LABVIEW MATHSCRIPT, one way to automate the computation of N L N L for many different combinations of parameter values is to use a for loop. (Read Programming in LABVIEW MATHSCRIPT-For Loops if you are not familiar with the use of for loops in LABVIEW MATHSCRIPT.) The following problems ask you to develop several different ways that for loops can be used to automate these computations.

### Exercise 3

#### Use a for loop

Add a for loop to your M-File from Exercise 2 to compute N L N L for b = 8 in b 8 in , h = 14 in h 14 in , and values of d d ranging from 1.0 in to 2.0 in.

#### Solution

This solution is by BrieAnne Davis.


for d=1.0:.05:2.0
b=8;
h=14;
vcyl=h*pi*(b/2)^2
vcube=d^3
nl=vcyl/vcube
end


### Exercise 4

#### Can you still use a for loop?

Modify your M-File from Exercise 3 to plot N L N L as a function of d d for b = 8 in b 8 in and h = 14 in h 14 in .

#### Solution

This solution is by Wade Stevens. Note that it uses the LABVIEW MATHSCRIPT hold on command after the plot statement to plot each point individually in the for loop.

clear all
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*')
hold on
end

This solution creates the plot in Figure 1.

This different solution is by Christopher Embrey. It uses the index variable j to step through the dv array to compute elements of the nlv array; the complete nlv array is computed, and then plotted outside the for loop.


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)

This solution creates the plot in Figure 2.

And finally, this solution by Travis Venson uses LABVIEW MATHSCRIPT's vector capabilities to perform the computation without a for loop.


%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)


### Exercise 5

#### More loops?

Modify your M-File from Exercise 3 to compute N L N L for d = 1.54 in d 1.54 in and various values of b b and h h .

#### Solution

This solution is by AJ Smith. The height, h, ranges from 12 to 15 and the base, b, ranges from 8 to 12.


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


## References

1. Anthony M. Starfield; Karl A. Smith; Andrew L. Bleloch. (1994). How To Model It: Problem Solving for the Computer Age. Edina, MN: Interaction Book Company.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks