2 fitting

Upload: said-maabed

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 2 Fitting

    1/21

    Computational Approach to

    Materials Science and Engineering

    Prita Pant and M. P. Gururajan

    October, 2012

    Copyright c 2012, Prita Pant and M P Gururajan. Permissionis granted to copy, distribute and/or modify this document underthe terms of the GNU Free Documentation License, Version 1.3or any later version published by the Free Software Foundation;with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the sectionentitled GNU Free Documentation License.

    1

  • 8/10/2019 2 Fitting

    2/21

    Module: Fitting

    1 Pre-requisites

    The modules in Part II of this course material.

    2 Learning goals

    To be able to fit a given data to a given polynomial and plot it.

    3 Fitting

    In this module, we use the term fiting to describe the process of findingout the best function that describes the given data. There are more than oneway of defining what the best function is, and different definitions can leadto different results. However, we do not discuss these details here. Further,we also do not discuss the mathematical ideas behind the notion of designmatrix. We refer the interested reader to books on linear regression.

    4 Lattice parameter variations with compo-sition: linear fit

    The lattice parameter of a material changes with composition as well as withtemperature. Generally, for small changes in composition, the change inlattice parameter is assumed to be linear. This is known as Vegards law.

    In Table 1 we show the data on the variation of lattice parameter in the

    Si-Ge system. We want to know if we can fit the data to a linear function; ifso, we also would like to know what the slope and intercept of the linear fitis.

    In the GNU Octavescript given below, we show how to carry out the linearfitting. In addition, after fitting, the fit is also plotted along with the ex-perimental data and the plot is shown in Fig. 1 below. The fitting exercise

    2

  • 8/10/2019 2 Fitting

    3/21

    Mol. % of silicon Lattice constant Density

    100 5.434 2.32887.4 5.454 2.7285.8 5.461 2.8075.7 5.473 3.0357.5 5.518 3.6244.3 5.549 3.9522.9 5.593 4.7015.0 5.620 4.8613.5 5.613 4.89

    12.6 5.626 0 5.657 5.323

    Table 1: The variation of density and lattice parameter with composition inSiGe alloys. The data is taken from Some properties of Germanium-Siliconalloys, E. R. Johnson and S. M. Christian, Phys. Rev., 95, pp. 560-561,1954.

    3

  • 8/10/2019 2 Fitting

    4/21

    indicates that the lattice parameter decreases by about 0.0022345 Angstroms

    for every mol % of Silicon; further, the fit gives the lattice parameter for 0mol % of silicon as 5.6498279 while the experimentally observed value is 5.657(indicating that the fit is quite good).

    # codes/LinearFitVegardsLaw.oct

    #

    # Copyright (C) 2011 Prita Pant and M P Gururajan

    #

    # This program is free software; you can redistribute it and/or modify

    # it under the terms of GNU General Public License as published by

    # the Free Software Foundation; either version 3 of the License, or (at# your option) any later version.

    #

    # This program is distributed in the hope that it will be useful, but

    # WITHOUT ANY WARRANTY; without even the implied warranty of

    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

    # General Public License for more details.

    #

    # You should have received a copy of the GNU General Public License

    # along with this program; if not, write to the Free Software Foundation,

    # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

    #

    #

    % Load the given data from the data file

    X = load("data.dat");

    % Read the first column of the data as composition

    comp = X(:,1);

    % Read the second column of the data as lattice parameter

    a = X(:,2);

    % Use polyfit to fit the data in the least square sense; note that the

    % third parameter, which is unity, is what tells Octave that we are

    % looking for a linear fit to the dataz = polyfit(comp,a,1);

    % Use the fit to generate data at equal intervals

    fitx = 0:10:100;

    fity = polyval(z,fitx);

    % Plot the experimental data and the fit

    4

  • 8/10/2019 2 Fitting

    5/21

    5.4

    5.45

    5.5

    5.55

    5.6

    5.65

    5.7

    0 20 40 60 80 100

    Latticeparamter(inAngstroms)

    Si (in mol%)

    Composition versus lattice parameter plot

    Figure 1: The linear least squares fit for the experimental data on latticeparameter variation with composition in Si-Ge alloy.

    plot(comp,a,o,fitx,fity)

    % Set the axes values

    axis([0 100 5.4 5.7]);

    % Label the axes and title the plot; also make the plot square

    xlabel "Si (in mol%)"

    ylabel "Lattice paramter (in Angstroms)"

    title "Composition versus lattice parameter plot"

    axis(square)

    % Print the plot as an eps file

    print -depsc VegardsLawGOCT.eps

    Note that the GNU Octave command used for fitting is polyfit. There arethree arguments for polyfit. The first is the independent variable (say, x);

    5

  • 8/10/2019 2 Fitting

    6/21

    the second is the dependent variable (say, y); and the third is the order of

    the fit. By choosing the third variable to be unity, we do a linear fit in theleast squares sense. However, by choosing it to be n, we can fit the datato an n-th order polynomial, namely, y = anx

    n +an1xn1 +...+ a1x+ a0

    and the coefficients an-a0 are returned to the user. In the script above, thecoefficient vector is stored as z.

    5 Low temperature specific heat of copper:Polynomial fit

    In the previous section, we discussed polynomial fit for the given data. How-ever, sometimes, the exact form of the polynomial is known, and we wish to fitthe data to the given polynomial and not just to any polynomial. For exam-ple, consider the variation of specific heat with temperature. Depending onthe mechanisms of absorption of heat in a material, the temperature depen-dence of specific heat will be different. In general, at very low temperatures,the specific heat has the following dependence on temperature:Cp= aT+bT

    3

    where aand b are constants. At high temperatures, the specific heat has thefollowing dependence on temperature: Cp = a+ bT + CT

    2 +dT2 wherea, b, c and d are constants. More information on the variation of specific

    heat with temperature is available in Swalin listed in References and furtherreading section.

    In Table 3 we show the data on the variation of specific heat with temperaturefor copper. Note that the data here is for the temperature range 0 to 20 Kwhich is different from the one that was discussed in the plotting module.Since the temperature range in question is 0 to 20 K, it can be consideredas the low temperature regime for copper. Unlike in the earleir case, sincewe know that at low temperatures, the variation of the specific heat withtemperature is polynomial (given by the polynomial Cp = aT + bT

    3), wewant to fit the data to this given polynomial and identify the constants a

    and b. In this section, we will show how to carry out such a polynomialfitting to the given data.

    The following GNU Octavescript fits the given specific heat data to a poly-nomial of the type aT+bT3.

    6

  • 8/10/2019 2 Fitting

    7/21

    Temperature Specific Heat

    1 7.43x104

    2 1.77x103

    3 3.37x103

    4 5.82x103

    5 9.43x103

    6 1.45x102

    7 2.13x102

    8 3.01x102

    9 4.14x102

    10 5.55x102

    12 9.36x102

    14 1.49x10116 2.25x101

    18 3.28x101

    20 4.62x101

    Table 2: The variation of specific heat with temperature in copper in thelow temperature regime (0 20K). The data is taken from Heat capacity ofreference materials: Cu and W, G. K. White and S. J. Collocott, J. Phys.Chem. Ref. Data, 13, pp. 1251-1257, 1984.

    7

  • 8/10/2019 2 Fitting

    8/21

    % Load the specific heat data

    S = load("CuSpHeatData.dat");

    % Get the temperature and specific heat from the data file

    T = S(:,1);

    Cp = S(:,2);

    % Plot the experimental data with open circles

    plot(T,Cp,o)hold on

    % Generate the design matrix

    X = [T T.*T.*T];

    % Calculate the coefficients a and b which are consistent with the given data

    a = X\Cp

    % Using the obtained a and b values, generate data points

    t = (0:1.:25);

    Y = [t t.*t.*t]*a;

    % Plot the fit

    plot(t,Y,.-)

    % Label the axes

    xlabel ("Temperature (in K)");

    ylabel ("Specific heat (in J/mol/K)");

    % Make the plot square and save the output file

    8

  • 8/10/2019 2 Fitting

    9/21

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    0 5 10 15 20 25

    Figure 2: The polynomial fit for the experimental data on specific heat vari-ation with temperature in copper at low temperatures (in the range 0-20).

    axis(square)

    print -depsc CuSpHeatFit.eps

    From this script, it is clear that the given data fits the polynomial Cp =2.2460 1004T+ 5.7411 1005T3. The plot generated using this scriptis shown in Fig. 2.

    Note the use of the so-called design matrix; the design matrix is a matrixin which for each data point, the independent variable is put in the required

    polynomial form, and knowing the value of the dependent variable, the un-known coefficients for the polynomial are evaluated. The solution of thismatrix equation gives us the coefficients that is consistent with the givendata.

    9

  • 8/10/2019 2 Fitting

    10/21

    6 Self-assessment questions

    1. What is the GNU Octave command for fitting a given data to a poly-nomial of the type y=a3x

    3 +a2x2 +a1x+a0?

    2. What is the GNU Octavecommand to calculate values using a given fit(that is obtained using polyfit)?

    3. To fit a given data to the polynomialy = a + bx5 one can use polyfit.True or false?

    7 Answers to self-assessment questions

    1. polyfit(x,y,3)

    2. polyval

    3. False. One has to use design matrix and solve the set of resultantequations to obtain such a fit.

    8 Exercises Problem 1. Linear fit

    Consider the consumption of copper as a function of temperature in areaction. The data is as shown in Table 3. Assume that the consump-tion is related to temperature through an Arrhenius type of reaction,namely, h = A exp Q

    RTwhere R is the universal gas cosntant, T is

    the absolute temperature, Q is the activation energy for the reaction,h is the copper consumption (in microns), A is the pre-exponentialconstant. From the given data, estimate Q and A.

    Problem 2. Polynomial fit

    The data for linear thermal expansion of cubic BN is given in Table. 4.Fit the data to a polynomial and plot the data and the fit.

    10

  • 8/10/2019 2 Fitting

    11/21

    Temperature (in K) h (in microns)

    473 0.26493 0.35513 0.42

    Table 3: The copper consumption in a reaction as a function of time. Thedata is taken fromKinetic analysis of the soldering reaction between eutectic

    SnPb alloy and Cu accompanied by ripening , H K Kim and K N Tu, Phys.Rev. B, 53, pp. 16027-16034, 1996.

    Temperature (in K) Linear thermal expansion aa0 103

    77 -0.380.05200 - 0.280.05

    297 0.00535 +0.440.14668 +0.860.14785 +1.4580.14889 +2.020.141008 +2.660.141061 +2.960.141137 +3.600.141205 +3.900.141289 +4.400.14

    Table 4: The variation of linear thermal expanion with temperature in BNin the temperature regime (77 1289 K). The data is taken from Thermalexpansion of some diamondlike crystals, G. A. Slack and S. F. Bartram, J.Appl. Phys.,46, pp. 89-98, 1975.

    11

  • 8/10/2019 2 Fitting

    12/21

    Temperature Specific Heat

    300 24.45350 24.88400 25.25450 25.61500 25.91550 26.21600 26.48

    650 26.73700 26.99800 27.48900 28.041000 28.661100 29.481200 30.531250 31.121300 32.16

    Table 5: The variation of specific heat with temperature in copper in the lowtemperature regime (300 1300 K). The data is taken from Heat capacity ofreference materials: Cu and W, G. K. White and S. J. Collocott, J. Phys.Chem. Ref. Data, 13, pp. 1251-1257, 1984.

    Problem 3. Non-linear, polynomial fitGiven below is the data forspecific heat of copper at constant pressure in the temperature range300 1300. It is known that in this range, the specific heat of copper isdescribed by the polynomial Cp(T) = 22.61 + 6.27 103TVerify thatthis indeed is so.

    9 Solution to exercises

    Solution to Problem 1.

    12

  • 8/10/2019 2 Fitting

    13/21

    Consider the expression: h = A exp QRT

    Taking logarithm on both

    sides, one obtains, log (h) = l og(A) QRT Thus, one can see thatthe plot of (1/T) versus log(h) is a straight line. The slope of thecurve, multiplied by R, the unviersal gas constant gives the activationenergy (in J/mol) and the intercept value, when exponentiated givesthe pre-exponential constant A. TheGNU Octavescript below does thisArrhenius plot as well as fitting. By executing the script, we calculatethe activation energy to be 24.252 kJ/mol and the pre-exponential con-stant to be 125.87 microns. The Arrhenius plot that we have fit thedata to, along with the data is shown in Fig. 3.

    # CuConsumption.oct#

    # Copyright (C) 2011 Prita Pant and M P Gururajan

    #

    # This program is free software; you can redistribute it and/or modify

    # it under the terms of GNU General Public License as published by

    # the Free Software Foundation; either version 3 of the License, or (at

    # your option) any later version.

    #

    # This program is distributed in the hope that it will be useful, but

    # WITHOUT ANY WARRANTY; without even the implied warranty of

    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

    # General Public License for more details.

    #

    # You should have received a copy of the GNU General Public License

    # along with this program; if not, write to the Free Software Foundation,

    # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

    #

    #

    % Load the given data from the data file

    X = load("CuConsumptionData.dat");

    % Read the first column of the data as temperatureT = X(:,1);

    % Read the second column of the data as dh

    dh = X(:,2);

    % Make the relevant x and y parameters; we would like to plot the logarith

    % of dh as a function of (1/T).

    13

  • 8/10/2019 2 Fitting

    14/21

    x = 1./T;

    y = log(dh);% Use polyfit to fit the data in the least square sense; note that the

    % third parameter, which is unity, is what tells Octave that we are

    % looking for a linear fit to the data

    z = polyfit(x,y,1);

    % Use the fit to generate data at equal intervals

    fitx = 0.0018:0.0001:0.0022;

    fity = polyval(z,fitx);

    % Plot the experimental data and the fit

    plot(x,y,o,fitx,fity)

    % Set the axes valuesaxis([0.0018 0.0022 -1.5 0]);

    % Label the axes and title the plot; also make the plot square

    xlabel "1/T"

    ylabel "ln(dh)"

    title "Arrhenius plot"

    axis(square)

    % Print the plot as an eps file

    print -depsc CuArrheniusPlot.eps

    % From the linear fit data, one can now calculate the activation energy

    % in J/mol and the pre-exponential constant

    z(1)*8.314

    exp(z(2))

    Solution to Problem 2.

    # ThermalExpansionBN.oct

    #

    # Copyright (C) 2011 Prita Pant and M P Gururajan#

    # This program is free software; you can redistribute it and/or modify

    # it under the terms of GNU General Public License as published by

    # the Free Software Foundation; either version 3 of the License, or (at

    # your option) any later version.

    14

  • 8/10/2019 2 Fitting

    15/21

    -1.4

    -1.2

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.0018 0.00185 0.0019 0.00195 0.002 0.00205 0.0021 0.00215 0.0022

    ln(dh)

    1/T

    Arrhenius plot

    Figure 3: Arrhenius plot for the consumption of copper in a reaction. Fromthis plot, one can obtain the activation in energy (in units of R, the universalgas cosntant) and the pre-exponential constant.

    15

  • 8/10/2019 2 Fitting

    16/21

    #

    # This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of

    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

    # General Public License for more details.

    #

    # You should have received a copy of the GNU General Public License

    # along with this program; if not, write to the Free Software Foundation,

    # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

    #

    #

    % Load the given data from the data fileX = load("BNalpha.dat");

    % Read the first column of the data as temperature

    T = X(:,1);

    % Read the second column of the data as linear thermal expansion (epsilon)

    epsilon = X(:,2);

    epsilon = epsilon.*1000;

    % Use polyfit to fit the data in the least square sense; note that the

    % third parameter, which is unity, is what tells Octave that we are

    % looking for a linear fit to the data

    z = polyfit(T,epsilon,2);

    % Use the fit to generate data at equal intervals

    fitx = 0:10:1300;

    fity = polyval(z,fitx);

    % Plot the experimental data and the fit

    plot(T,epsilon,o,fitx,fity)

    % Set the axes values

    axis([0 1300 -1000 5000]);

    % Label the axes and title the plot; also make the plot square

    xlabel "Temperature (in K)"

    ylabel " Linear thermal expansion"

    title "Temperature verus linear thermal expansion plot"axis(square)

    % Print the plot as an eps file

    print -depsc ThermalExpansionBN.eps

    16

  • 8/10/2019 2 Fitting

    17/21

    -1000

    0

    1000

    2000

    3000

    4000

    5000

    0 200 400 600 800 1000 1200

    Linearthermalexpansion

    Temperature (in K)

    Temperature verus linear thermal expansion plot

    Figure 4: The polynomial fit for the experimental data on linear thermalexpansion variation with temperature in Boron Nitride in the temperaturerange 771289 K.

    17

  • 8/10/2019 2 Fitting

    18/21

    Solution to Problem 3.

    Using the script below, we see that the data can be fit to the polynomialCp(T) = 22.342 + 6.8475 10

    3Tas opposed to the given expression,namely,Cp(T) = 22.6 1 + 6.27 103TThus, we see that the given datadoes fit to the known Cp expression quite well.

    # Solution3.oct

    #

    # Copyright (C) 2011 Prita Pant and M P Gururajan

    #

    # This program is free software; you can redistribute it and/or modify

    # it under the terms of GNU General Public License as published by

    # the Free Software Foundation; either version 3 of the License, or (at

    # your option) any later version.

    #

    # This program is distributed in the hope that it will be useful, but

    # WITHOUT ANY WARRANTY; without even the implied warranty of

    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

    # General Public License for more details.

    ## You should have received a copy of the GNU General Public License

    # along with this program; if not, write to the Free Software Foundation,

    # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

    #

    #

    % Load the specific heat data

    S = load("CuCpHighTVariation.dat");

    % Get the temperature and specific heat from the data file

    T = S(:,1);

    Cp = S(:,2);

    % Plot the experimental data with open circles

    18

  • 8/10/2019 2 Fitting

    19/21

    plot(T,Cp,o)hold on

    % Generate the design matrix

    X = [ones(size(T)) T];

    % Calculate the coefficients a and b which are consistent with the given d

    a = X\Cp

    % Using the obtained a and b values, generate data points

    t = (300:1.:1300.);

    Y = [ones(size(t)) t]*a;

    % Plot the fit

    plot(t,Y,.-)

    % Label the axes

    xlabel ("Temperature (in K)");

    ylabel ("Specific heat (in J/mol/K)");

    % Make the plot square and save the output file

    axis(square)

    print -depsc CuHighTSpHeatFit.eps

    10 References and further reading

    1. Thermodynamics of solids, R. A. Swalin, 2nd edition, Wiley-Interscience

    19

  • 8/10/2019 2 Fitting

    20/21

    24

    26

    28

    30

    32

    34

    200 400 600 800 1000 1200 1400

    Specificheat(inJ/mol/K)

    Temperature (in K)

    Figure 5: The polynomial fit for the experimental data on specific heat vari-ation with temperature in copper at temeperatures in the range 300-1300K).

    20

  • 8/10/2019 2 Fitting

    21/21

    publishers, 1972.

    2. Advanced Engineering Mathematics, E. Kreyszig, 8th edition, John Wi-ley and Sons, 1999.

    3. Elementary Numerical Analysis, K. E. Atkinson, 3rd edition, WileyIndia, 2003.

    21