# Connexions

You are here: Home » Content » Programming in LabVIEW MathScript-A Data Analysis 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 Data Analysis Example Using For Loops

Summary: This is an example of using LabVIEW MathScript to solve a simple engineering data analysis problem in which velocity and acceleration are computed from altitude data from a home-built rocket test launch.

## Note:

This example requires an understanding of the relationships between position, velocity, and acceleration of an object moving in a straight line. The Wikipedia article Motion Graphs and Derivatives has a clear explanation of these relationships, as well as a discussion of average and instantaneous velocity and acceleration and the role derivatives play in these relationships. Also, in this example, we will approximate derivatives with forward, backward, and central differences; Lecture 4.1 by Dr. Dmitry Pelinovsky at McMaster University contains useful information about this approximation. We will also approximate integrals using the trapezoidal rule; The Wikipedia article Trapezium rule has an explanation of the trapezoidal rule.

## Trajectory Analysis of an Experimental Homebuilt Rocket

On his web page Richard Nakka's Experimental Rocketry Web Site: Launch Report - Frostfire Two Rocket, Richard Nakka provides a very detailed narrative of the test firing of his Frostfire Two homebuilt rocket and subsequent data analysis. (His site provides many detailed accounts of tests of rockets and rocket motors. Some rocket launches were not as successful as the Frostfire Two launch; his site provides very interesting post-flight analysis of all launches.)

## Computation of Velocity and Acceleration from Altitude Data

In this section, we will use LABVIEW MATHSCRIPT to analyze the altitude data extracted from the plot "Altitude and Acceleration Data from R-DAS" on Richard Nakk's web page. This data is in the file Altitude.csv. We will use this data to estimate velocity and acceleration of the Frostfire Two rocket during its flight.

### Exercise 1

#### Get the data

Download the altitude data set in the file Altitude.csv (right click on the file's link) onto your computer. Then be sure to save the file to your MATHSCRIPT working directory. This can be determined by navigating to the File/MathScript Perferences tab. The file is formatted as two columns: the first column is time in seconds, and the second column is altitude in feet. Load the data into LABVIEW MATHSCRIPT and plot the altitude as a function of time.

The following sequence of LABVIEW MATHSCRIPT commands will load the data, create a vector t of time values, create a vector s of altitude values, and plot the altitude as a function of time.

Altitude = csvread('Altitude.csv');
t =Altitude(:,1);
s =Altitude(:,2);
plot(t,s)

The plot should be similar to that in Figure 1.

### Exercise 2

#### Forward Differences

Write a LABVIEW MATHSCRIPT script that uses a for loop to compute velocity and acceleration from the altitude data using forward differences. Your script should also plot the computed velocity and acceleration as function of time.

#### Solution

This solution is by Scott Jenne; it computes and plots the velocity:

Altitude = csvread('Altitude.csv');
t=Altitude(:,1);
s=Altitude(:,2);
for n=1:180;
v=((s(n+1))-s(n))/((t(n+1))-t(n));
hold on
plot(t(n),v,'o')
end

The plot produced by this code is shown in Figure 2.

### Exercise 3

#### Backward Differences

Modify your script from Exercise 2 to compute velocity and acceleration using backward differences. Remember to save your modified script with a different name than your script from Exercise 2.

#### Solution

This solution by Bryson Hinton:

Altitude = csvread('Altitude.csv');
t=Altitude(:,1);
s=Altitude(:,2);
for x=2:181
v(x)=(s(x)-s(x-1))/(t(x)-t(x-1));
plot(t(x),v(x),'b.')
hold on
end
The plot produced by this code is shown in Figure 3.

### Exercise 4

#### Central Differences

Modify your script from Exercise 2 to compute velocity and acceleration using central differences. Remember to save your modified script with a different name than your script from Exercise 2 and Exercise 3.

#### Solution

This code computes the velocity using the central difference formula.

clear all
t=Altitude(:,1);
s=Altitude(:,2);
for n=2:180
v(n-1)=(s(n+1)-s(n-1))/(t(n+1)-t(n-1));
end
plot(t(2:180),v)
The plot produced by this code is shown in Figure 4.

Compare the velocity and acceleration values computed by the different approximations. What can you say about their accuracy?

### Exercise 5

#### Can it be done without loops?

Modify your script from Exercise 2 to compute velocity and acceleration without using a for loop.

#### Solution

This code uses LABVIEW MATHSCRIPT's diff function to compute the difference between adjacient elements of t and s, and the ./ function to divide each element of the altitude differences with the corresponding element of the time differences:

clear all
t=Altitude(:,1);
s=Altitude(:,2);
v=diff(s)./diff(t);
plot(t(1:180),v)

The plot produced by this code is shown in Figure 5.

## Computation of Velocity and Altitude from Acceleration Data

In this section, we will use LABVIEW MATHSCRIPT to analyze the acceleration data extracted from the plot "Altitude and Acceleration Data from R-DAS" on Richard Nakk's web page. Download the acceleration data set in the file Acceleration.csv (right click on the file's link) onto your computer. The first column is time in seconds, and the second column is acceleration in g's. The following commands load the data into LABVIEW MATHSCRIPT and plot the acceleration as a function of time.

Acceleration = csvread('Acceleration.csv');
t = Acceleration(:,1);
a = Acceleration(:,2);
plot(t,a)

The plot should be similar to that in Figure 6.

### Exercise 6

#### Trapezoidal Rule

Write a LABVIEW MATHSCRIPT script that uses a for loop to compute velocity and altitude from the acceleration data using the trapezoidal rule. Your script should also plot the computed velocity and altitude as function of time.

#### Solution

This solution is by Jonathan Selby:

Acceleration = csvread('Acceleration.csv');
t=Acceleration (:,1);
a=Acceleration (:,2);
v(1)=0;
for n=1:181
v(n+1)=(t(n+1)-t(n))*(a(n+1)+a(n))/2+v(n);
end
plot(t,v)
This code creates the plot in Figure 7. This code can be easily extended to also compute altitude while it is computing velocity:
Acceleration = csvread('Acceleration.csv');
t=Acceleration (:,1);
a=Acceleration (:,2);
v(1)=0; % Initial velocity
s(1)=0; % Initial altitude
for n=1:181
v(n+1)=(t(n+1)-t(n))*(a(n+1)+a(n))/2+v(n);
s(n+1)=(t(n+1)-t(n))*(v(n+1)+v(n))/2+s(n);
end
plot(t,s)

This code creates the plot in Figure 8.

### Exercise 7

#### Can it be done without loops?

Modify your script from Exercise 6 to compute velocity and altitude without using a for loop.

#### Solution

This solution by Nicholas Gruman uses the LABVIEW MATHSCRIPT trapz function to compute velocity with the trapezoidal rule with respect to time at all given points in time:

Acceleration = csvread('Acceleration.csv');
t=Acceleration(:,1);
A=Acceleration(:,2);
c = length(t);
for i = 1:c
v(i)=trapz(t(1:i),A(1:i));
end
Altitude could also be computed by adding the following line in the same For loop as the previous code:
s(i)=trapz(t(1:i),v(1:i));


## 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