Computer Programming (TKK-2144)

45
Computer Programming (TKK-2144) 13/14 Semester 1 Instructor: Rama Oktavian Email: [email protected] Office Hr.: M.13-15, W. 13-15 Th. 13-15, F. 13-15

description

13/14 Semester 1. Computer Programming (TKK-2144). Instructor: Rama Oktavian Email: [email protected] Office Hr.: M.13-15, W. 13-15 Th. 13-15, F. 13-15. Outlines. 1. Interpolation: Basic Theory. 2. Methods of interpolation. 3 . Interpolation in Excel . - PowerPoint PPT Presentation

Transcript of Computer Programming (TKK-2144)

Page 1: Computer Programming (TKK-2144)

Computer Programming(TKK-2144)

13/14 Semester 1

Instructor: Rama OktavianEmail: [email protected] Hr.: M.13-15, W. 13-15 Th. 13-15, F. 13-15

Page 2: Computer Programming (TKK-2144)

Outlines

1. Interpolation: Basic Theory

2. Methods of interpolation

3. Interpolation in Excel

4. Interpolation in chem.eng

Page 3: Computer Programming (TKK-2144)

Interpolation (basic theory)Interpolation functiona function that passes exactly through a set of data points.Interpolating functions to interpolate values in tables

find functional values at other values of the independentvariable, e.g. sin(0.63253)

Page 4: Computer Programming (TKK-2144)

Interpolation (basic theory)Interpolation

1. Linear Interpolation

2. Polynomial Interpolation

3. Cubic Spline Interpolation

4. Lagrange Polynomial Interpolation

Page 5: Computer Programming (TKK-2144)

Interpolation methodsLinear Interpolation Linear interpolation is obtained by passing a straight line between2 data points

)(xf the exact function for which values are known only at a discrete set of data points

)(xg the interpolated approximation to )(xf

10 , xx the data points(also referred to as interpolation points or nodes)

Page 6: Computer Programming (TKK-2144)

Interpolation methodsLinear Interpolation

In tabular form:

Page 7: Computer Programming (TKK-2144)

Interpolation methodsLinear Interpolation

If is a linear function then)(xg bAxxg )(

To pass through points and ))(,( 00 xfx ))(,( 11 xfx

)()()()( 0101

00 xfxf

xxxxxfxg

Page 8: Computer Programming (TKK-2144)

Interpolation methodsLinear Interpolation

Advantages:• Easy to use (homework, exams)• Exact for linear functions

Disadvantages:• Not very accurate for nonlinear functions

Page 9: Computer Programming (TKK-2144)

Interpolation methodsLinear Interpolation

Sin(0.63) = ??

Page 10: Computer Programming (TKK-2144)

Interpolation methodsError in Linear Interpolation

Error is defined as:

Page 11: Computer Programming (TKK-2144)

Interpolation methodsPolynomial Interpolation

Two steps:

• obtain polynomial coefficients• evaluate the value of the polynomial at the desired location (xi)

For N data points, there is a unique polynomial (usually of order n-1) that goes through each pointThis is an interpolating polynomial, because it goes exactly through each data point

Page 12: Computer Programming (TKK-2144)

Interpolation methodsPolynomial Interpolation First step:• obtain polynomial coefficientsFor general n, the goal is to determine the coefficients a1, a2,…,an so that for all i = 1:n

iin yxp )(1

Pn-1(x) = a1 + a2x + a3x2 +…+ anxn-1

Writing these equations in matrix-vector form, we obtain

Vandermonde matrix

Page 13: Computer Programming (TKK-2144)

Interpolation methodsPolynomial Interpolation Second step:• evaluate the value of the polynomial at the desired location (xi)

iin yxp )(1

Pn-1(xi) = a1 + a2xi + a3xi2 +…+ anxin-1

Sin(0.63) = ??

Page 14: Computer Programming (TKK-2144)

Interpolation in excelLookup Tables • Often, we need to retrieve data that is stored in a table• For example, consider these tables:

Page 15: Computer Programming (TKK-2144)

Interpolation in excelLOOKUP(Lookup_value,Lookup_vector,Result_vector)

search downthe lookupvector

find eithera matchingvalue, ora valuegreater thanthe lookupvalue, whicheveroccurs first go to the same position in

the result vector, and extractthe value there

Page 16: Computer Programming (TKK-2144)

Interpolation in excelMetal Modulus of

Elasticity, psi Density, lb/in3 Yield Strength, psi

Aluminum 2014-T6 10,600,000 0.101 60,000

Aluminum 6061-T6 10,000,000 0.098 37,000

Stainless Steel 304 28,000,000 0.284 30,000

Structural Steel A36 29,000,000 0.284 36,000

Since the properties to be retrieved are in columns, the table is called a vertical lookup table

The vertical lookup table command in Excel is:VLOOKUP(lookup value, table range, column number, true/false)

Page 17: Computer Programming (TKK-2144)

Interpolation in excelVLOOKUP(Lookup_value,Table_array,col_index_num,Range_lookup)

searchdownleftcolumnoftablearraytofindmatch

Which column number forextraction of result? 1, 2, 3 or 4?

result

optional argument:if FALSE, requiresan exact match

Page 18: Computer Programming (TKK-2144)

Interpolation in excelExamples

Page 19: Computer Programming (TKK-2144)

Interpolation in excelExamplesUsing VLOOKUP to find the freezing point of a 33% solution

The formula=VLOOKUP(F3,$A$3:$D$54,2,0)

Page 20: Computer Programming (TKK-2144)

Interpolation in excelOther ways to Lookup Tables Match(Lookup_value, lookup_array, match type) Returns the relative position in the array that matches the lookup value.

Index(range, position) Returns the value from the range in the position specified

Page 21: Computer Programming (TKK-2144)

Interpolation in excelExample

=MAX($B$5:$B$l6) 83119

Page 22: Computer Programming (TKK-2144)

Interpolation in excelExample

use the MATCH function to find the position of the maximum value in the rangeMATCH(lookup_value,lookup_array,match_type_num)=MATCH(83119,$B$5:$B$16,0)

Page 23: Computer Programming (TKK-2144)

Interpolation in excelExample

use the INDEX function to return the value in the same position in the array of months:

=INDEX( $A$5:$A$16,4)

=INDEX( $A$5:$A$16, MATCH( MAX( $B$5: $B$16), $B$5:$B$16,0))

Page 24: Computer Programming (TKK-2144)

Interpolation in excelLookup value for two-way tables

Page 25: Computer Programming (TKK-2144)

Interpolation in excelLookup value for two-way tables

=VLOOKUP(Temp,Table, MATCH( Percent, P-Row, 1 )+ 1,1)

=VLOOKUP( M2, $A$4:$1$32, MATCH( N2, $B$3:$K$3,1 )+1, 1)

Page 26: Computer Programming (TKK-2144)

Interpolation in excelA Note About Lookup Tables

Lookup tables will not interpolate values! When looking up a numerical value, if an exact match is not

found (and the “TRUE” option allows for an approximate match), then the value searched for is rounded down to the next tabulated value.

We will illustrate this in the following example

Page 27: Computer Programming (TKK-2144)

Interpolation in excelExample

The population data of a town is given in the table

We want to use a lookup table to report the population for any year entered

Page 28: Computer Programming (TKK-2144)

Interpolation in excelExample

Note that using the VLOOKUP function (with the TRUE/FALSE option left off or set to TRUE) returns the population for the next year in the table lower than the input value:

Page 29: Computer Programming (TKK-2144)

Interpolation in excelLinear interpolation in excelFor each population interval, construct an IF statement to see if the input year falls within the interval range…If the input year is within the interval, then the interpolation formula should be used…

If the input year is not within the interval, then a space is entered into the cell to make it blank.

Page 30: Computer Programming (TKK-2144)

Interpolation in excelLinear interpolation in excel

IF statements add a great deal of flexibility to Excel spreadsheets – allow control over how calculations are performed

Lookup tables are convenient for accessing data that is stored in table form. Important to remember that lookup tables do not interpolate between values

Linear interpolation formulas can be added to tables in order to provide a better estimate of values between tabulated points

Page 31: Computer Programming (TKK-2144)

Interpolation in excelApplication of VLOOKUP: Linear interpolation from a table of physical properties

For any %HNO3from 0 to 90, wewant an automaticestimate of theheat capacity

Page 32: Computer Programming (TKK-2144)

Interpolation in excelApplication of VLOOKUP: Linear interpolation from a table of physical properties

Create index columns to the right and left of the table

Page 33: Computer Programming (TKK-2144)

Interpolation in excelApplication of VLOOKUP: Linear interpolation from a table of physical propertiesEnter VLOOKUP functions based on “NitricTable2” and using the low and high row indices to extract the bracketing values of %HNO3 and Heat Capacity.

Page 34: Computer Programming (TKK-2144)

Interpolation in excel

LowPct HighPct

LowHC

HighHC

linear approximationbetween to locationsin the table

input%HNO3

HeatCapestimate

input LowPct

HeatCap HighHC LowHC LowHCHighPct LowPct

Page 35: Computer Programming (TKK-2144)

Interpolation in excelAutomatic linear interpolationformula and results

Page 36: Computer Programming (TKK-2144)

Interpolation in excelChange values of the input to get different results

[including bad results when input outside ofacceptable range]

the good the bad and the ugly!

Page 37: Computer Programming (TKK-2144)

Interpolation in excelLinear Interpolation using TREND function

TREND can be used to perform linear interpolation between two adjacent data points.

TREND( known_y's, known_x's, new_x 's, const)

=TREND( B20: B21 , A20:A21, F18,1)

Page 38: Computer Programming (TKK-2144)

Interpolation in excelLinear Interpolation in a Two-way Table

Find the viscosity value in the table for x = 76°F, y = 56.3 wt% ethylene glycol

Page 39: Computer Programming (TKK-2144)

Interpolation in excelLinear Interpolation in a Two-way Table

First step, interpolate the value at y = 56.3

Second step, interpolate the value at x = 76

Page 40: Computer Programming (TKK-2144)

ExtrapolationDefinition

The process of taking data values at points x1, ..., xn, and approximating a value outside the range of the given points.

The process of estimating and forecasting

Sin (2.6) = ??

Page 41: Computer Programming (TKK-2144)

ExtrapolationMethods

Linear extrapolation

If the two data points nearest the point to be extrapolated are and , linear extrapolation gives the function:

Polynomial extrapolation

Page 42: Computer Programming (TKK-2144)

ExtrapolationMethods

Linear extrapolation

Given the following data which is known to be linear, extrapolate the y value when x = 2.3.

(0.3 0.80), (0.7, 1.3), (1.2, 2.0), (1.8, 2.7)

Page 43: Computer Programming (TKK-2144)

Interpolation in Chem. EngExample - Vapor Pressure of Water

Determine vapor pressure of water at:• 25 °C• 92 °C• 105 °C

Page 44: Computer Programming (TKK-2144)

Interpolation in Chem. EngExample - “Real” Gases

zRTVp ˆp- pressurez- compressibility factorR- gas constantT- temperature - Molar volumeV̂

Find z at T = 725K and p = 8bar.

Page 45: Computer Programming (TKK-2144)