Вы находитесь на странице: 1из 24

Polymorphism

 Polymorphism is a feature of functions in


modern programming languages such as
LabVIEW.
 It means that a single function work on
inputs of different dimensions and data
types.
 This lets one function do the work that
you’d need several different functions to do
in older non-polymorphic languages.

Floyd, Digital Fundamentals, 10th ed


Polymorphism Example: Add

 LabVIEW’s Add function is an


example of a polymorphic
function.
 It can add a scalar plus a
scalar, or a scalar plus an
array, or an array plus an array.
 In a non-polymorphic language,
this would require several
different functions.
Floyd, Digital Fundamentals, 10th ed
Clusters
 Recall that an array is a
variable-sized collection of
elements that are all of the
same data type (such as
numeric, string, or
Boolean).
 In contrast, a cluster is a
fixed-sized collection of
elements of mixed data
types.

Floyd, Digital Fundamentals, 10th ed


Usefulness of Clusters

 Clusters are useful when you have a related


group of elements of different data types that
you want to associate with each other.
 Example: you might group a string containing a
person’s name together with an integer containing
his/her age and a Boolean indicating his/her
citizenship status.
 Clusters often let you simplify block diagrams
by reducing the number of wires on the
diagram.
Floyd, Digital Fundamentals, 10th ed
Creating a Cluster of Controls or
Indicators

 To create a cluster of controls or indicators:


1. Place a cluster shell on the front panel from
the Controls >> Modern >> Array, Matrix
& Cluster palette.
2. Place controls or indicators inside that
cluster shell. (A cluster can contain either
controls or indicators, but it cannot contain
both.)

Floyd, Digital Fundamentals, 10th ed


Brown or Pink?
 On the block diagram,
cluster terminals and
cluster wires are
colored brown if all of
the items in the
cluster are numeric.
 But the terminals and
wires are colored pink
if one or more of the
items are non-
thnumeric.
Floyd, Digital Fundamentals, 10 ed
Order within a Cluster
 Individual items within a cluster are referred
to by the order in which they were placed in
the cluster.
 The first item placed in a cluster becomes
element 0, the next item placed in the cluster
becomes element 1, and so on.
 To change the order, right-click
a cluster’s border on the
front panel and select
Reorder Controls in Cluster.
Floyd, Digital Fundamentals, 10th ed
Connecting Control Cluster to
Indicator Cluster

 You can wire a control cluster to an indicator


cluster if they contain the same number of
elements and if the data types of the
corresponding elements match.
 Example: if a control cluster’s element 0 is a
string control and its element 1 is a numeric
control, you could not wire it to an indicator
cluster whose element 0 is a numeric indicator
and whose element 1 is a string indicator.

Floyd, Digital Fundamentals, 10th ed


Cluster Functions

 LabVIEW provides several functions for


working with clusters, including:
 Unbundle
 Unbundle By Name
 Bundle
 Bundle By Name
 These and others are on the Functions >>
Programming >> Cluster & Variant
palette.
Floyd, Digital Fundamentals, 10th ed
Unbundle and Unbundle By Name
 The Unbundle and Unbundle By Name
functions are used to split a cluster into its
individual objects. (Similar to the Index
Array function for arrays.)

 Recommendation: Use Unbundle By Name


th instead
Floyd, Digital Fundamentals, 10 ed of Unbundle.
Bundle
 One use of the Bundle function is to
assemble objects into a new cluster.
(Similar to one use of the Build Array
function for arrays.)

 Recall that the Build Array function can also be used to add
elements to an existing array. There is no comparable
Floyd, Digital Fundamentals, 10th ed
function for clusters, since their size cannot change.
Bundle and Bundle By Name
 Another use of the Bundle function is to
change the values of one or elements in an
existing cluster. The Bundle By Name
function can also be used in this way.

 Recommendation: For this use, use


th
Bundle By Name instead of Bundle.
Floyd, Digital Fundamentals, 10 ed
Cluster Example: Displaying
Multiple Plots on a Chart

 Many LabVIEW indicators and functions


have input terminals that can accept
clusters.
 Example: To display more than one plot on
a waveform chart, bundle the data together
using the Bundle function. (We’ll discuss
charts in more detail next week.)

Floyd, Digital Fundamentals, 10th ed


Converting Between Arrays and
Clusters
 In some cases it
makes sense to
convert an array
to a cluster (or
vice versa).
 For such cases,
LabVIEW
provides
conversion functions on the Array palette.
(The same functions also appear on the
th
Cluster
Floyd, Digital Fundamentals, 10 ed palette.)
Matrices

 A matrix is a two-dimensional array of


numbers. (And a vector is a one-
dimensional array of numbers.)
 The branch of math called linear algebra
studies rules and techniques for
manipulating matrices and vectors.
 Scientists and engineers use matrices and
vectors to solve many types of problems.

Floyd, Digital Fundamentals, 10th ed


Matrices in LabVIEW
 LabVIEW has a matrix datatype that is
basically a special case of the array
datatype.
 A matrix must be two-dimensional, and it
can only hold numbers (real or complex).

 LabVIEW does not have a special vector


th datatype: just use a 1-D array.
Floyd, Digital Fundamentals, 10 ed
LabVIEW Matrix Functions
 LabVIEW has a large collection of powerful
functions that work on matrices and vectors.
 See the Programming > Array > Matrix palette
and the Mathematics > Linear Algebra palette.

Floyd, Digital Fundamentals, 10th ed


Solving Systems of Linear
Equations

 As a simple example, matrices can be


used to solve systems of linear equations,
which arise in many applications.
 Applications include the circuit-analysis
techniques called loop analysis and
nodal analysis that you studied in DC
Circuits.
 Suppose we want to find the current through
each resistor in the circuit on the next slide….
Floyd, Digital Fundamentals, 10th ed
Floyd, Digital Fundamentals, 10th ed
Loop Analysis

 Loop analysis (also called “mesh analysis”)


involves three steps:
1. Apply Kirchhoff’s Voltage Law (KVL) around
each loop to set up a system of linear
equations. LabVIEW can’t help with this step;
you have to do it by hand.
2. Solve the system of linear equations. Doing
this by hand can be tedious, but with LabVIEW
it’s easy.
3. Interpret the results of Step 2.
Floyd, Digital Fundamentals, 10th ed
Step 1. Applying KVL

 In our circuit, applying


KVL to the three loops
gives us the following
system of equations:

44,500 𝐼𝐴 − 22,000 𝐼𝐵 − 7500 𝐼𝐶 = 0


−22,000 𝐼𝐴 + 30,200 𝐼𝐵 − 8200 𝐼𝐶 = 12
−7500 𝐼𝐴 − 8200 𝐼𝐵 + 25,700 𝐼𝐶 = 0

Floyd, Digital Fundamentals, 10th ed


Step 2. Solving the System of
Equations in LabVIEW

44,500 𝐼𝐴 − 22,000 𝐼𝐵 − 7500 𝐼𝐶 = 0


−22,000 𝐼𝐴 + 30,200 𝐼𝐵 − 8200 𝐼𝐶 = 12
−7500 𝐼𝐴 − 8200 𝐼𝐵 + 25,700 𝐼𝐶 = 0

Floyd, Digital Fundamentals, 10th ed


Step 3. Interpreting the Results
 To interpret the results
we must recognize that
in the original circuit,
𝐼1 = 𝐼𝐴
𝐼2 = 𝐼𝐵 − 𝐼𝐴
𝐼3 = 𝐼𝐶 − 𝐼𝐴
𝐼4 = 𝐼𝐵 − 𝐼𝐶
𝐼𝐿 = 𝐼𝐶
 This gives us the current
through each resistor.
Floyd, Digital Fundamentals, 10th ed
Converting Between Arrays and
Matrices
 In some cases it
makes sense to
convert an array
to a matrix (or
vice versa).
 For such cases,
LabVIEW
provides
conversion functions on the Array palette.

Floyd, Digital Fundamentals, 10th ed

Вам также может понравиться