A Poor Example of a Project - But Gives Some Direction

advertisement
A Poor Example of a Project - But Gives
Some Direction
ã Bill Knowlton
Materials Science & Engineering
Electrical & Computer Engineering
Boise State Unversity
Ÿ The idea behind this example is two-fold:
1. To demonstrate some of the analytical capabilities of Mathematica for which the
student may use to examine physical concepts when provided data and equations.
2. To demonstrate the below-minimum amount of computational work that is required
for a project in this course
Note: The comments and discussion on the data and results of the manipulation of the
data in this example and the labeling (arrows, text,&/or legends) of the plots are below
the minimum required.
Ideal Gas Law Example
Ÿ In this example project, we will examine the ideal gas law [1] in several ways:
- Plot the ideal gas law examining pressure as a function of volume and temperature
(i.e., p=p(V,T) ) using several different approaches
1. Using the Range[ ] and Evaluate[ ] commands
2. Using the Table[ ] and Show[ ] commands
3. Using the Manipulate[ ] command
- Fit ideal gas law data using several approaches
1. Using the Manipulate[ ] command
2. Using the NonlinearModelFit[ ] curve fitting command with several error analyses
ã 1. Using the Range[ ] and Evaluate[ ] commands
Here, the Ideal gas law defined as a function where p, v, n, and T are the pressure (atm), volume (liters),
number of moles (moles), and temperature (K), respectively. The range of temperatures is defined
using the command Range[ ].
The Evaluate[ ] command with the Range[ ] command allow a function to be plotted as a function of
two variables. In the example below, pressure is plotted as a function of volume and temperature. The
command is good for plotting 3 variables on a 2D plot.
2
In[1]:=
Project Example.nb
Clear@v, R, T, p, nD H*initialization of variables*L
nRT
p@v_, n_, T_D :=
H*Ideal gas law defined as a function where p,
v
v, n, and T are the pressure, volume,
number of moles, and temperature, respectively*L
H*Constants*L
R = 0.08206;H* ideal gas constant, atm L Hmol KL*L
imin = 200; imax = 500; step = 50;
T = Range@imin, imax, stepD; H*defining the temperature range*L
H*plots*L
Plot@Evaluate@p@v, 1, TDD, 8v, 1, 10<, Frame ® True,
GridLines ® Automatic, PlotRange ® 880, 10<, 80, 50<<,
PlotStyle ® 8RGBColor@0, 0, 1D<, FrameLabel ® 8"v HLL", "p HatmL"<,
PlotLabel -> "p vs T for T=200-500K & n=1 mole"D
p vs T for T=200-500K & n=1 mole
50
40
Out[6]=
p HatmL
30
20
10
0
0
2
4
v HLL
6
8
10
Project Example.nb
3
ã Adding arrows and text to a plot can be done several ways. Below, I just copied and pasted the plot
and then used “Drawing Tools” under the “Graphics” menu.
p vs T for T=200-500K & n=1 mole
50
40
Increasing Temperature
In[7]:=
p HatmL
30
20
10
0
0
2
4
v HLL
6
8
10
8
10
p vs T for T=200-500K & n=1 mole
50
40
Increasing Temperature
Out[7]=
p HatmL
30
20
10
0
0
2
4
v HLL
6
Comment on the plot:
From the isotherm plot, it can be seen that pressure is inversely proportional to volume - that is, as volume
increases, pressure decreases. Additionally, as temperature increases both pressure and volume increase
demonstrating that pressure and volume are directly proportional to temperature. Note that labels and arrows
have been added to help guide the reader.
4
Project Example.nb
ã
2. Using the Table[ ] and Show[ ] commands - Good for plotting 3 variables on a 2D plot
In the example below, pressure is plotted as a function of volume and temperature using the Table[ ]
command, which allows a function generate data from an equation that is a function of one or more
varialbes - in this case, two variables. The Table is named as another variable and then the Show[ ]
command is used to plot the data in the table.
In[8]:=
Clear@v, R, T, p, pressureplotsD
nRT
p@v_, n_, T_D :=
v
H*Constants*L
R = 0.08206;H*atm L Hmol KL*L
pressureplots = Table@Plot@p@v, 1, TD, 8v, 1, 10<,
Frame ® True, GridLines ® Automatic, PlotRange ® 880, 10<, 80, 50<<,
PlotStyle ® 8RGBColor@0, 0, 1D<, FrameLabel ® 8"v HLL", "p HatmL"<,
PlotLabel -> "p vs T for T=200-500K & n=1 mole"D, 8T, 200, 500, 50<D;
Show@pressureplotsD
p vs T for T=200-500K & n=1 mole
50
40
p HatmL
30
Out[12]=
20
10
0
0
2
4
v HLL
6
8
10
Comment on the plot:
The Table[ ] command produces the same isotherm plot as when using the Evaluate[ ] & Range[ ] commands.
Note that labels and arrows need to be added to the plot to help guide the reader.
Project Example.nb
ã
5
2. Using the Manipulate[ ] command - good for seeing trends in an equation and rough fitting of data
with a particular equation
A more interactive method to examine the effect of multiple variables in a plot is to use the Manipulate[
] command. In the example below, the plot of Pressure versus Volume taken from above and
‘wrapping’ the Manipulate[ ] command around it with the addition of both temperature and the number
of moles results in the same P vs V plot plus two levers: temperature and moles. The levers of each
can be moved and the plot will change accordingly.
In[13]:=
Clear@v, R, T, pD
nRT
p@v_, n_, T_D :=
v
H*Constants*L
R = 0.08206;H*atm L Hmol KL*L
H*Plots*L
Manipulate@Plot@p@v, n, TD, 8v, 1, 10<, Frame ® True, GridLines ® Automatic,
PlotRange ® 880, 10<, 80, 50<<, PlotStyle ® 8RGBColor@0, 0, 1D<,
FrameLabel ® 8"v HLL", "p HatmL"<, PlotLabel -> "p vs T for T=200-500K"D,
88T, 300, "Temperature HKL"<, 100, 500, 5<, 88n, 1, "n HmolesL"<, .100, 5, .1<D
H* both temperature and number of moles can be changed *L
Temperature HKL
n HmolesL
p vs T for T=200-500K
50
40
Out[16]=
p HatmL
30
20
10
0
0
2
4
v HLL
6
8
10
Comment : Increasing either the moles or the temperature by moving the slides from left to right increases the
pressure. Note that by clicking on the ‘+‘ results in the actual value of the corresponding variable to be displayed.
6
Example.nb
Comment : Increasing either the moles or the temperature by moving the slides from left to rightProject
increases
the
pressure. Note that by clicking on the ‘+‘ results in the actual value of the corresponding variable to be displayed.
ã We can also import pressure versus volume data
In[57]:=
Clear@v, R, T, pD
nRT
p@v_, n_, T_D :=
v
H*Constants*L
R = 0.08206;H*atm L Hmol KL*L
H*Importing data; Note: to insert the location of your file, D
use "Insert" from pull-down menu and then select "File Path"*L
data = Import@"D:\\Bill\\Bsu\\Courses\\MSE 510 ElecOptDielProps\\Projects
Exams Quizzes @MSE 510D\\Projects\\pressure.csv"D
H*Plots*L
dataplot = ListPlot@data, Frame ® True, GridLines ® Automatic,
PlotStyle -> 8RGBColor@0, 1, 1D, AbsolutePointSize@10D<,
FrameLabel ® 8"v HLL", "p HatmL"<D
Manipulate@Show@Plot@p@v, n, TD, 8v, .1, 10<, Frame ® True, GridLines ® Automatic,
PlotRange ® 880, 10<, 80, 140<<, PlotStyle ® 8RGBColor@0, 0, 1D<,
FrameLabel ® 8"v HLL", "p HatmL"<, PlotLabel -> "p vs T for T=200-500K"D,
dataplotD, 88T, 300, "Temperature HKL"<, 100, 500, 5<,
88n, 1, "n HmolesL"<, .100, 5, .1<D
Out[60]=
880.42, 130.783<, 80.98, 52.3133<, 82.15, 26.1566<, 83.17, 17.4378<,
83.98, 13.0783<, 85.1, 10.4627<, 87.99, 6.53916<, 810, 5.23133<<
120
Out[61]=
p HatmL
100
80
60
40
20
2
4
v HLL
6
8
10
Project Example.nb
7
Temperature HKL
n HmolesL
p vs T for T=200-500K
140
120
100
p HatmL
Out[62]=
80
60
40
20
0
0
2
4
v HLL
6
8
10
Comment : Using the Manipulate[ ] command provides a means to understand the degree to which certan
parameters affect a model or equation.
8
Project Example.nb
ã For Non-linear curve fitting and output of various statistical error analysis parameters [2-5], one can
use the NonlinearModelFit[ ] function.
In[23]:=
Clear@SomeData, SomeDataplot, SomeDatafit, plotFitD
H*Place data in list*L
SomeData = 880, 0<, 81, 1<, 82, 4.1<, 83, 8.9<, 84, 16.1<, 85, 24.9<<
H*Create scatter plot of data*L
SomeDataplot = ListPlot@SomeData, Frame ® True, GridLines ® Automatic,
PlotStyle ® 8RGBColor@1, 0, 0D, PointSize@0.02`D<,
FrameLabel ® 8"y-data Harb. UnitsL", "x-data Harb. UnitsL"<D
H*Perform fit and define the fitting function*L
Print@"y = ",
SomeDatafit = NonlinearModelFit@SomeData, a x ^ 2 + b x + c, 8a, b, c<, xDD
H*Plotting the NonlinearModelFit data. Note that SomeDatafit
has to show that it is a function of x; I.e., SomeDatafit@xD*L
plotFit = Plot@SomeDatafit@xD, 8x, 0, 5<, Frame ® True, GridLines ® Automatic,
PlotStyle ® 8RGBColor@0, 1, 0D<, FrameLabel ® 8"x", "y"<D
H*showing the plots pplot and plotpfit on the same graph*L
Show@SomeDataplot, plotFit, PlotLabel ® "Fit = Green Line; Data = Red Points"D
Out[24]=
880, 0<, 81, 1<, 82, 4.1<, 83, 8.9<, 84, 16.1<, 85, 24.9<<
25
Out[25]=
x-data Harb. UnitsL
20
15
10
5
0
0
1
2
3
y-data Harb. UnitsL
4
y = FittedModelB -0.00714286 + 0.0421429 x + 0.989286 x2 F
5
Project Example.nb
9
25
20
Out[27]=
y
15
10
5
0
0
1
2
3
4
5
4
5
x
Fit = Green Line; Data = Red Points
25
Out[28]=
x-data Harb. UnitsL
20
15
10
5
0
0
1
2
3
y-data Harb. UnitsL
Comment : The Non Linear Curve fitting model shows very close agreement to the data. The degree of
"goodness of fit" is examined in the next section.
Note that labels and arrows need to be added to the plots to help guide the reader.
10
Project Example.nb
ã Error analysis on Nonlinear Curve Fitting that includes:
1. Listing the Nonlinear Fit Model
2. Lising some of the parameters associated with the fit
3. Coefficient of Determination
4. Adjusted Coefficient of Determination
5. Residuals - listing and plotting
6. Confidence Intervals - listing and plotting
Note: When using any of these parameters, it is insufficient to use one in a project without explaining
what the parameter is and why it is being used. For discussion and definitions of data and error
analyses, see referenced [2-5].
In[29]:=
H* Use LinearModelFit command that is new to Mathematica 7*L
Print@"y = ",
SomeDatafit = NonlinearModelFit@SomeData, a x ^ 2 + b x + c, 8a, b, c<, xDD
Print@"Model Parameter Table = ", SomeDatafit@"ParameterTable"DD
H*Lists some parameters of the fit*L
PrintA"R2 = ", SomeDatafit@"RSquared"DE H* Provides the R2 value *L
PrintA"Adjusted R2 = ", SomeDatafit@"AdjustedRSquared"DE
H* Provides the adjusted R2 value *L
Print@"Residuals = ", SomeDatafit@"FitResiduals"DD
H* Lists the Residuals of the fit*L
Print@"Plot of Residuals"D
ListPlot@SomeDatafit@"FitResiduals"D, PlotRange ® 8- 0.1, 0.1<,
Filling ® Axis, Frame ® True, PlotLabel ® "Residuals from Fit"D
H* Provides the Residuals of the fit *L
ListPlot@
Table@SomeDatafit@"ParameterConfidenceIntervals", ConfidenceLevel ® pD,
8p, 8.8, .85, .9, .95, .99<<D, Frame ® True, GridLines ® Automatic,
PlotLabel ® "Confidence Intervals: 80%, 85%, 90%, 95%, 99%"D
y = FittedModelB -0.00714286 + 0.0421429 x + 0.989286 x2 F
Estimate
Model Parameter Table = a
b
c
Standard Error t-Statistic
0.989286
0.0172763
0.0421429
0.0899915
-0.00714286 0.0956716
P-Value
57.2627
0.0000117322
0.468298
0.671496
-0.0746601 0.945185
R2 = 0.999966
Adjusted R2 = 0.999932
Residuals = 80.00714286, - 0.0242857, 0.0657143, - 0.122857, 0.11, - 0.0357143<
Plot of Residuals
Project Example.nb
11
Residuals from Fit
0.10
0.05
Out[35]=
0.00
-0.05
-0.10
0
1
2
3
4
5
6
Confidence Intervals: 80%, 85%, 90%, 95%, 99%
1.0
0.8
Out[36]=
0.6
0.4
0.2
0.0
-0.4
-0.2
0.0
0.2
0.4
0.6
0.8
Comment: When using any of these parameters, it is insufficient to use one in a project without explaining what
the parameter is and why it is being used. For discussion and definitions of data and error analyses, see
referenced [2-5].
Note that labels and arrows need to be added to the plots to help guide the reader.
12
Project Example.nb
ã Calculus can be performed on the Nonlinear Fit Model:
1. Integration: with and without limits - 2 approaches
2. 1st Derivative - 3 approaches
3. 2nd Derivative - 3 approaches
In[37]:=
H*integration without limits - 2 ways*L
Integrate@SomeDatafit@xD, xD
à SomeDatafit@xD â x
H*integration with limits - 2 ways*L
Integrate@SomeDatafit@xD, 8x, 2, 5<D
à SomeDatafit@xD â x
5
2
H*1st derivative - 3 ways*L
D@SomeDatafit@xD, xD
¶x SomeDatafit@xD
SomeDatafit '@xD
H*Second Derivative - 3 ways*L
D@SomeDatafit@xD, 8x, 2<D
¶x ¶x SomeDatafit@xD
SomeDatafit ''@xD
Out[37]=
Out[38]=
Out[39]=
Out[40]=
Out[41]=
Out[42]=
Out[43]=
Out[44]=
Out[45]=
Out[46]=
- 0.00714286 x + 0.0210714 x2 + 0.329762 x3
- 0.00714286 x + 0.0210714 x2 + 0.329762 x3
39.0032
39.0032
0.0421429 + 1.97857 x
0.0421429 + 1.97857 x
0.0421429 + 1.97857 x
1.97857
1.97857
1.97857
Project Example.nb
13
ã Indefinite integrals and derivatives can be plotted:
1. Integration: with or without limits
2. 1st Derivative
3. 2nd Derivative
In[47]:=
Clear@iSomeDatafit, dSomeDatafitD
H*Defining functions to plot*L
H*Assigning a function name to the integral of SomeDatafit *L
iSomeDatafit@x_D = Integrate@SomeDatafit@xD, xD;
H*Assigning a function name to the derivative of SomeDatafit *L
dSomeDatafit@x_D = SomeDatafit '@xD;
H*Plotting the functions*L
PlotAiSomeDatafit@xD, 8x, 0, 10<, Frame ® True, GridLines ® Automatic,
PlotStyle ® 8RGBColor@0, 1, 0D<, FrameLabel ® 9"x", "Ù yâx"=,
PlotLabel ® "First Derivative of Nonlinear Fit Model"E
Plot@dSomeDatafit@xD, 8x, 0, 10<, Frame ® True, GridLines ® Automatic,
PlotStyle ® 8RGBColor@0, 1, 0D<, FrameLabel ® 8"x", "y'"<,
PlotLabel ® "Second Derivative of Nonlinear Fit Model"D
First Derivative of Nonlinear Fit Model
300
250
Ù yâx
200
Out[50]=
150
100
50
0
0
2
4
6
x
8
10
14
Project Example.nb
Second Derivative of Nonlinear Fit Model
20
15
y'
Out[51]=
10
5
0
0
2
4
6
8
10
x
Comments: The Nonlinear Model is a 3rd order or degree polynomial fit. The first derivative should show a
quadratic response while the second derivative should show a linear response. Indeed, this is what is observed
in the plots above.
Performing calculus on models can provide useful information including:
o
o
Differentiation
§
Slope
§
Max-Min
§
Inflection points
§
Curvature and radius of curvature
§
Develop Differentials
Integration
§
Sum under curve (y*x relationships)
§
Within cycle where paths differ and hysteresis effects exist
§
Develop State functions
§
Thermodynamics - work and energy relations
ã References:
[1] W. Seese and G. Daub, Basic chemistry, 4th Ed. (Prentice Hall, 1985) p. 274-277.
[2] D. Skoog, D. West, & F. Holler, “Fundatmentals of Analytical Chemistry”, 5th Ed., (Sauners College
Publishing, New york; 1963) Ch. 1
[3] H. Motulsky & A. Christopoulos, “Fitting Models to Biological Data using Linear and Nonlinear
Regression -A practical Guide to Curve Fitting, Version 4 (GraphPad Prism; 2003) p. 1-351.
[4] Interpreting Regression Results, OriginLabs - Origin Software - http://www.originlab.com/
[5] H. Berendsen, “A Student’s Guide to Data & Error Analysis”, (Cambridge Press, 2011), p. 1-225
Note: [3] & [4] are posted
Work in Progress
Project Example.nb
15
Work in Progress
In[52]:=
Clear@v, R, T, p, nplotsD
pv
n@T_, p_, v_D :=
RT
H*Constants*L
R = 0.08206;H*atm L Hmol KL*L
nplots =
Table@Plot@n@T, 1, vD, 8T, 273, 400<, Frame ® True, GridLines ® Automatic,
PlotRange ® 80.01, 0.1<, PlotStyle ® 8RGBColor@0, 0, 1D<,
FrameLabel ® 8"T HKL", "n Hnumber of molesL"<,
PlotLabel -> "n vs T for T=273-400K & p=1 atm"D, 8v, 0.5, 2, .25<D;
Show@
nplotsD
n vs T for T=273-400K & p=1 atm
0.10
Out[56]=
n Hnumber of molesL
0.08
0.06
0.04
0.02
280
300
320
340
T HKL
360
380
400
Download