MSE 104L laboratory 3 exercisesĀ¶

Authors: Enze Chen (University of California, Berkeley)

Note

This is an interactive exercise, so you will want to click the and open the notebook in DataHub (or Colab for non-UCB students).

This notebook contains a series of exercises to help you process your data from Lab 3. It doesnā€™t answer all of the discussion questions in the lab procedures, but it will help you create some figures that can supplement the narrative of your lab report.

This lab doesn't have a whole lot of new MSE material (from a conceptual standpoint), so we'll try to give some Python tips/challenges. You will not be assessed on any of the coding material here, but we hope you'll give it a shot and have some fun.

ContentsĀ¶

The exercises in this notebook include:

  1. Plotting refresher

  2. Writing functions

  3. Vegardā€™s law

Plot a spectra as a sanity checkĀ¶

Back to top

Just like Lab 2, weā€™ll start with a plot of one of the spectra. You know the drill!

Of course, if you donā€™t remember what you did in lab 2, you can always reference your completed notebooks in JupyterHub. We hope plotting in Python feels smooth at this point, whether itā€™s from muscle memory or referencing templates.

Write a function to calculate the lattice constantĀ¶

Back to top

In the previous lab, you had to calculate the lattice constant and apply the Nelson-Riley method for your samples with cubic crystal structure. Well, since Cuā€“Ni forms an FCC solid solution, you have the great privilege of repeating what you did for each of your samples here. Of course, if you did the programming solution last time, youā€™ll have no problem with this part of the lab. šŸ˜ There are two ways to go about this:

  1. (Works) You can copy-paste the individual code sections from the previous lab into a single code block and just run it repeatedly, with different peak positions (corresponding to different Cuā€“Ni samples), to get lattice constants through Nelson-Riley.

  2. (Slightly snazzier?) You can do something fancier and write a function that makes things look cleaner as you repeat the steps. Recall that functions in Python look like:

def function_name(args):
       # do something

Below is an example of what we mean. If you donā€™t like the functions approach, you can delete the code (or create a new cell) and do Method 1 instead.

# -------------   WRITE YOUR CODE IN THE SPACE BELOW   ---------- #
import numpy as np
import pandas as pd
# add more imports here

def df_calcs(angles, wavelength):
    # in your function, this would be the operations to calculate the lattice constant
    df = pd.DataFrame({'angles':angles})
    df['sub'] = df['angles'] - 1
    df['d'] = np.sqrt(df['sub'])
    df['s'] = [3, 4, 6, 12]
    df['a'] = df['d'] * df['s']
    return df

def make_nr(dataframe):
    # make a similar function that makes NR plots based on the DataFrame!
    pass
    
    
# ------------------------------------------------------------
# change the inputs below and call the function - voila!
data = [17.02, 10.0, 4.99, 2.01]
wl = None

df = df_calcs(data, wl)
display(df)

make_nr(df)
angles sub d s a
0 17.02 16.02 4.002499 3 12.007498
1 10.00 9.00 3.000000 4 12.000000
2 4.99 3.99 1.997498 6 11.984991
3 2.01 1.01 1.004988 12 12.059851

Vegardā€™s lawĀ¶

Back to top

Recall that in the pre-lab you learned about Vegardā€™s law (which is pretty remarkable!) that says:

\[ a_{\text{A-B}} = a_{\text{A}} x_{\text{A}} + a_{\text{B}} x_{\text{B}} \]

where \(a_M\) and \(x_M\) are the lattice constant and concentration, respectively, of species \(M\). For the binary case where this applies, note \(x_{\text{B}} = 1 - x_{\text{A}}\).

Anyways, youā€™ll do the Nelson-Riley calculation above and get a few values of \(a\) corresponding to different concentrations \(x\). You then have to plot this and comment on whether Vegardā€™s law is obeyed. There are several ways to answer this question, and we encourage you to be creative. Here are just two suggestions, one based on something youā€™ve already seen, and another based on something that you have probably also seen in other contexts.

  1. Like with Nelson-Riley plots, you can compute a line of best fit, plot that line, and look at the value of \(a\) at the endpoints. Do they agree with the experimental lattice constants of the pure metals? Can you quantify the amount of error?

  2. You can compute a linear correlation coefficient (also known as Pearsonā€™s \(r\)) between your values of \(a\) and \(x\). Pearsonā€™s \(r\) can be expressed mathematically as:

\[ r_{X, Y} = \frac{\text{covariance}(X, Y)}{\sigma(X) \sigma(Y)} \]

where \(\sigma(\cdot)\) is the standard deviation, and values of \(r\) range from \([-1, +1]\) as follows:

pearson

Please note: A friendly reminder that \(r\) has no relationship to the orange line of best fit (it is not the slope!), but rather is an estimate of the linearity between the green data points. The orange line is merely a guide to the eye.

Computing \(r\)Ā¶

Once again, NumPy makes our lives pretty easy by handing to us the np.corrcoef() function. This function takes in x and y arrays and returns their correlation coefficient as a matrix (which element should we choose?). It should take only a few lines of codeā€”see if you can get it working!

# -------------   WRITE YOUR CODE IN THE SPACE BELOW   ---------- #

ConclusionĀ¶

Back to top

This concludes the programming exercises for Lab 3. Congratulations! We hope youā€™re proud of the plots that you generated and we wish you luck with the lab writeup. šŸ“