INTEGRAL, LEAST SQUARE & CHARACTER OPERATIONS Budi Hartono, ST, MPM
integral
INTEGRAL CALCULATION
Integral Calculation
– the concept
a
standard mathematical technique can be used to calculate the area, volume, etc can be solved analytically or numerically computer
solve the integral problems numerically
expression:
Integral Calculation
– the concept
Three numerical methods to solve integral problems
Simpson method Rectangle Trapezoid
Similar idea: Total Area = sum of the partial area
Integral Calculation
– the concept
Analytical y
Y = F(x)
B Area = ∫ F ( x ) dx A
A
x B
Numerical Area = A1+A2+A3+…+An the more the better (accuration) , but time ??
Integral Calculation
– Simpson Method
F ( X 0 ) + 4 × F ( X 0+1( p ) ) + 2 × F ( X 0+ 2( p ) ) B P Area = ∫ F ( x)dx = × + 4 × F ( X 0+ 3( p ) ) + ... + 3 A 2 × F ( X ( n − 2)( p ) ) + 4 × F ( X ( n −1)( p ) ) + F ( X ( n )( p ) ) Where: N = (B-A) / P; the number of sub intervals P : the width of sub interval X0 : A Xn : B
parabolic approach
Simpson Method – analytical vs. numerical 2
3 2 ( 4 x − 3 x + 2)dx = ??? ∫ 1
2
[
]
2
3 2 4 3 ( 4 x − 3 x + 2 ) dx = x − x + 2x 1 ∫
=12-2 = 10
1
Numerically, assumed that p=0.1
(4 • 13 − 3 • 12 + 2) 3 2 3 2 + 4(4 • 1.1 − 3 • 1.1 + 2) + 2((4 • 1.2 − 3 • 1.2 + 2) = ( p / 3) × + ... 3 2 3 2 + 2(4 • 1.8 − 3 • 1.8 + 2) + 4(4 • 1.9 − 3 • 1.9 + 2) 3 2 + (4 • 2 − 3 • 2 + 2) = 9.602
Simpson Method – error
Accuration limit • Ideally, the numerical calculation should result in convergence. • The more the area is divided, the more the calculation focus on a single fixed number Analytical Area
iteration #
Accuration limit
Analytical
L.C.
I.S
iteration #
• an 'accuration limit' must be determined to check the difference between the current result with the previous one • the limit is a number close to zero.
Accuration limit (2)
Integral _ Simpson − LastCalc <ε Integral _ Simpson if epsilon is nearly zero there is no reason to continue the calculation already close to the asymptotic line further 'loop' will not significantly increase the accuration
Accuration limit (3) Occasionally,
the limit is too far to reach
can not approach the asymptotic line not convergent
use
additional limitation e.g. limit the loop calculation up to 20 recurrences
Simpson Method - flowchart Start
Integral_simpsoncurrentcalculation LastCalc pervious calculation
Read F(x), A, B, ε
P width, Nnumber of sub classes
Integral_Simpson= P/3*SUM
Integral_Simpson=0 P=(B-A)/2
FOR I=1 TO 20
NO LastCalc = Integral_Simpson N=(B-A)/P
ABS [ Integral_Simpson-LastCalc)/ Integral_Simpson] < ε ??
P=P/2
Print Integral_Simpson
Next I FOR J=2,4, …, N-2 SUM = SUM + 2xF(A+JxP) +4xF(A+(J+1)xP)
Print "can' be found within 20 iterations"
Stop
Integral Calculation
– Rectangle B Area = ∫ F ( x ) dx A
y
Y = F(x)
A
x B
F ( x0 ) + F ( x0+ p ) 2 Area = ∫ F ( x)dx = P + F ( x0+ 2 p ) + ... 1 + F ( x ) + F ( x ) ( n−2) p ( n −1) p
Rectangle - numerical 2
[
]
2
3 2 4 3 ( 4 x − 3 x + 2 ) dx = x − x + 2x 1 ∫ 1
Numerically, assumed that p=0.1
(4 • 13 − 3 • 12 + 2) 3 2 3 2 + (4 • 1.1 − 3 • 1.1 + 2) + (4 • 1.2 − 3 • 1.2 + 2) = ( p / 2) × + ... 3 2 3 2 + (4 • 1.8 − 3 • 1.8 + 2) + (4 • 1.9 − 3 • 1.9 + 2) 3 2 + (4 • 2 − 3 • 2 + 2) = 9.075
Rectangle – the error Integral _ Re ct − LastCalc <ε Integral _ Re ct
Analytical Area
iteration #
Rectangle - flowchart Integral_rectcurrentcalculation
Start
LastCalc pervious calculation P width, Nnumber of sub classes
Read F(x), A, B, ε Integral_Rect= P*SUM
Integral_rect=0 P=(B-A)/2
FOR I=1 TO 20
NO LastCalc = Integral_Rect N=(B-A)/P
ABS [ Integral_Rect-LastCalc)/ Integral_Rect] < ε ??
P=P/2
Print Integral_Rect
Next I FOR J=0 to N-1
SUM = SUM + F(A+JxP)
Print "can' be found within 20 iterations"
Stop
Integral Calculation
– Trapezoid B Area = ∫ F ( x ) dx A
y
Y = F(x)
A
x B
F ( X 0 ) + 2 × F ( X 0+1( p ) ) + 2 × F ( X 0+ 2( p ) ) B P + 2 × F ( X 0+ 3( p ) ) + ... Area = ∫ F ( x)dx = × + 2 × F ( X ) + 2 × F ( X ) 2 ( n − 2 )( p ) ( n − 1 )( p ) A + F ( X ( n )( p ) )
=12-2 = 10
Trapezoid 2
[
]
2
3 2 4 3 ( 4 x − 3 x + 2 ) dx = x − x + 2x 1 ∫ 1
Numerically, assumed that p=0.1
(4 • 13 − 3 • 12 + 2) 3 2 3 2 + 2(4 • 1.1 − 3 • 1.1 + 2) + 2((4 • 1.2 − 3 • 1.2 + 2) = ( p / 2) × + ... 3 2 3 2 + 2(4 • 1.8 − 3 • 1.8 + 2) + 2(4 • 1.9 − 3 • 1.9 + 2) 3 2 + (4 • 2 − 3 • 2 + 2) = 11.125
Trapezoid - flowchart Integral_trapcurrentcalculation
Start
LastCalc pervious calculation P width, Nnumber of sub classes
Read F(x), A, B, ε Integral_Trap= P*SUM
Integral_trap=0 P=(B-A)/2
FOR I=1 TO 20
NO LastCalc = Integral_Trap N=(B-A)/P
ABS [ Integral_Trap-LastCalc)/ Integral_Trap] < ε ??
P=P/2
Print Integral_Trap
Next I FOR J=0 to N-1 SUM = SUM + F(A+JxP) +F(A+(J+1)xP)
Print "can' be found within 20 iterations"
Stop
Comparison of three methods Generally
speaking:
simpson
the most accurate parabolic
rectangle least accurate
trapezoid
in-between
Least square
LEAST SQUARE
Least Square -
Introduction
we
employ Regression Analysis to examine the relationship among quantitative variables.
The
technique is used to predict the value of one variable (the dependent variable - y) based on the value of other variables (independent variables x1, x2,…xk.)
The Model Independent Variable -1 (x1) Independent Variable -2 (x2) Independent Variable -3
Dependent Variable BLACK BOX
(x3) Independent Variable -4 (x4) relationships ??
(y)
The Model The
first order linear model or a simple regression model, y = A + Bx y = dependent variable x = independent variable Α = y-intercept Β = slope of the line ε = error variable
A and B are unknown, ytherefore, are estimated from the data.
Rise B = Rise/Run A
Run
x
Estimating the Coefficients The
estimates are determined by
drawing a sample from the population of interest, calculating sample statistics. producing a straight line that cuts into the data.
y
The question is: Which straight line fits best
x
Least Squares Method The best line is the one that minimizes the sum of squared vertical differences between the points and the line.
2 2 2 Sum of squared differences (2 - 1) = (4 +- 2)(1.5 + - 3) (3.2 + - 4)2 = 6.89 2 2 2 Sum of squared differences (2 -2.5) = (4 +- 2.5) (1.5 + - 2.5) (3.2 +- 2.5)2 = 3.99
4 3 2.5 2
Let us compare two lines The second line is horizon
(2,4)
(4,3.2)
(1,2)
(3,1.5)The
1
1
2
3
smaller the sum of squared differences the better the fit of the line to the data. 4
Least Square method y = A + Bx where:
(∑ xi ∑ yi ) ∑ xiyi − ∑ xiyi − nx y n ˆ βB1 = or 2 2 2 ( x ) x − n x ∑ ∑ 2 i i ∑ xi − n Aˆ0 = yYbar β − βˆ1-xB Xbar
n : # data
• Example 1 Relationship between odometer reading and a used car’s selling price.
A car dealer wants to find the relationship between the odometer reading and the selling price of used cars. A random sample of 100 cars is selected, and the data. Find the regression line.
Car Odometer 1 37388 2 44758 3 45833 4 30862 5 31705 6 34010 . . . . . .
Price 5318 5061 5008 5795 5784 5359 . . .
Independent variable x Dependent variable
Least Square Method – flow chart START
(∑ xi ∑ yi ) ∑ xiyi − nx y n βˆB1-=# of data or N 2 2 2 ∑ xi − nx 2 (∑ xi ) ∑ xi − n βˆ0 = yYbar − βˆ1x- B Xbar A ∑ xiyi −
Read N
SUMX = 0; SUMY = 0 SUMXY=0; SUMXX=0
AVEX = SUMX /N AVEY = SUMY /N
FOR I=1 TO N
B = (SUMXY-N*AVEX*AVEY)/ (SUMXX-N*AVEX^2) A = AVEY - B*AVEX
SUMX =SUMX+X[I] SUMY =SUMY+Y[I] SUMXY =SUMXY+X[I]Y[I] SUMXX =SUMXX+X[I]X[I]
PRINT "Y=" & A &"X+" B
NEXT I STOP
Using the computer
Tools > Data analysis > Regression > [Shade the y range and the x range] > SUMMARY OUTPUT
5500 5000 4500 19000
29000
ˆ = 6533−.0312 y x
ANOVA df Regression Residual Total
Price
Regression Statistics Multiple R 0.806308 R Square 0.650132 Adjusted R Square 0.646562 Standard Error 151.5688 Observations 100
6000
1 98 99
39000 Odometer
SS MS F Significance F 4183528 4183528 182.1056 4.4435E-24 2251362 22973.09 6434890
CoefficientsStandard Error t Stat P-value Intercept 6533.383 84.51232 77.30687 1.22E-89 Odometer -0.03116 0.002309 -13.4947 4.44E-24
49000
6533
Price
6000 5500 5000 4500
0
No data
19000
29000
39000
49000
Odometer
ˆ = 6533−.0312 y x The intercept is b0 = 6533.
This is the slope of the line. For each additional mile on the odometer, the price decreases by an average of $0.031 Do not interpret the intercept as the “Price of cars that have not been driven”
character
CHARACTER OPERATIONS
character -
Inverting the sentence
first assignment
counter: I from I=1 to N
tnemngissa tsrif
counter: J from J=N to 1
inversion - flowchart start
counter: I counter: J
Read SENTENCE J=N
for I = 1 to N
INVERT[J] = SENTENCE[I] PRINT INVERT J = J-1 STOP NEXT I