LAB 1: MATLAB - Introduction to Programming. Objective:

Similar documents
MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

August 7, 2007 NUMERICAL SOLUTION OF LAPLACE'S EQUATION

EEE161 Applied Electromagnetics Laboratory 1

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Solving Differential Equations on 2-D Geometries with Matlab

DISCRETE RANDOM VARIABLES EXCEL LAB #3

Homework 1 Solutions

Using SkyTools to log Texas 45 list objects

Lab 6: Linear Algebra

ES205 Analysis and Design of Engineering Systems: Lab 1: An Introductory Tutorial: Getting Started with SIMULINK

SYMBOLIC AND NUMERICAL COMPUTING FOR CHEMICAL KINETIC REACTION SCHEMES

ME 320L/354L Laboratory 3

ENGR Spring Exam 2

Electric Fields and Equipotentials

Lab 1: Dynamic Simulation Using Simulink and Matlab

Lab 5: Post Processing and Solving Conduction Problems. Objective:

MAT 343 Laboratory 6 The SVD decomposition and Image Compression

Introduction to Computer Tools and Uncertainties

Experiment: Oscillations of a Mass on a Spring

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

AMS 27L LAB #8 Winter 2009

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Introduction to Computational Neuroscience

Math Assignment 3 - Linear Algebra

Example Example Example Example

C:\Dokumente und Einstellungen \All Users\Anwendungsdaten \Mathematica. C:\Dokumente und Einstellungen \albert.retey\anwendungsdaten \Mathematica

Partial Differential Equations (PDEs) and the Finite Difference Method (FDM). An introduction

Finite Element Modules for Enhancing Undergraduate Transport Courses: Application to Fuel Cell Fundamentals

2D Plotting with Matlab

Lab 3 Acceleration. What You Need To Know: Physics 211 Lab

Matlab Instruction Primer; Chem 691, Spring 2016

The roots are found with the following two statements. We have denoted the polynomial as p1, and the roots as roots_ p1.

Temperature measurement

Physics 103 Newton s 2 nd Law On Atwood s Machine with Computer Based Data Collection

ON SITE SYSTEMS Chemical Safety Assistant

Molecular Modeling and Conformational Analysis with PC Spartan

SCIENTIFIC COMPUTING 7P-100-SCI

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Physics 212E Spring 2004 Classical and Modern Physics. Computer Exercise #2

Lab I. 2D Motion. 1 Introduction. 2 Theory. 2.1 scalars and vectors LAB I. 2D MOTION 15

Linear Motion with Constant Acceleration

LAB 2 - ONE DIMENSIONAL MOTION

New Mexico Tech Hyd 510

2: SIMPLE HARMONIC MOTION

Introduction to SVD and Applications

2 Solving Ordinary Differential Equations Using MATLAB

Lab I. 2D Motion. 1 Introduction. 2 Theory. 2.1 scalars and vectors LAB I. 2D MOTION 15

CHEMDRAW ULTRA ITEC107 - Introduction to Computing for Pharmacy. ITEC107 - Introduction to Computing for Pharmacy 1

Lecture 5b: Starting Matlab

Cantera / Stancan Primer

Companion. Jeffrey E. Jones

HOMEWORK 3: Phase portraits for Mathmatical Models, Analysis and Simulation, Fall 2010 Report due Mon Oct 4, Maximum score 7.0 pts.

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 53L Fall Test I. Rebecca A. Simmons & Michael R. Gustafson II

1-D Convection-Diffusion Lab

Representations of Motion in One Dimension: Speeding up and slowing down with constant acceleration

Senior astrophysics Lab 2: Evolution of a 1 M star

Determination of Density 1

The data for this lab comes from McDonald Forest. We will be working with spatial data representing the forest boundary, streams, roads, and stands.

Chapter 2: Numeric, Cell, and Structure Arrays

BCMB/CHEM 8190 Lab Exercise Using Maple for NMR Data Processing and Pulse Sequence Design March 2012

Matlab Section. November 8, 2005

Experiment 0 ~ Introduction to Statistics and Excel Tutorial. Introduction to Statistics, Error and Measurement

M E R C E R W I N WA L K T H R O U G H

Linear Algebra Using MATLAB

Created: 2/3/96 Modified: September 29, Author: Theresa Julia Zielinski Page 1

Handout 8 MATLAB Code for the Projection Method and BC Details for the Lid-Driven Cavity Problem

PHY 123 Lab 9 Simple Harmonic Motion

EEL2216 Control Theory CT1: PID Controller Design

1 Introduction to MATLAB

41. Sim Reactions Example

COMS 6100 Class Notes

MAT300/500 Programming Project Spring 2019

Problem 1 (10 points)

Laboratory handouts, ME 340

1. The Basic X-Y Scatter Plot

MAGNETITE OXIDATION EXAMPLE

Lab #10 Atomic Radius Rubric o Missing 1 out of 4 o Missing 2 out of 4 o Missing 3 out of 4

Read through A Graphing Checklist for Physics Graduate Students before submitting any plots in this module. See the course website for the link.

Laboratory handout 5 Mode shapes and resonance

Project 2: Using linear systems for numerical solution of boundary value problems

Fin System, Inc. Company Report. Temperature Profile Calculators. Team 1 J. C. Stewards, Lead A. B. Williams, Documentation M. D.

Conformational Analysis of n-butane

Remember that C is a constant and ë and n are variables. This equation now fits the template of a straight line:

Fluid flow I: The potential function

DOAS measurements of Atmospheric Species

Simultaneous Equations Solve for x and y (What are the values of x and y): Summation What is the value of the following given x = j + 1. x i.

PHYS 301 HOMEWORK #13-- SOLUTIONS

Lab 15 Taylor Polynomials

MERGING (MERGE / MOSAIC) GEOSPATIAL DATA

Speed and Velocity: Recall from Calc 1: If f (t) gives the position of an object at time t, then. velocity at time t = f (t) speed at time t = f (t)

Experiment 1: Linear Regression

Bfh Ti Control F Ws 2008/2009 Lab Matlab-1

Lab Activity: The Central Limit Theorem

TALLINN UNIVERSITY OF TECHNOLOGY, INSTITUTE OF PHYSICS 6. THE TEMPERATURE DEPENDANCE OF RESISTANCE

Refine & Validate. In the *.res file, be sure to add the following four commands after the UNIT instruction and before any atoms: ACTA CONF WPDB -2

The Geodatabase Working with Spatial Analyst. Calculating Elevation and Slope Values for Forested Roads, Streams, and Stands.

ECE 220 Laboratory 4 Volt Meter, Comparators, and Timer

EGR 111 Heat Transfer

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

TOPIC 2 Computer application for manipulating matrix using MATLAB

Transcription:

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, T(x, y), in a solid with constant thermal conductivity and rectangular cross section as shown in Figure 1 can be found analytically by solving the heat diffusion equation (or Laplace s Equation) 2 T x + 2 T 2 y = 0 (1) 2 using the Separation of Variables method. An introduction to the method can be found in Bergman, et al., Introduction to Heat Transfer, 6 th ed., Wiley, 2011, pp 231-235. Another excellent reference for analytical solutions to conduction problems is Carslaw and Jaeger, Conduction of Heat in Solids, Oxford University Press, 1959. 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). y y = L y T(x, y = L y ) = T 2 T(x = 0, y) = T 1 T(x = L x, y) = T 1 x T(x, y = 0) = T 1 x = L x Figure 1. Schematic of two-dimensional domain for conduction heat transfer.

As an example, for the following boundary conditions shown in Figure 1 T( x = 0,y) = T 1, T( x = L x, y) = T 1, T( x, y = 0) = T 1, T( x, y = L y ) = T 2 (2) where T 1 and T 2 are constant temperatures and dimensionless temperature θ( x, y) is defined as ( ) = T ( x, y ) T 1 θ x, y the analytical solution can be determined to be ( ) = 2 π θ x, y which is an infinite series solution. Laboratory: n=1 T 2 T 1 (3) ( 1) n +1 +1 % sin n π x ( sinh n π y L x ' * n & L x ) sinh n π L y ( ) ( L x ). (4) 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. 2

3. Set the values for the geometry and boundary condition parameters (L x, L y, T 1, and T 2 ). 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: ) 4. Create vectors of x and y values of the coordinates at which you need to evaluate the temperature. First, set N x and N y 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; dy = Ly/Ny; % m, increment in x-direction % 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 θ =1 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 (4) here end end 3

You will need to use a third nested for loop to sum up the terms in Equation (4). 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. 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 N c is the number of contours (whose value needs to be set), dt is the step in temperature 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 T 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. 4

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, changing the 4th line to contourf(x, y, T', v, 'LineStyle', 'none') will remove the lines between the contours. 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: L x = L y = 1 m, T 1 = 0 C, and T 2 = 100 C. Include tables for temperature values at each location for N x = N y = 5 and N x = N y = 10. 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. 5

Cartesian Coordinates: ( ) Δx Δy = L y N y and y j = ( j 1) Δy T i, j = T(x i,y j ) where Δx = L x N x and x i = i 1 y y T 1, Ny+1 T Nx+1, Ny+1 Figure is shown with N x = N y = 3 L y x T 1,2 T 2,2 T 1,1 T 2,1 T Nx+1, 1 x L x Matrix Notation: " T 1,1 T 1,2! T 1,Ny % +1 $ ' T 2,1 T 2,2 " [ T] = $ ' $ " # " ' $ ' # T Nx +1,1!! T Nx +1,N y +1& j (column number) NOTE: The T matrix is rotated clockwise 90 relative to the Cartesian coordinate system. i (row number) Figure 2. Schematic diagrams of Cartesian coordinate system and matrix notation. 6