Module 5 - NPTel

27 downloads 330 Views 78KB Size Report
Computational Approach to. Materials Science and Engineering. Prita Pant and M. P. Gururajan. October, 2012. Copyright c 2012, Prita Pant and M P Gururajan.
Computational Approach to Materials Science and Engineering Prita Pant and M. P. Gururajan October, 2012

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

1

Module: Numerical Integration 1

Pre-requisites

The modules in Part II of this course material.

2

Learning goals • Given a set of data, to numerically integrate the data.

3

Gibbs free energy from specific heat data

The Gibbs free energy of a material is defined as G = H − T S where H is the enthalpy, T is the absolute temperature and S is the entropy. The Gibbs free energy is a very important thermodynamic quantity in the study of materials. For example, it plays a key role in identifying whether a material will undergo phase transformations or not. In a material, if as a function of temperature one can measure the specific heat at constant pressure, then, the enthalpy and entropy can be obtained RT R T Cp by the following formulae: H = H0 + 0 Cp dT and S = 0 T dT , where H0 is the enthalpy at zero Kelvin. Suppose, we are given the specific heat at constant pressure as a function of temeprature, say, as in Table. 1. Suppose we wish to calculate the change in specific heat and the change in entropy in heating copper from 300 to 1300 K. This can be achieved by numerically integrating the data as shown in the script below. Note that we use the command trapz to numerically integrate the given discrete data. # # # # # #

CuHandS.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 2

Temperature

Specific Heat

300 350 400 450 500 550 600 650 700 800 900 1000 1100 1200 1250 1300

24.45 24.88 25.25 25.61 25.91 26.21 26.48 26.73 26.99 27.48 28.04 28.66 29.48 30.53 31.12 32.16

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

3

# 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("CuCpHighTVariation.dat"); % Numerically integrate the Cp with T trapz(X(:,1),X(:,2)) % Let us generate the Cp/T data N = size(X(:,1)) for i=1:16 Z(i) = X(i,2)/X(i,1); endfor Z = Z’; % Let us now integrate Cp/T with T trapz(X(:,1),Z) From this script, we find that the ∆H = 27.705 kJ/mol and ∆S = 39.590 kJ/mol/K. These values can be verified by integrating the fitted polynomial (see Problem 1).

4

Area under the stress-strain curve

Let us consider the stress-strain data for a brass sample, shown in Table. 2. A plot of the data is as shown in Fig. 1. From such a stress-strain plot, one can calculate the total energy absorbed by the material before fracture. This quantity is known as toughness and is an integral of the area under the 4

Stress (in MPa)

Strain

1.91 48.75 93.09 142.25 195.71 245.32 306.3 362.96 405.79 425.84 438.82 468.1 503.18 527.74 544.11 554.38 558.74 59.56 59.65

0.00 0.05 0.09 0.13 0.18 0.23 0.32 0.45 0.66 0.93 1.2 2.47 5.19 7.91 10.63 13.35 16.07 18.79 21.52 Table 2: Tabulation of stress strain data for a brass sample.

curve. In this case, for example, by integrating the area under the curve for all the points except the last two, one can obtain the toughness. The script for calculating the toughness is shown below. The calculated toughness is 8.1707 x 109 J/m3 . If we integrate the curve in the linear regime, that is, before the onset of yield, we obtain the stored elastic energy (see Problem 2).

# Toughness.oct 5

Stress-Strain curve for a brass sample 6e+08

5e+08

Stress (in MPa)

4e+08

3e+08

2e+08

1e+08

0 0

5

10

15

20

25

Strain

Figure 1: The plot of stress-strain data for a brass sample.

6

# # 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("StressStrainBrass.dat"); % Make the stress and strain vectors strain = X(:,2); stress = X(:,1).*1.e6; % Let us leave out the last two points in the necking region N = size(strain); for i=1:N(1)-2 x(i) = strain(i); y(i) = stress(i); endfor % Numerically integrate the area under the stress strain curve trapz(x,y)

5

Evaluating the error function

Consider the error function data given in Table. 3. By definition, erf(z) = Rz √2 exp (−x2 )dx. Thus, one can verify the tabulated values by carrying π 0 7

out the integration numerically. Note that this integration is carried out on a function and not on discrete data as in the earlier cases. Here is the script which will evaluate the error function values for the given z value. % Define the error function function y = f(x) y = 2.*exp(-x*x)/(sqrt(pi)); endfunction % Integrate the function from 0 to the given z value quad("f",0,-2.0) quad("f",0,-1.5) quad("f",0,-1.0) quad("f",0,-0.5) quad("f",0,0.) quad("f",0,0.5) quad("f",0,1.) quad("f",0,1.5) quad("f",0,2.0) There are many different commands to carrying out integration of this type as well as many different ways of indicating the function to the program. Here is another example: quadgk (@(x) exp(-x.*x), 0, 0.5)*2/(sqrt(pi)) In this case, note that the function is given by element-wise opeartion (that is .* instead of *). Here is yet another way: f = inline("exp(-x.*x)*2/(sqrt(pi))") quadl(f,0,0.5) Before we conclude this module, we draw the attention of the reader to the following two points: • Unlike numerical differentiation, numerical integration is more robust; any errors in the data do not interact with the approximation procedure itself. 8

z

erf(z)

-2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0

-0.9953 -0.9661 -0.8427 -0.5205 0.0000 0.5205 0.8427 0.9661 0.9953

Table 3: Tabulation of error function values in the range x = −2 to x = 2. See Materials Science and Engineering: a first course, V Raghavan, Third edition, Prentice-Hall of India Pvt. Ltd, 1995, for example. • The command ‘lsode’ which is used to integrate ODEs is based on numerical integration.

6

Self-assessment questions 1. What is the command in GNU Octave to evaluate the integral numerically given discrete data? 2. Give at least two commands which will carry out the integration of a given function. 3. For quadl, the function is to be given in terms of elementwise. True or false? 4. Should the integrand be given elementwise for quad to carry out the integration? 5. Can quad take the integration limits of ±∞? 9

7

Answers to self-assessment questions 1. trapz 2. quad, quadl 3. True. 4. No. 5. Yes; it can.

8

Exercises • Problem 1. Fit the specific heat data given in Table. 1 to a polynomial and use the fit to calculate the change in entropy and enthalpy. • Problem 2. Calculate the stored elastic energy for the given stressstrain data for brass (Table. 2).

9

Solution to exercises • Solution to Problem 1. # # # # # # # # # # # #

Solution1.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 10

# # # # # # #

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); % 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; % Integrate the data to obtain change in enthalpy trapz(t,Y) % To integrate Cp/T for i=300:1300 Z(i-299) = a(1)/i + a(2); 11

endfor trapz(t’,Z) • Solution to Problem 2. # Solution2.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("StressStrainBrass.dat"); % Make the stress and strain vectors strain = X(:,2); stress = X(:,1).*1.e6; % Let us take the points in the elastic regime for i=1:7 x(i) = strain(i); y(i) = stress(i); endfor % Numerically integrate the area under the stress strain curve trapz(x,y) 12

The answer turns out to be 53.107750 x 106 J/m3 . It is possible to fit the given data to a straight line and carry out the integration. The script below does this and the answer is not too different in this case either. # Solution2a.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("StressStrainBrass.dat"); % Make the stress and strain vectors strain = X(:,2); stress = X(:,1).*1.e6; % Let us take the points in the elastic regime for i=1:7 x(i) = strain(i); y(i) = stress(i); endfor z = polyfit(x,y,1); fitx = 0:0.01:0.32; fity = polyval(z,fitx); plot(x,y,’o’,fitx,fity,’r’) 13

% Numerically integrate the area under the stress strain curve trapz(fitx,fity)

10

References and further reading

1. Numerical recipes in C: the art of scientific computing, W. H. Press, S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery, Second edition, Cambridge University Press, 1992. 2. Advanced Engineering Mathematics, E. Kreyszig, 8th edition, John Wiley and Sons, 1999. 3. Elementary Numerical Analysis, K. E. Atkinson, 3rd edition, Wiley India, 2003. 4. Introduction to Materials Science and Engineering, V. Raghavan, 5th edition, Prentice-Hall India Pvt. Limited, 2004.

14