LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab....

10
LAB 1: MATLAB - Introduction to Programming Objective: The objective of this laboratory is to review how to use MATLAB as a programming tool and to review a classic analytical solution to a steady-state two-dimensional heat transfer conduction problem. We will also review how to write a program that is well documented and easy to read. Background: The steady-state, two-dimensional temperature distribution, , in a solid with constant thermal conductivity can be found analytically by solving a homogeneous, 2 nd order partial differential equation (PDE) known as the heat diffusion equation (or Laplace’s Equation) (1) using the Separation of Variables method. An introduction to the method can be found in Bergman, et al., Fundamentals of Heat Transfer, 8 th ed., Wiley, pp 211-215. Another excellent reference for analytical solutions to conduction problems is Carslaw and Jaeger, Conduction of Heat in Solids, Oxford University Press, 1959. An analytical solution to Equation (1) is possible if the boundaries of the domain correspond to orthogonal coordinates, such as for the rectangle in cartesian coordinates shown in Figure 1. Furthermore, a solution can be obtained when (i) one direction of the problem is expressed by a homogeneous ordinary differential equation (ODE) subject to homogeneous boundary conditions, (ii) the other direction is expressed by a homogenous ODE subject to one homogeneous and one non-homogeneous boundary condition, and (iii) the sign of the separation variable is chosen such that the boundary-value problem of the homogeneous direction leads to a characteristic-value problem. We are interested in analytical solutions in this class because they are useful for validating a numerical method (or making sure our approximate numerical calculations are accurate). Figure 1. Schematic of two-dimensional domain and boundary conditions for conduction heat transfer. Tx, y ( ) 2 T x 2 + 2 T y 2 = 0 x y x = L x y = L y T (y = 0) = T 1 T (y = L y ) = T 2 T (x = L x ) = T 1 T (x = 0) = T 1

Transcript of LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab....

Page 1: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

LAB 1: MATLAB - Introduction to Programming Objective: The objective of this laboratory is to review how to use MATLAB as a programming tool and to review a classic analytical solution to a steady-state two-dimensional heat transfer conduction problem. We will also review how to write a program that is well documented and easy to read. Background: The steady-state, two-dimensional temperature distribution, , in a solid with constant thermal conductivity can be found analytically by solving a homogeneous, 2nd order partial differential equation (PDE) known as the heat diffusion equation (or Laplace’s Equation)

(1) using the Separation of Variables method. An introduction to the method can be found in Bergman, et al., Fundamentals of Heat Transfer, 8th ed., Wiley, pp 211-215. Another excellent reference for analytical solutions to conduction problems is Carslaw and Jaeger, Conduction of Heat in Solids, Oxford University Press, 1959. An analytical solution to Equation (1) is possible if the boundaries of the domain correspond to orthogonal coordinates, such as for the rectangle in cartesian coordinates shown in Figure 1. Furthermore, a solution can be obtained when (i) one direction of the problem is expressed by a homogeneous ordinary differential equation (ODE) subject to homogeneous boundary conditions, (ii) the other direction is expressed by a homogenous ODE subject to one homogeneous and one non-homogeneous boundary condition, and (iii) the sign of the separation variable is chosen such that the boundary-value problem of the homogeneous direction leads to a characteristic-value problem. We are interested in analytical solutions in this class because they are useful for validating a numerical method (or making sure our approximate numerical calculations are accurate).

Figure 1. Schematic of two-dimensional domain and boundary conditions for conduction heat transfer.

T x, y( )

∂2T∂x2

+ ∂2T∂y2

= 0

x

y

x = Lx

y = Ly

T (y = 0) = T1

T (y = Ly) = T2

T (x = Lx) = T1 T (x = 0) = T1

Page 2: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

2

For our example we will obtain a solution for the boundary conditions also shown in Figure 1 where and are constant temperatures. We begin by defining dimensionless temperature as

. (2)

Substituting this into Equation (1) the heat diffusion equation and boundary conditions become

(3)

(homogeneous) (4) (non-homogenous) (5)

Next, we assume that the solution can be separated into a function that only depends on x and a function that only depends on y given by . (6) Substituting this into Equation (3) we get

(7)

where the partial derivatives become ordinary derivatives because each function only depends on one variable. Dividing Equation (7) by we obtain

(8)

where the separation variable, , must be a constant because the first term can only depend on x and the second term can only depend on y. Also, the negative sign is required by condition (iii) above to get a solution for our specified boundary conditions that are both homogenous in the x-direction. From Equation (8) we can write two separate homogeneous ODE’s

(9)

(10)

T1 T2

θ x,y( ) =T x,y( ) −T1T2 −T1

∂2θ∂x2

+ ∂2θ∂y2

= 0

θ x = 0, y( ) = θ x = Lx , y( ) = θ x, y = 0( ) = 0

θ x, y = Ly( ) = 1X

Y

θ x, y( ) = X x( )Y y( )

Y d2 Xdx2

+X d2 Ydy2

= 0

X Y

1Xd 2 Xdx2

= −1Yd 2 Ydy2

= −λ 2

λ

d 2 Xdx2

+ λ 2 X = 0

d 2 Ydx2

− λ 2 Y = 0

Page 3: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

3

thus, we have reduced the original PDE to a system of two ODE’s with the following solutions (11) . (12) In order to verify that these solutions are valid, substitute Equations (11) and (12) into Equations (9) and (10), respectively. To apply the boundary conditions, we begin with the homogenous ones given by Equation (4) to get the following: (13)

(14)

(15)

From these results, we can get the following solution

(16)

where and are combined into the new constant and the definition for the hyperbolic sine is used. Note, this function is a valid solution for any value of n. Because Equation (3) is linear by superposition the following infinite sum is also a valid solution

. (17)

This form is needed to satisfy the final boundary condition which is imposed by substituting Equation (5) into equation (17) to get

(18)

To solve for we need to write an infinite series expansion in terms of orthogonal functions,

for , that satisfies the following relationship on the domain

X = C1 cos λx( )+C2 sin λx( )

Y = C3e+λ y +C4e

−λ y

θ x = 0, y( ) = C1 Y = 0 → C1 = 0

θ x = Lx , y( ) = C2 sin λ Lx( )Y = 0 → λ = nπLx

for n = 1,2,3,…

θ x, y = 0( ) = C2 sin nπ xLx

⎝⎜⎞

⎠⎟C3 +C4( ) = 0 → C3 = −C4

θ x, y( ) = C2C3 sin nπ xLx

⎝⎜⎞

⎠⎟e+nπ y Lx − e−nπ y Lx( ) = Cn sin nπ x

Lx

⎝⎜⎞

⎠⎟sinh nπ y

Lx

⎝⎜⎞

⎠⎟

C2 C3 Cn

θ x, y( ) = Cn sinnπ xLx

⎝⎜⎞

⎠⎟sinh nπ y

Lx

⎝⎜⎞

⎠⎟n=1

θ x, y = Ly( ) = Cn sinnπ xLx

⎝⎜⎞

⎠⎟sinh

nπ LyLx

⎝⎜

⎠⎟

n=1

∑ = 1

Cngn x( ) n = 1,2,3,… a ≤ x ≤ b

Page 4: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

4

(19)

For this problem, based on Equation (18), the appropriate choice for our nth orthogonal function is on the domain . Also, recall that any function can be expressed in terms of an infinite series of orthogonal functions as

(20)

Again, based on Equation (18) if we can equate Equation (18) and Equation (20) to get

(21)

which allows us to obtain by inspection

(22)

To solve for we multiply Equation (20) by and integrate over the domain

(24)

where by Equation (19) for orthogonal functions the only term in the summation that is not zero is when . Thus, Equation (23) reduces to

. (25)

For our problem where and we get

. (26)

Finally, these results are combined to get the dimensionless temperature distribution

gm x( )gn x( )dxa

b

∫ = 0 for m ≠ n

gn x( ) = sin nπ x Lx( ) 0 ≤ x ≤ Lx

f x( ) = An gn x( )n=1

f x( ) = 1

Cn sinnπ xLx

⎝⎜⎞

⎠⎟sinh

nπ LyLx

⎝⎜

⎠⎟

n=1

∑ = An sinnπ xLx

⎝⎜⎞

⎠⎟n=1

Cn =An

sinh nπ Ly Lx( )An gm x( )

gm x( ) f x( )dxa

b

∫ = gm x( ) An gn x( )dxn=1

∑a

b

m = n

f x( )gn x( )dxa

b

∫ = An gn2 x( )dx

a

b

gn x( ) = sin nπ x Lx( ) f x( ) = 1

An =sin nπ x Lx( )dx

0

Lx∫sin2 nπ x Lx( )dx

0

Lx∫= 2π

−1( )n+1 +1n

Page 5: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

5

. (27)

We can now calculate the heat transfer rates within the domain from our known temperature distribution and Fourier’s Law for conduction heat transfer given by the following for two-dimensional cartesian coordinates

. (28)

Substituting in our dimensionless temperature from Equation (2) this becomes

(29)

Substituting in our solution from Equation (27) we get

. (30)

Note that due to the singularities at the upper left and upper right corners of the domain (where the temperature jumps from to ) the heat flux does not converge to a finite solution. For the bottom boundary at the solution does converge and is given by

. (31)

To obtain the total heat transfer rate we integrate Equation (30) over the bottom area to get

(32)

(33)

θ x, y( ) = 2π−1( )n+1 +1n

sin nπ xLx

⎝⎜⎞

⎠⎟n=1

∑ sinh nπ y Lx( )sinh nπ Ly Lx( )

!′′q = −k ∂T∂xi + ∂T

∂yj

⎛⎝⎜

⎞⎠⎟

!′′qk T1 −T2( ) =

∂θ∂xi + ∂θ

∂yj

⎛⎝⎜

⎞⎠⎟

!′′qk T1 −T2( ) = 2

−1( )n+1 +1Lx

cosnπ xLx

⎝⎜⎞

⎠⎟n=1

∑ sinh nπ y Lx( )sinh nπ Ly Lx( )

⎣⎢⎢

⎦⎥⎥i +

2−1( )n+1 +1Lx

sinnπ xLx

⎝⎜⎞

⎠⎟n=1

∑ cosh nπ y Lx( )sinh nπ Ly Lx( )

⎣⎢⎢

⎦⎥⎥j

T1 T2y = 0

!′′q y = 0( )k T1 −T2( ) = 2

−1( )n+1 +1Lx

sin nπ xLx

⎝⎜⎞

⎠⎟n=1

∑ 1sinh nπ Ly Lx( )

⎣⎢⎢

⎦⎥⎥j

!q y = 0( )k T1 −T2( ) = 2

−1( )n+1 +1Lx

sin nπ xLx

⎝⎜⎞

⎠⎟n=1

∑ 1sinh nπ Ly Lx( )

⎣⎢⎢

⎦⎥⎥dx

0

Lx∫0w

∫ dz j

!q y = 0( ) wk T1 −T2( ) = 4

π−1( )n+1 +1nn=1

∑ 1sinh nπ Ly Lx( )

⎣⎢⎢

⎦⎥⎥j

Page 6: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

6

Laboratory: For this laboratory, you will write a MATLAB m-file to calculate the temperature from the analytical solution above at a matrix of x and y coordinates and make a contour plot of the results. Below are some general instructions on how to write your program. Please refer to the online MATLAB help files as needed for more complete information. If you have not previously used MATLAB, or if you have forgotten some of MATLAB’s features or syntax, you should use the program help material before starting this assignment. In particular, use the “Help” menu item and “Product Help” sub menu item to bring up the “Help” window. On the left press the “Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material in the “Quick Start,” “Language Fundamentals,” “Mathematics,” “Graphics,” and “Programming” sections. 1. Open MATLAB by double clicking on the MATLAB icon on the desktop. Create a new script (or m-file) using the “File”, “New”, and “Script” menu item. Save the file as “ME554_lab1.m” (or anything else, but do NOT use a name with spaces) using the “File” and “Save As” menu item. Recall that a script (or m-file) is simply a list of MATLAB instructions saved in a file with a “.m” extension. Write a header for your program using “%” for comment lines. Your header should include the program name, description, your name, date created, date last modified, and a variable list with definitions (come back and add these later). Continue to use many comment lines throughout your program to describe each subsequent set of commands and to indicate units for each parameter. 2. Next, put a line with just clear, clc to clear the memory of all variables and clear the screen. 3. Set the values for the geometry and boundary condition parameters ( , , , and ). You can do this generally in three ways: simple assignment statement, by reading input from the screen during execution, or by reading data from a file. Use the first method for some of the input as follows: T1 = 100; % deg. Celsius where the semicolon suppresses output during execution and the units are included as a comment. Also, try using the second method with an input statement: T2 = input(‘Input temperature at y = Ly in deg. C: ‘)

Lx Ly T1 T2

Page 7: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

7

4. Create vectors of x and y values of the coordinates at which you need to evaluate the temperature. First, set and to the number of increments in each direction. Again, you can do this with any of the methods given is Step 2. Second, calculate the increments between calculations: dx = Lx/Nx; % m, increment in x-direction dy = Ly/Ny; % m, increment in y-direction Third, create the vectors of x and y values using the following: x = [0:dx:Lx]; y = [0:dy:Ly]; If you do not know what these lines create, enter them in the “Command Window” in MATLAB with numbers substituted for the variables to test their output. Note that these lines demonstrate some of the power of MATLAB in that you can create a vector of data in a single line, unlike FORTRAN or c++. 5. Initialize a matrix to store the data for during calculations using the zeros function and set

at your top boundary using: theta = zeros(Nx + 1, Ny + 1); theta(:,Ny+1) = 1; This is required for two reasons: (1) to allocate the correct amount of space in memory for your array and (2) to initialize to zero for the summation in the next step. 6. Calculate the value of at each (x, y) location. To do this, use two nested for loops to cycle through each (x, y) location with the following syntax: for j = 2:Ny for i = 2:Nx % insert Equation (27) here end end You will need to use a third nested for loop to sum up the terms in Equation (27). Use “…” to continue your equation on the next line to make it easier to read. Note that for MATLAB “pi” will give you with sufficient precision. For this sum, let n go from 1 to 199. Because you are stopping the calculation at a finite number of terms your results will have truncation error in addition to machine precision error for double precision calculations. Note that in Equation (4), for even values of n, the summation term goes to 0 so you can skip these terms to speed up the calculation. One way to do this in MATLAB is to use “for n = 1:2:199” which increments n by 2 for each loop instead of the default increment of 1. 7. Convert the calculated matrix to temperatures using the variable name “T”.

Nx Ny

θ

θ =1

θ

θ

π

θ

Page 8: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

8

8. Print out temperature data to the screen for review using fprintf to produce formatted output. The following lines work well: for j = Ny+1:-1:1 fprintf('%7.1f', T(:,j)) fprintf('\n') end These lines print out the temperatures in the order you would expect for a Cartesian coordinate system (rotated counterclockwise 90˚ as shown in Figure 2). The ‘%7.1f’ sets the format for the temperatures as floating point with seven spaces and one number after the decimal place. The ‘\n’ adds a carriage return between lines. Alternatively, you can use (fliplr(T))’ or rot90(T) to rotate the T matrix before printing it out. 9. Make a contour plot of the resulting temperatures using the following lines: dT = (T2 – T1)/Nc; v = T1:dT:T2; colormap(jet) contourf(x, y, T', v) colorbar Tmax = max(T1, T2) Tmin = min(T1, T2) caxis ([Tmin, Tmax]) axis equal tight title('Contour Plot of Temperature in deg. C') xlabel('x (m)') ylabel('y (m)') where is the number of contours (whose value needs to be set), is the temperature step between contours, and v is a vector that sets the temperature levels for the contours. The MATLAB built in function colormap sets the colors used for contours, contourf creates the filled contour plot where ' after transposes the matrix, colorbar adds a scale to the plot where caxis sets the limits, axis equal makes the x and y axes have the same length, and xlabel and ylabel add x-axis and y-axis labels to the plot. 10. Try changing the variables or options for Steps 8 and 9 to alter the output. You can also explore some of the many other options for plotting data. For example, to remove the lines between the contours change the 4th line to the following: contourf(x, y, T', v, 'LineStyle', 'none') 11. Calculate the dimensionless heat transfer rate in the y-direction at given by Equation (33).

Nc dT

T

y = 0

Page 9: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

9

Assignment Submit your lab assignment as a single pdf file using PolyLearn with the items listed below. 1. Set the following values in your code: , , and . Include

tables for temperature values at each location for and . • Label them “Table 1” and “Table 2” along with a descriptive caption above the table. • Include x and y locations for each temperature on your table so you can determine which

temperatures are evaluated at the same location. • Use 4 decimal places for temperature data evaluated using double precision. 2. Verify that your solution has converged to at least four decimal places for temperatures on the interior of your domain. Discuss how limits in machine precision and truncating the infinite series solution to a finite number of terms influence the accuracy (be quantitative) of your calculated temperatures. To help you answer this question use the MATLAB function eps() to determine the accuracy of both double and single precision floating point calculations and compare this to the magnitude of terms in your summation. 3. Compare the values in Table 1 and 2 for each case at the same coordinate locations. Are the temperatures the same or different (be quantitative) at each location where they overlap? Briefly explain why this result makes sense. 4. Include contour plots for the two cases in step 2. For each figure include “Figure 1” and “Figure 2” along with a descriptive caption below the figure. Make sure that if you plot or print out your results in grayscale that you use “grayscale” for your colormap so that each temperature has a distinct shade. 5. Do the contour plots change for these two cases? Briefly explain why this result makes sense and how this corresponds to the accuracy of your solution. 6. Include a copy of your final m-file.

Lx = Ly = 1m T1 = 0oC T2 = 100

oC

Nx = Ny = 5 Nx = Ny = 10

Page 10: LAB 1: MATLAB - Introduction to Programmingkshollen/ME554/Labs/ME554_Lab_1.pdf“Contents” tab. Under the “MATLAB” and “Getting Started” submenus read through the material

10

Cartesian Coordinates: where and

and Matrix Notation:

Figure 2. Schematic diagrams of Cartesian coordinate system and matrix notation.

Ti, j = T (xi , y j ) Δx = Lx Nx xi = i −1( ) ΔxΔy = Ly N y y j = j −1( ) Δy

T⎡⎣ ⎤⎦ =

T1,1 T1,2 ! T1,Ny+1

T2,1 T2,2 "

" # "TNx+1,1 ! ! TNx+1,Ny+1

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

Lx

x

y

T1,1 T2,1

T2,2 T1,2

TNx+1, Ny+1 T1, Ny+1

TNx+1, 1

∆x Ly

∆y

j (column number)

i (row number)

NOTE: The T matrix is rotated clockwise 90˚ relative to the Cartesian coordinate system.

Figure is shown with

Nx = Ny = 3