Uploaded by ikholomieiev

Computer practical 1

advertisement
Computer Practical 1
Introduction
Each computer practical consists of multiple elements to help you practice is with different concepts
and techniques. The goal is to get you to get comfortable with the different programs. Although the
computer practical needs to be handed in individually we encourage you to work together to find the
solutions to more difficult problems. Please upload all relevant documents in their original format (so
upload an excel file as .xlsx and the Python notebooks as .ipnb etc. ). Use the following format for
naming your files: CP1_Sxxxxxxx.xlsx to make it easy for the TA’s to check them 😊.
Assignment 1: Calculus with Excel
In the first assignment, based on the computer practical of Campbell et al1 will practice with some
concepts you learned in the calculus course and use excel as a tool for visualization and for finding
numerical solutions to problems. We will first go through the methods and later apply them to
Hermite polynomials. This is a group of polynomials that you will encounter later in the course.
-
Determine real roots of quadratic equations
Use excel to visualize functions to aid in the determination of the real roots
Use the Newton-Raphson Method to find real roots of polynomials
Integration under a curve
Use this knowledge to find the roots of Hermite polynomials and show that they are
orthogonal.
Real roots of quadratic equations
You are going to create a spreadsheet to determine the real roots of quadratic equations of the form:
π‘Žπ‘₯ 2 + 𝑏π‘₯ + 𝑐 = 0
For such quadratics, the discriminant b2−4ac dictates whether or not there are real solutions for the
quadratic function.
Open the “Excel sheet CP1” spreadsheet, and view the tab labelled “Quadratic”.
1. In this worksheet, compute the roots to the following quadratic equations:
7π‘₯ 2 − 2π‘₯ − 1 = 0
4π‘₯ 2 − 20π‘₯ + 25 = 0
3π‘₯ 2 + 4π‘₯ + 2 = 0
2 − π‘₯2 + π‘₯ + 1 = 0
Use an IF function to change the outcome depending whether or not there are real roots. If the value
from the discriminant is <0, then set the value to “No real roots”; if the discriminant is ≥0, display both
values for the real roots to three decimal places.
Newton Raphson Method
Whilst the quadratic formula is useful, finding the real roots of more complex systems, such as
polynomials, is more challenging. There are methods to find the roots algebraically, but they might be
time consuming, not always obvious, and only possible for low degree polynomials. For higher degree
polynomials finding the roots, is more approximating the roots.
In such scenarios, a variety of iterative numerical methods
have been developed, such as the Newton-Raphson
algorithm, which provide approximations of the roots,
rather than analytical solutions.
The Newton Raphson Method is defined as follows:
x1 = x0 −
f(x0 )
f ′ (x0 )
for f ′ (xn ) ≠ 0
For a function f(x) and an initial guess of a root of the
function x0, a closer approximation to a root of the
function, x1, is calculated: subsequent iterations, using the
previous value, then will converge upon a root:
x𝑛+1 = x𝑛 −
f(x𝑛 )
f ′ (x𝑛 )
for f ′ (xn ) ≠ 0
Graphically, this process can be illustrated as shown right. The degree of convergence upon a root is
measured using the convergence precision, discussed below.
We will set up a spreadsheet in Excel to perform this analysis (provided the function is differentiable),
which can be used to approximate real roots of any cubic function with general formula:
𝑓(π‘₯) = π‘Žπ‘₯ 3 + 𝑏π‘₯ 2 + 𝑐π‘₯ + 𝑑
2. In the tab labelled “Newton Method”, insert the right formula and values in the already
prepared boxes:
i)
cells for the values of the coefficients a−d
ii)
a cell for the initial guess valueπ‘₯0
iii)
the formulae needed for𝑓(π‘₯𝑛 ) and 𝑓 ′ (π‘₯𝑛 )dependent on π‘₯𝑛
NB. You will have to derive the formula for 𝑓′(π‘₯𝑛 )
iv)
the formulae to determine each π‘₯𝑛+1 approximation using the previous values of π‘₯𝑛 ,
𝑓(π‘₯𝑛 ) , 𝑓 ′ (π‘₯𝑛 ) according to the Newton-Raphson formula
v)
the formulae to calculate the difference between each iteration (referred to as the
convergence precision).
With the equations in place, it is possible to begin evaluating potential guesses, and seeing which root
(if any) it converges to. There are several caveats to using this method:
1. The first is that if any guess xn happens to hit a stationary point of the function 𝑓(π‘₯), then the
derivative 𝑓′(π‘₯) )will be 0, and therefore division by 0 will return an error.
2. If the initial estimate is poor, i.e. too far from the root, a large number of iterations may be
required in order to achieve convergence. Choosing better initial values (significantly larger,
slightly larger, slightly smaller, significantly smaller) can probe potential scenarios.
3. If there are multiple roots, this method will only find one of these. Multiple initial guesses will
be needed to determine other roots.
It can therefor always be useful to plot the function to get a good idea of what values for the root are
expected.
NB. The proximity of the initial guess does not necessarily result in finding the nearest solution.
For example, for the function:
𝑓(π‘₯) = π‘₯ 3 − 2π‘₯ + 3
The initial guesses (π‘₯0 ) tabulated below converge to the
following real roots – with an initial guess π‘₯0 = 2, the
convergent root misses the nearest root (~0.7984), and
converges on the root at approx. -1.1284.
Initial guess
(x0)
0
2
4
Convergent root
(5 d.p.)
0.79836
-1.12842
3.33006
Convergence threshold
When a large number of iterations are required to obtain convergence, you may be best advised to
choose better initial guesses that are closer to a root. As these are numerical solutions to the values,
each iteration get closer and closer to the actual root; as this process could continue indefinitely, there
comes a point where the precision obtained numerically is sufficient. It is therefore useful to set a
convergence precision threshold, where a sufficiently good approximation is assumed when the
difference between the new approximation and the previous approximation falls below a threshold
value:
𝑓(π‘₯𝑛 ) − 𝑓(π‘₯𝑛−1 ) < π‘ π‘œπ‘šπ‘’ π‘‘β„Žπ‘Ÿπ‘’π‘ β„Žπ‘œπ‘™π‘‘ π‘£π‘Žπ‘™π‘’π‘’
We will consider this threshold value to be 0.0001.
Finding the real roots
3. Using the Newton-Raphson numerical method, determine the real root(s) of the following
polynomials:
π‘₯ 3 − 2π‘₯ 2 − 25π‘₯ + 50 = 0
π‘₯ 3 − 8π‘₯ 2 + 19π‘₯ − 12 = 0
Indicate your initial guesses and the subsequent iterations to satisfy convergence. Include a graph of
each function. You may want to make different tabs for the different formulas.
4. Use different techniques of factoring the polynomial to find the roots by hand. Do your results
match? Is a threshold of 0.0001 good enough? If not, what threshold would you choose?
Area under a curve: Riemann summation
Finding the area under a curve is particularly important in science; for example, determining the area
of a signal in a 1H NMR spectrum to obtain an integral value, and therefore the relative number of
equivalent protons in one particular environment with respect to another.
A common method for approximating this area uses Riemann summation, more commonly referred
to as “the Trapezium Rule”, which is explained as follows:
The area of any trapezium can be calculated from the lengths of the parallel sides, and the distance
between them, such that:
For any function, y = f(x), the area under the curve can be approximated as if it were comprised of
many trazepia of width x, whose areas are equal to
Δπ‘₯
(𝑓(π‘₯𝑖 ) +
2
𝑓(π‘₯𝑖+1 ))
If we consider a function as being constructed from n trapezia, then:
Δπ‘₯ =
𝑏=π‘₯𝑛
∫
π‘Ž=π‘₯0
𝑓(π‘₯)𝑑π‘₯ ≈
𝑏−π‘Ž
,
𝑛
and:
Δπ‘₯
(𝑓(π‘₯0 ) + 𝑓(π‘₯1 ) + 𝑓(π‘₯1 ) + β‹― + 𝑓(π‘₯𝑛−2 ) + 𝑓(π‘₯𝑛−2 ) + 𝑓(π‘₯𝑛−1 ) + 𝑓(π‘₯𝑛−1 )
2
+ 𝑓(π‘₯𝑛 ))
Noting that all the inner terms are duplicated, the equation can be rewritten:
𝑖=𝑏
𝑏
Δπ‘₯
∫ 𝑓(π‘₯)𝑑π‘₯ ≈
∑(2𝑓(π‘₯𝑖 ) − 𝑓(π‘₯π‘Ž ) − 𝑓(π‘₯𝑏 ))
2
π‘Ž
𝑖=π‘Ž
𝑖=𝑏
= Δπ‘₯ [∑ 𝑓(π‘₯𝑖 ) −
𝑖=π‘Ž
𝑓(π‘₯π‘Ž ) + 𝑓(π‘₯𝑏 )
]
2
i.e. the total area can be approximated by summing all
the values of the function between the limits,
subtracting half the values at the limits, then multiplying
this value by the width between data-points. Rather than
laboriously calculating each value, the data can be
computed in Excel.
In this task, you are going to apply the Trapezium Rule to
approximate the area under a curve, and display a graph
of both the function and the cumulative (running) total
for the integral (see example, right).
Open the tab labelled “Trapezium Rule”. You are provided with data for a function, 𝑓((π‘₯))
5. Calculate the area under the curve between x = 0.00 and x = 3.00 to two decimal places.
6. Display the results graphically for both the function and the cumulative (running) total for the
integral. To graph the cumulative integral of the function, compute the area of each individual
trapezium, then create a column that keeps a running total based upon the total area up to
that point, namely:
new total area = sum of the previous total area + area of next trapezium
To display the second function on the same graph, Right-click on the existing scatter plot and click
“Select Data.” In the pop-up window, you need to add a new Legend Entry (Series), selecting the
relevant x and y values. When displaying the data, remember to include a legend to indicate which
line is which.
Hermite Polynomials
In vibrational spectroscopy the harmonic approximation, where we treat the potential energy as a
parabolic curve, is very important. This harmonic approximation allows us the treat the vibrations in
a molecule as a quantum mechanical mass on a spring. The potential energy can then be described
by:
1
𝑉 = π‘˜π‘“ π‘₯ 2 π‘€β„Žπ‘’π‘Ÿπ‘’ π‘˜π‘“ 𝑖𝑠 π‘‘β„Žπ‘’ π‘“π‘œπ‘Ÿπ‘π‘’ π‘π‘œπ‘›π‘ π‘‘π‘Žπ‘›π‘‘
2
The Schrödinger equation then becomes:
−
ℏ2 𝑑2 ψ 1
+ π‘˜ π‘₯2ψ = 𝐸 ψ
2π‘š 𝑑π‘₯ 2 2 𝑓
For now is not expected that this makes total sense, this information is here to give you some
background information and explain why skills in excel can be so useful. Can’t wait to learn about
this? You can find more explanation on quantum mechanics and the harmonic oscillator in Atkins’
Physical Chemistry, chapter 8.
The wavefunctions that form the solution to this Schrödinger equation are all of the following form:
ψ𝑛 (π‘₯) = β„Žπ‘’π‘Ÿπ‘šπ‘–π‘‘π‘’ π‘π‘œπ‘™π‘¦π‘›π‘œπ‘šπ‘–π‘Žπ‘™ × πΊπ‘Žπ‘’π‘ π‘ π‘–π‘Žπ‘›
1/4
= 𝐻𝑛
2
(𝑦)𝑒 −𝑦 /2
π‘₯
ℏ2
π‘€β„Žπ‘’π‘Ÿπ‘’ 𝑦 = , α = (
)
𝛼
π‘šπ‘˜π‘“
𝐻𝑛 (𝑦), is a Hermite polynomial. You can find the first couple of Hermite polynomials below.
7. Plot the first 5 Hermite polynomials in the same graph, values for m, kf are already given in
the spreadsheet. Let x run from -1 Å to 1Å. Which of them are odd functions and which are
even functions?
Hermite polynomials are so called orthogonal polynomials, which makes the solutions to the
Schrödinger equations also orthogonal, as should be. Two (wave)functions are said to be orthogonal
if:
∞
∫ ψ∗𝑛 ψπ‘š 𝑑π‘₯ = 0 π‘€β„Žπ‘’π‘› π‘š ≠ 𝑛
−∞
Here the * indicates that we take the complex conjugate. For now our functions are all real, so we
don’t have to worry about this.
8. Use the trapezium rule to show that ψ2 and ψ 3 are indeed orthogonal to each other. As we
can’t really go to infinity, you need to select an appropriate range for the x-value.
9. Can you also work out the integral by hand? What is the difference with the value you get from
the excel calculation? Can you explain this difference?
Assignment 2: Identification of IR and Raman Spectra
In this practical you will work with real spectral data and handle the data using the program
Spectragryph, which is available under the chemistry folder in the start menu or if you work on your
own computer can be downloaded from the Nestor course homepage (do not download it from the
official Spectragryph site as you will need to request a license etc.).
The spectra are provided in their native format so cannot be opened directly in excel. You need first
to open the spectra in the program spectragryph and then extract the necessary data The data for
each exercise can be found in the sub folder with the exercise number (Set 1-3)
Building a spectral database of compounds with FTIR.
Open the spectra in folder ‘set 1’ in Spectragryph.
When preparing a spectral database it is important to correct for spectral errors such as a poor
baseline. It is essential however that all changes are made to an absorbance spectrum and not the
transmittance spectrum. For example:
Drag and drop a spectrum into Spectragryph and select the y-axis dropdown as shown below
Select Absorbance to convert from transmittance to absorbance. Then go to the ‘process’ tab as
shown below and select simple baseline.
Click on a point on the baseline where there are no bands e.g. at 2200 cm-1
Then save the spectrum as a jcamp.dx format (this is an industry wide format). You can do this for all
the spectra in one go
First convert to absorbance
Then select ‘simple baseline’ and immediately click at a peak free point on the spectra e.g. 2400 cm-1
in this case.
You can save these spectra as a series of processed spectra
Make a new folder: and then carry out a batch export as shown
Tick the legends as filenames checkbox!
1. Open the ‘unknown spectra’ folder and use this spectral data base to identify the unknown
compounds by comparison. It is faster if you first look at the unknown spectrum and identify
what functional groups are present.
2. Open the spectrum mixture of ‘three solvents’ and work out by comparison and band
assignment what the three solvents are.
Identification of impurity in a bottle of acetonitrile
There are a sets of Raman spectra of common compounds shown in ‘set 2’. The names of the
compounds are given on the label.
We haven’t done any Raman theory etc yet so for now threat the spectra as upside-down IR spectra,
with sometimes different band intensities than you would expect.
3. Use these spectra to identify the contaminant in the acetonitrile labelled ‘contaminated
acetonitrile)
Identification of the solvents present in a mixture
There are three FTIR spectra of mixtures of common compounds in ‘set 3’.
4. Use the database of solvents and chemical intuition to work out which solvents make up the
mixtures responsible for each of the three spectra.
Assignment 3: Planck’s Law
Open the file ‘Planck's Law.ipynb’ using Google Colab or Jupyter Notebook. Follow the instructions in
the notebook.
References
1 Craig D. Campbell, Zoe M. Smallwood, and Malcolm I. Stewart, Journal of Chemical
Education 2020 97 (9), 2635-2642
Download