Difference between revisions of "MAP:Cantilever Beam"
(→Mathematica) |
(→Mathematica) |
||
Line 158: | Line 158: | ||
== Mathematica == | == Mathematica == | ||
− | Starting with the Summer of 2021, I have been working on moving from Maple to Mathematica as a symbolic computation language. A static, publicly-viewable Mathematica version of this program is in the box below. If you have a [https://www.wolframcloud.com/ Wolfram Cloud] account (including the free basic version), you can go to the | + | Starting with the Summer of 2021, I have been working on moving from Maple to Mathematica as a symbolic computation language. A static, publicly-viewable Mathematica version of this program is in the box below. If you have a [https://www.wolframcloud.com/ Wolfram Cloud] account (including the free basic version), you can go to the Cloud version via [https://www.wolframcloud.com/env/b59aa935-2740-40e6-84b0-00afaf875f8f this link] |
<html> | <html> |
Revision as of 18:18, 9 June 2021
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. There are two Python versions - the first is a more object-oriented approach and the second is a more state-machine approach.
MATLAB
1 %% Initialize the workspace
2 % Clear all variables
3 clear
4 % Change display to short exponential format
5 format short e
6
7 %% Load and manipulate the data
8 % Load data from Cantilever.dat
9 beam_data = load('Cantilever.dat')
10 % Copy data from each column into new variables
11 mass = beam_data(:,1);
12 displ = beam_data(:,2);
13 % Convert mass to a force measurement
14 force = mass*9.81;
15 % Convert displacement in inches to meters
16 displ = (displ*2.54)/100;
17
18 %% Perform calculations
19 % Use polyfit to find first-order fit polynomials
20 P = polyfit(force, displ, 1)
21
22 %% Generate predictions
23 % Create 100 representational force values
24 force_model = linspace(min(force),max(force),100);
25 % Calculate Displacement predictions
26 disp_model = polyval(P, force_model);
27
28 %% Generate and save plots
29 % Bring up a figure window
30 figure(1)
31 % Clear the figure window
32 clf
33 % Plot Displacement as a function of Force
34 plot(force, displ, 'ko')
35 % Turn hold on, plot the model values, and turn hold off
36 hold on
37 plot(force_model, disp_model, 'k-')
38 hold off
39 % Turn the grid on
40 grid on
41 % Label and title the graph
42 xlabel('Force (Newtons)')
43 ylabel('Displacement (meters)')
44 title('Displacement vs. Force for Cantilever.dat (NetID)')
45 % Save the graph to PostScript and PDF
46 print -deps RunCanPlot
47 print -dpdf RunCanPlot
Python
More Pythonic
1 # %% Import modules
2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 # %% Load and manipulate the data
6 # Load data from Cantilever.dat
7 beam_data = np.loadtxt('Cantilever.dat')
8 # Copy data from each column into new variables
9 mass = beam_data[:, 0].copy()
10 disp = beam_data[:, 1].copy()
11 # Convert mass to a force measurement
12 force = mass * 9.81
13 # Convert displacement in inches to meters
14 disp = (disp * 2.54) / 100.0
15
16 # %% Perform calculations
17 # Use polyfit to find first-order fit polynomials
18 p = np.polyfit(force, disp, 1)
19 print(p)
20
21 # %% Generate predictions
22 # Create 100 representational force values
23 force_model = np.linspace(min(force), max(force), 100)
24 # Calculate displacement predictions
25 disp_model = np.polyval(p, force_model)
26
27 # %% Generate and save plots
28 # Create a Figure and Axes
29 fig, ax = plt.subplots(num=0, clear=True)
30 # Plot Displacement as a function of Force
31 ax.plot(force, disp, 'ko')
32 # Plot the model values
33 ax.plot(force_model, disp_model, 'k-')
34 # Turn the grid on
35 ax.grid(True)
36 # Label and title the graph
37 ax.set_xlabel('Force (Newtons)')
38 ax.set_ylabel('Displacement (meters)')
39 ax.set_title('Displacement vs. Force for Cantilever.dat (NetID)')
40 # Use tight layout
41 fig.tight_layout()
42 # Save the graph to PostScript
43 fig.savefig('RunCanPlot.eps')
44 fig.savefig('RunCanPlot.pdf')
More MATLABic
1 # %% Import modules
2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 # %% Load and manipulate the data
6 # Load data from Cantilever.dat
7 beam_data = np.loadtxt('Cantilever.dat')
8 # Copy data from each column into new variables
9 mass = beam_data[:, 0].copy()
10 displ = beam_data[:, 1].copy()
11 # Convert mass to a force measurement
12 force = mass * 9.81
13 # Convert displace in inches to meters
14 displ = (displ * 2.54) / 100.0
15
16 # %% Perform calculations
17 # Use polyfit to find first-order fit polynomials
18 p = np.polyfit(force, displ, 1)
19
20 # %% Generate predictions
21 # Create 100 representational Force values
22 force_model = np.linspace(min(force), max(force), 100)
23 # Calculate Displacement predictions
24 disp_model = np.polyval(p, force_model)
25
26 # %% Generate and save plots
27 # Bring up a figure window
28 plt.figure(1)
29 # Clear the figure window
30 plt.clf()
31 # Plot Displacement as a function of Force
32 plt.plot(force, displ, 'ko')
33 # Plot the model values
34 plt.plot(force_model, disp_model, 'k-')
35 # Turn the grid on
36 plt.grid()
37 # Label and title the graph
38 plt.xlabel('Force (Newtons)')
39 plt.ylabel('Displacement (meters)')
40 plt.title('Displacement vs. Force for Cantilever.dat (NetID)')
41 # Save the graph to PostScript and PDF
42 plt.savefig('RunCanPlot.eps')
43 plt.savefig('RunCanPlot.pdf')
Embedded Example
The following is an example using Trinket. You can make changes to the code and then re-run it!
Mathematica
Starting with the Summer of 2021, I have been working on moving from Maple to Mathematica as a symbolic computation language. A static, publicly-viewable Mathematica version of this program is in the box below. If you have a Wolfram Cloud account (including the free basic version), you can go to the Cloud version via this link