Vous êtes sur la page 1sur 34

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

x
A 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 ) ) 
P  
B
Area = ∫ F ( x)dx = ×  + 4 × F ( X 0+ 3( p ) ) + ... + 
A
3  
 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
parabolic approach
Xn : B
Simpson Method – analytical vs. numerical
2

∫ − + 2)dx = ???
3 2
( 4 x 3 x
1

[ ]
2
2
∫ − + = − + 2x 1
3 2 4 3
( 4 x 3 x 2) dx x x =12-2 = 10
1

Numerically, assumed that p=0.1

(4 • 13 − 3 • 12 + 2) 
 
+ 4(4 • 1.1 − 3 • 1.1 + 2) + 2((4 • 1.2 − 3 • 1.2 + 2)
3 2 3 2

= ( p / 3) × + ... 
 
+ 2(4 • 1.8 − 3 • 1.8 + 2) + 4(4 • 1.9 − 3 • 1.9 + 2) 
3 2 3 2

 
+ (4 • 2 − 3 • 2 + 2)
3 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

I.S iteration #
L.C.

• 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_simpsoncurrentcalculation
LastCalc  pervious calculation

Read F(x), A, P width, Nnumber of sub classes


B, ε
Integral_Simpson=
Integral_Simpson=0 P/3*SUM
P=(B-A)/2

ABS [ Integral_Simpson-LastCalc)/
FOR I=1 TO 20 Integral_Simpson]
< ε ??
NO
LastCalc = Integral_Simpson P=P/2 Print
N=(B-A)/P Integral_Simpson
Next I

FOR J=2,4, …, N-2


Print
SUM = SUM + 2xF(A+JxP) "can' be found
+4xF(A+(J+1)xP) within 20 iterations"
Stop
Integral Calculation – Rectangle
B
y
Area = ∫ F ( x ) dx
A
Y = F(x)

x
A 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
∫ − + = − + 2x 1
3 2 4 3
( 4 x 3 x 2) dx x x
1

Numerically, assumed that p=0.1

(4 • 13 − 3 • 12 + 2) 
 
+ (4 • 1.1 − 3 • 1.1 + 2) + (4 • 1.2 − 3 • 1.2 + 2) 
3 2 3 2

= ( p / 2) × + ... 
 
+ (4 • 1.8 − 3 • 1.8 + 2) + (4 • 1.9 − 3 • 1.9 + 2)
3 2 3 2

 
+ (4 • 2 − 3 • 2 + 2)
3 2


= 9.075
Rectangle – the error

Integral _ Re ct − LastCalc

Integral _ Re ct

Analytical

Area

iteration #
Rectangle - flowchart
Integral_rectcurrentcalculation
Start LastCalc  pervious calculation
P width, Nnumber of sub classes

Read F(x), A,
B, ε
Integral_Rect=
Integral_rect=0 P*SUM
P=(B-A)/2

ABS [ Integral_Rect-LastCalc)/
FOR I=1 TO 20 Integral_Rect]
< ε ??
NO
LastCalc = Integral_Rect P=P/2 Print
N=(B-A)/P Integral_Rect
Next I

FOR J=0 to N-1


Print
"can' be found
SUM = SUM + F(A+JxP)
within 20 iterations"
Stop
Integral Calculation – Trapezoid
B
y
Area = ∫ F ( x ) dx
A
Y = F(x)

x
A B
 F ( X 0 ) + 2 × F ( X 0+1( p ) ) + 2 × F ( X 0+ 2( p ) )
 
P  + 2 × F ( X 0+ 3( p ) ) + ...
B

Area = ∫ F ( x)dx = ×   =12-2 = 10
2 + 2 × F ( X ) + 2 × F ( X )
A
 ( n − 2 )( p ) ( n − 1)( p )

 + F ( X ( n )( p ) ) 
Trapezoid
[ ]
2
2
∫ − + = − + 2x 1
3 2 4 3
( 4 x 3 x 2) dx x x
1

Numerically, assumed that p=0.1

(4 • 13 − 3 • 12 + 2) 
 
+ 2(4 • 1.1 − 3 • 1.1 + 2) + 2((4 • 1.2 − 3 • 1.2 + 2)
3 2 3 2

= ( p / 2) × + ... 
 
+ 2(4 • 1.8 − 3 • 1.8 + 2) + 2(4 • 1.9 − 3 • 1.9 + 2) 
3 2 3 2

 
+ (4 • 2 − 3 • 2 + 2)
3 2


= 11.125
Trapezoid - flowchart
Integral_trapcurrentcalculation
Start LastCalc  pervious calculation
P width, Nnumber of sub classes

Read F(x), A,
B, ε
Integral_Trap=
Integral_trap=0 P*SUM
P=(B-A)/2

ABS [ Integral_Trap-LastCalc)/
FOR I=1 TO 20 Integral_Trap]
< ε ??
NO
LastCalc = Integral_Trap P=P/2 Print
N=(B-A)/P Integral_Trap
Next I

FOR J=0 to N-1


Print
SUM = SUM + F(A+JxP) "can' be found
+F(A+(J+1)xP) 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
Dependent Variable
(x2)
BLACK
(y)
BOX
Independent Variable -3
(x3)

Independent Variable -4
(x4)

relationships ??
The Model
 Thefirst order linear model or a simple
regression model,
y = A + Bx

y = dependent variable
A and B are unknown,
x = independent variable
ytherefore, are estimated
Α = y-intercept from the data.

Β = slope of the line


ε = error variable 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.

Sum of squared differences


(2 - 1)
= (4
2
+- 2)(1.5
2
+ - 3)(3.2
2
+ - 4)2 = 6.89
Sum of squared differences
(2 -2.5)
= (4 2
+- 2.5)
(1.5
2
+ - 2.5)
(3.2
2
+- 2.5)2 = 3.99
Let us compare two lines
4 (2,4)
 The second line is horizon
3 (4,3.2)
2.5
2
(1,2)
(3,1.5)The smaller the sum of
1 squared differences
the better the fit of the
1 2 3
line
4
to the data.
Least Square method
y = A + Bx

where: (∑ xi ∑ yi )
∑ xiyi −
ˆ n ∑ xiyi − nx y
βB1 = 2
or 2 2
2 (∑ x i ) ∑ xi − n x
∑ 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
Car Odometer Price
the relationship between 1 37388 5318
the odometer reading and 2 44758 5061
the selling price of used 3 45833 5008
4 30862 5795
cars. 5 31705 5784
 A random sample of 100 6 34010 5359
cars is selected, and the . . .
. . .
data. . . .
 Find the regression line.
Independent variable x
Dependent variable
Least Square Method – flow chart (∑ xi ∑ yi )
∑ xiyi −
n ∑ xiyi − nx y
βˆB1-=# of data
N 2
or 2 2
2 (∑ xi ) ∑ xi − nx
START ∑ xi −
n
βˆ0 = yYbar
A − βˆ1x- B Xbar
Read N

SUMX = 0; SUMY = 0 AVEX = SUMX /N


SUMXY=0; SUMXX=0 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
6000

Regression Statistics 5500

Price
Multiple R 0.806308 5000
R Square 0.650132
Adjusted R Square
0.646562
4500
Standard Error
151.5688 19000 29000 39000 49000
Observations 100 Odometer
ŷ = 6533−.0312
x
ANOVA
df SS MS F Significance F
Regression 1 4183528 4183528 182.1056 4.4435E-24
Residual 98 2251362 22973.09
Total 99 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
6533
6000
5500

Price
5000
4500
0 No data 19000 29000 39000 49000
Odometer

ŷ = 6533−.0312
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
counter: I
start
counter: J

Read SENTENCE

J=N

for I = 1 to N

INVERT[J] = SENTENCE[I]
PRINT INVERT

J = J-1
STOP

NEXT I

Vous aimerez peut-être aussi