Difference between revisions of "MAP:Cantilever Beam"

From PrattWiki
Jump to navigation Jump to search
Line 61: Line 61:
 
# %% Load and manipulate the data
 
# %% Load and manipulate the data
 
# Load data from Cantilever.dat
 
# Load data from Cantilever.dat
beam_data = np.loadtxt('cantilever.dat')
+
beam_data = np.loadtxt('Cantilever.dat')
 
# Copy data from each column into new variables
 
# Copy data from each column into new variables
mass = beam_data[:, 0].copy()
+
mass = beam_data[:, 0].copy()
displ = beam_data[:, 1].copy()
+
disp = beam_data[:, 1].copy()
 
# Convert mass to a force measurement
 
# Convert mass to a force measurement
 
force = mass * 9.81
 
force = mass * 9.81
# Convert displace in inches to meters
+
# Convert displacement in inches to meters
displ = (displ * 2.54) / 100.0
+
disp = (disp * 2.54) / 100.0
  
 
# %% Perform calculations
 
# %% Perform calculations
 
# Use polyfit to find first-order fit polynomials
 
# Use polyfit to find first-order fit polynomials
p = np.polyfit(force, displ, 1)
+
p = np.polyfit(force, disp, 1)
 +
print(p)
  
 
# %% Generate predictions
 
# %% Generate predictions
# Create 100 representational Force values
+
# Create 100 representational force values
 
force_model = np.linspace(min(force), max(force), 100)
 
force_model = np.linspace(min(force), max(force), 100)
# Calculate Displacement predictions
+
# Calculate displacement predictions
 
disp_model = np.polyval(p, force_model)
 
disp_model = np.polyval(p, force_model)
  
 
# %% Generate and save plots
 
# %% Generate and save plots
# Bring up a figure window
+
# Create a Figure and Axes
fig, ax = plt.subplots(1, num=1, clear=True)
+
fig, ax = plt.subplots(num=0, clear=True)
 
# Plot Displacement as a function of Force
 
# Plot Displacement as a function of Force
ax.plot(force, displ, 'ko')
+
ax.plot(force, disp, 'ko')
 
# Plot the model values
 
# Plot the model values
 
ax.plot(force_model, disp_model, 'k-')
 
ax.plot(force_model, disp_model, 'k-')
 
# Turn the grid on
 
# Turn the grid on
ax.grid()
+
ax.grid(True)
 
# Label and title the graph
 
# Label and title the graph
ax.set(xlabel='Force (Newtons)',
+
ax.set_xlabel('Force (Newtons)')
      ylabel='Displacement (meters)',
+
ax.set_ylabel('Displacement (meters)')
      title='Displacement vs. Force for Cantilever.dat (NetID)')
+
ax.set_title('Displacement vs. Force for Cantilever.dat (NetID)')
# Save the graph to PostScript and PDF
+
# Use tight layout
 +
fig.tight_layout()
 +
# Save the graph to PostScript
 
fig.savefig('RunCanPlot.eps')
 
fig.savefig('RunCanPlot.eps')
 
fig.savefig('RunCanPlot.pdf')
 
fig.savefig('RunCanPlot.pdf')
 +
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 01:15, 23 January 2019

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
beam_data = load('Cantilever.dat')
% Copy data from each column into new variables
mass  = beam_data(:,1);
displ = beam_data(:,2);
% Convert mass to a force measurement
force = mass*9.81;
% Convert displacement in inches to meters
displ = (displ*2.54)/100;

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

%% Generate predictions
% Create 100 representational force values
force_model = linspace(min(force),max(force),100);
% Calculate Displacement predictions
disp_model = polyval(P, force_model);

%% 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, displ, 'ko')
% Turn hold on, plot the model values, and turn hold off
hold on
plot(force_model, disp_model, '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 and PDF
print -deps RunCanPlot
print -dpdf RunCanPlot

Python

More Pythonic

# %% Import modules
import numpy as np
import matplotlib.pyplot as plt

# %% Load and manipulate the data
# Load data from Cantilever.dat
beam_data = np.loadtxt('Cantilever.dat')
# Copy data from each column into new variables
mass = beam_data[:, 0].copy()
disp = beam_data[:, 1].copy()
# Convert mass to a force measurement
force = mass * 9.81
# Convert displacement in inches to meters
disp = (disp * 2.54) / 100.0

# %% Perform calculations
# Use polyfit to find first-order fit polynomials
p = np.polyfit(force, disp, 1)
print(p)

# %% Generate predictions
# Create 100 representational force values
force_model = np.linspace(min(force), max(force), 100)
# Calculate displacement predictions
disp_model = np.polyval(p, force_model)

# %% Generate and save plots
# Create a Figure and Axes
fig, ax = plt.subplots(num=0, clear=True)
# Plot Displacement as a function of Force
ax.plot(force, disp, 'ko')
# Plot the model values
ax.plot(force_model, disp_model, 'k-')
# Turn the grid on
ax.grid(True)
# Label and title the graph
ax.set_xlabel('Force (Newtons)')
ax.set_ylabel('Displacement (meters)')
ax.set_title('Displacement vs. Force for Cantilever.dat (NetID)')
# Use tight layout
fig.tight_layout()
# Save the graph to PostScript
fig.savefig('RunCanPlot.eps')
fig.savefig('RunCanPlot.pdf')

More MATLABic

# %% Import modules
import numpy as np
import matplotlib.pyplot as plt

# %% Load and manipulate the data
# Load data from Cantilever.dat
beam_data = np.loadtxt('Cantilever.dat')
# Copy data from each column into new variables
mass  = beam_data[:, 0].copy()
displ = beam_data[:, 1].copy()
# Convert mass to a force measurement
force = mass * 9.81
# Convert displace in inches to meters
displ = (displ * 2.54) / 100.0

# %% Perform calculations
# Use polyfit to find first-order fit polynomials
p = np.polyfit(force, displ, 1)

# %% Generate predictions
# Create 100 representational Force values
force_model = np.linspace(min(force), max(force), 100)
# Calculate Displacement predictions
disp_model = np.polyval(p, force_model)

# %% 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, displ, 'ko')
# Plot the model values
plt.plot(force_model, disp_model, 'k-')
# Turn the grid on
plt.grid()
# 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 and PDF
plt.savefig('RunCanPlot.eps')
plt.savefig('RunCanPlot.pdf')