Difference between revisions of "MAP:Cantilever Beam"

From PrattWiki
Jump to navigation Jump to search
Line 52: Line 52:
  
 
== Python ==
 
== Python ==
 +
<source lang=python>
 +
## Initialize the workspace
 +
# Import required packages
 +
import numpy as np
 +
import matplotlib.pyplot as plt
 +
 +
## Load and manipulate the data
 +
# Load data from Cantilever.dat
 +
Cantilever = np.loadtxt('Cantilever.dat')
 +
# Copy data from each column into new variables
 +
Mass = Cantilever[:,0]
 +
Displacement = Cantilever[:,1]
 +
# Convert Mass to a Force measurement
 +
Force = Mass*9.81;
 +
# Convert Displacement in inches to meters
 +
Displacement = (Displacement*2.54)/100
 +
 +
## Perform calculations
 +
# Use polyfit to find first-order fit polynomials
 +
P = np.polyfit(Force, Displacement, 1)
 +
 +
## Generate predictions
 +
# Create 100 representational Force values
 +
ForceModel = np.linspace(min(Force),max(Force),100)
 +
# Calculate Displacement predictions
 +
DispModel = np.polyval(P, ForceModel)
 +
 +
## Generate and save plots
 +
# Bring up a figure window
 +
plt.figure(1)
 +
# Clear the figure window
 +
plt.clf
 +
# Plot Displacement as a function of Force
 +
plt.plot(Force, Displacement, 'ko')
 +
# Plot the model values
 +
plt.plot(ForceModel, DispModel, 'k-')
 +
#hold off
 +
# Turn the grid on
 +
plt.grid(1)
 +
# Label and title the graph
 +
plt.xlabel('Force (Newtons)')
 +
plt.ylabel('Displacement (meters)')
 +
plt.title('Displacement vs. Force for Cantilever.dat (NetID)')
 +
# Save the graph to PostScript
 +
#print -deps RunCanPlot
 +
plt.savefig('RunCanPlot.eps')
 +
</source>

Revision as of 03:25, 6 November 2017

The Cantilever Beam lab has been a foundation of EGR 103 for several years. It demonstrates how to initialize the workspace, load and manipulate data, perform calculations, generate values of a model equation, plot data and model values, and save plots. Not bad for the second week of an introductory course in computational methods! For at least the short-term, the Cantilever Beam lab will live on in MATLAB and in Python. Since the lab itself actually develops the final code, it is acceptable to post it here. Here's what the lab's solution looks like in MATLAB and in Python 3:

MATLAB

%% Initialize the workspace
% Clear all variables
clear
% Change display to short exponential format
format short e

%% Load and manipulate the data
% Load data from Cantilever.dat
load Cantilever.dat
% Copy data from each column into new variables
Mass = Cantilever(:,1);
Displacement = Cantilever(:,2);
% Convert Mass to a Force measurement
Force = Mass*9.81;
% Convert Displacement in inches to meters
Displacement = (Displacement*2.54)/100;

%% Perform calculations
% Use polyfit to find first-order fit polynomials
P = polyfit(Force, Displacement, 1)

%% Generate predictions
% Create 100 representational Force values
ForceModel = linspace(min(Force),max(Force),100);
% Calculate Displacement predictions
DispModel = polyval(P, ForceModel);

%% Generate and save plots
% Bring up a figure window
figure(1)
% Clear the figure window
clf
% Plot Displacement as a function of Force
plot(Force, Displacement, 'ko')
% Turn hold on, plot the model values, and turn hold off
hold on
plot(ForceModel, DispModel, 'k-')
hold off
% Turn the grid on
grid on
% Label and title the graph
xlabel('Force (Newtons)')
ylabel('Displacement (meters)')
title('Displacement vs. Force for Cantilever.dat (NetID)')
% Save the graph to PostScript
print -deps RunCanPlot

Python

## Initialize the workspace
# Import required packages
import numpy as np
import matplotlib.pyplot as plt

## Load and manipulate the data
# Load data from Cantilever.dat
Cantilever = np.loadtxt('Cantilever.dat')
# Copy data from each column into new variables
Mass = Cantilever[:,0]
Displacement = Cantilever[:,1]
# Convert Mass to a Force measurement
Force = Mass*9.81;
# Convert Displacement in inches to meters
Displacement = (Displacement*2.54)/100

## Perform calculations
# Use polyfit to find first-order fit polynomials
P = np.polyfit(Force, Displacement, 1)

## Generate predictions
# Create 100 representational Force values
ForceModel = np.linspace(min(Force),max(Force),100)
# Calculate Displacement predictions
DispModel = np.polyval(P, ForceModel)

## Generate and save plots
# Bring up a figure window
plt.figure(1)
# Clear the figure window
plt.clf
# Plot Displacement as a function of Force
plt.plot(Force, Displacement, 'ko')
# Plot the model values
plt.plot(ForceModel, DispModel, 'k-')
#hold off
# Turn the grid on
plt.grid(1)
# Label and title the graph
plt.xlabel('Force (Newtons)')
plt.ylabel('Displacement (meters)')
plt.title('Displacement vs. Force for Cantilever.dat (NetID)')
# Save the graph to PostScript
#print -deps RunCanPlot
plt.savefig('RunCanPlot.eps')