If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Definite Integral Calculation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-25-09, 15:27
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Arrow Definite Integral Calculation

Definite Integral interval [a, b]: Sf(x)dx = G(b) - G(a) where G'(x) = f(x) , or differential of G(x) = f(x).

Integrals appear in many practical situations.
Consider a swimming pool. If it is rectangular, then from its length, width, and depth we can easily determine the volume of water it can contain (to fill it), the area of its surface (to cover it), and the length of its edge (to rope it).

But if it is oval with a rounded bottom, all of these quantities call for integrals. Practical approximations may suffice for such trivial examples, but precision engineering (of any discipline) requires exact and rigorous values for these elements.

I create the query which calculate integral of 3 * X^2 - 2 * X in interval [2, 4]....

S(3 * X^2 - 2 * X)dx = X^3 - X^2 and exact value of integral will be
S = (4^3 - 4^2) - (2^3 - 2^2) = 48 - 4 = 44.

We know nothing in our solution about exact value and differential and suppose to calculate approximate value of integral.
Process will stop when the absolute difference between current value of integral and previous become less or equal some eps real number.

Code:
With
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(2), double(4), '3 * X^2 - 2 * X', double(1.e-3)
   from sysibm.sysdummy1 
)
,
Integral_calc (Xs, Xf, Xc, step, curint, prevint, eps, iterno) as
(select Xstart, Xfinish, double(Xstart - (Xfinish - Xstart) / 10.) Xc, 
                double((Xfinish - Xstart) / 10.) step,  double(0), double(0), eps, int(0) 
 from Source
union All
select Xs, Xf, Xc + step, step, curint + Fc * step, prevint, eps, iterno + 1
  from Integral_calc, table
(select 3 * power(Xc + step, 2) - 2 * (Xc + step) Fc 
   from sysibm.sysdummy1 ) it
where  Xc + step <= Xf 
Union All
select Xs, Xf, Xs - (step / 2.), step / 2., 0., curint, eps, iterno + 1
  from Integral_calc
where Xc + step > Xf 
  and abs(curint - prevint) > eps
)
,
Integral(integral_value, integral_image) as 
(select curint, 'interval: [' || varchar(Xs) || ', ' || varchar(Xf) || ']:  S' || '(' 
                              || imgFunc || ')dx = ' || varchar(round(curint, 3))    
from Integral_calc, Source 
where iterno = (select max(iterno) from Integral_calc)
)
select integral_value, integral_image from Integral
Result of calculation:

Quote:
INTEGRAL_VALUE............................ INTEGRAL_IMAGE
4.40005859378289E+001............... interval: [2.0E0, 4.0E0]: S(3 * X^2 - 2 * X)dx = 4.4001E1
Could be very useful for students and engineers.

Lenny
Reply With Quote
  #2 (permalink)  
Old 09-25-09, 15:50
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Post

This query is working fast for short intervals.

Do not use it for intervals with length > 10....

Also, if you want calculate yours integral, you have to change
Code:
table (select 3 * power(Xc + step, 2) - 2 * (Xc + step) Fc 
   from sysibm.sysdummy1 ) it
and

Code:
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(1), double(12), '3 * X^2 - 2 * X', double(1.e-3)
   from sysibm.sysdummy1
Lenny
Reply With Quote
  #3 (permalink)  
Old 09-25-09, 23:42
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Lenny, do you think somebody here remember what is it "definite integral" ?

Kara S.
Reply With Quote
  #4 (permalink)  
Old 09-26-09, 23:15
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Post Just better look

For me looks better if looks so:

Code:
With
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(2), double(4), '3 * X^2 - 2 * X', double(1.e-3)
   from sysibm.sysdummy1 
)
,
Integral_calc (Xs, Xf, Xc, step, CurInt, PrevInt, eps, iterno) as
(select Xstart, Xfinish, double(Xstart - (Xfinish - Xstart) / 10.) Xc, 
                double((Xfinish - Xstart) / 10.) step,  double(0), double(0), eps, int(0) 
 from Source
union All
select Xs, Xf, Xc + step, step, CurInt + FuncVal * step, PrevInt, eps, 
        iterno + 1

From Integral_calc, 
table (select 3 * power(X, 2) - 2 * X  as FuncVal 
           From 
           (select (Xc + step) as X rom sysibm.sysdummy1 ) ii     ) it
where  Xc + step <= Xf 

Union All
select Xs, Xf, Xs - (step / 2.), step / 2., 0., CurInt, eps, iterno + 1
  from Integral_calc
where Xc + step > Xf 
  and abs(curint - prevint) > eps
)
,
Integral(integral_value, integral_image) as 
(select curint, 'interval: [' || varchar(Xs) || ', ' || varchar(Xf) || ']:  S' || '(' 
                              || imgFunc || ')dx = ' || varchar(round(curint, 3))    
from Integral_calc, Source 
where iterno = (select max(iterno) from Integral_calc)
)
select integral_value, integral_image from Integral
Kara S.
Reply With Quote
  #5 (permalink)  
Old 09-27-09, 00:00
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by DB2Plus
Lenny, do you think somebody here remember what is it "definite integral" ?

Kara S.
No, of course not. You two guys are the only ones to carry the torch of knowledge. All the rest are just some lowly yeomen capable of no more than herding tablespaces...
Reply With Quote
  #6 (permalink)  
Old 09-27-09, 02:03
affek affek is offline
SPAMMER
 
Join Date: Sep 2009
Posts: 4
wow that is so difficul for me...to bad
Reply With Quote
  #7 (permalink)  
Old 09-27-09, 08:14
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Quote:
Originally Posted by n_i
No, of course not. You two guys are the only ones to carry the torch of knowledge. All the rest are just some lowly yeomen capable of no more than herding tablespaces...
Sorry, if I did make mistake.

Kara S.
Reply With Quote
  #8 (permalink)  
Old 09-27-09, 12:39
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Lightbulb

In my opinion when interval includes special numbers pi or e, we have to use something special,
because we don't know exact values of these two numbers and have to use scalar functions, e.g.
pi = 2 * asin(1.0) , e = exp(1.)

Kara S.

Last edited by DB2Plus; 09-27-09 at 15:49.
Reply With Quote
  #9 (permalink)  
Old 09-27-09, 13:15
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Post Example with trigonometric function

For example: interval [0, 3/4pi] S sin(x)dx:

Code:
With
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(0), 1.5 * asin(1.), 'sin(x)', double(1.e-4)
   from sysibm.sysdummy1 
)
,
Integral_calc (Xs, Xf, Xc, step, CurInt, PrevInt, eps, iterno) as
(select Xstart, Xfinish, double(Xstart - (Xfinish - Xstart) / 10.) Xc, 
                double((Xfinish - Xstart) / 10.) step,  double(0), double(0), eps, int(0) 
 from Source
union All
select Xs, Xf, Xc + step, step, CurInt + FuncVal * step, PrevInt, eps, 
        iterno + 1

From Integral_calc, 
table (select sin(x) as FuncVal 
           From 
           (select (Xc + step) as X rom sysibm.sysdummy1 ) ii     ) it
where  Xc + step <= Xf 

Union All
select Xs, Xf, Xs - (step / 2.), step / 2., 0., CurInt, eps, iterno + 1
  from Integral_calc
where Xc + step > Xf 
  and abs(curint - prevint) > eps
)
,
Integral(integral_value, integral_image) as 
(select curint, 'interval: [' || varchar(Xs) || ', ' || varchar(Xf) || ']:  S' || '(' 
                              || imgFunc || ')dx = ' || varchar(round(curint, 4))    
from Integral_calc, Source 
where iterno = (select max(iterno) from Integral_calc)
)
select integral_value, integral_image from Integral
Kara S.
Reply With Quote
  #10 (permalink)  
Old 09-27-09, 17:20
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Exclamation Some notes

Quote:
Originally Posted by Lenny77
This query is working fast for short intervals.

Do not use it for intervals with length > 10....

Also, if you want calculate yours integral, you have to change
Code:
table (select 3 * power(Xc + step, 2) - 2 * (Xc + step) Fc 
   from sysibm.sysdummy1 ) it
and

Code:
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(1), double(12), '3 * X^2 - 2 * X', double(1.e-3)
   from sysibm.sysdummy1
Lenny
Hi, Lenny !
We have to change
Code:
Integral(integral_value, integral_image) as 
(select curint, 'interval: [' || varchar(Xs) || ', ' || varchar(Xf) || ']:  S' 
                                   || '('  || imgFunc || ')dx = ' 
                                   || varchar(round(curint, 3))    
from Integral_calc, Source 
where iterno = (select max(iterno) from Integral_calc)
Because of
Quote:
round(curint, 3)
depends on EPS

Kara S.
Reply With Quote
  #11 (permalink)  
Old 09-29-09, 11:20
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation Small correction to trigonometric integral

Thank you Kara for yours trigonometric example !

One small correction:

Code:
With
Source (Xstart, Xfinish, imgFunc, eps) as
(select double(0), 1.5 * asin(1.), 'sin(x)', double(1.e-4)
   from sysibm.sysdummy1 
)
,
Integral_calc (Xs, Xf, Xc, step, CurInt, PrevInt, eps, iterno) as
(select Xstart, Xfinish, double(Xstart - (Xfinish - Xstart) / 10.) Xc, 
                double((Xfinish - Xstart) / 10.) step,  double(0), double(0), eps, int(0) 
 from Source
union All
select Xs, Xf, Xc + step, step, CurInt + FuncVal * step, PrevInt, eps, 
        iterno + 1

From Integral_calc, 
table (select sin(x) as FuncVal 
           From 
           (select (Xc + step) as X from sysibm.sysdummy1 ) ii     ) it
where  Xc + step <= Xf 

Union All
select Xs, Xf, Xs - (step / 2.), step / 2., 0., CurInt, eps, iterno + 1
  from Integral_calc
where Xc + step > Xf 
  and abs(curint - prevint) > eps
)
,
Integral(integral_value, integral_image) as 
(select curint, 'interval: [' || varchar(Xs) || ', ' || varchar(Xf) || ']:  S' || '(' 
                              || imgFunc || ')dx = ' || varchar(round(curint, 4))    
from Integral_calc, Source 
where iterno = (select max(iterno) from Integral_calc)
)
select integral_value, integral_image from Integral
and it works !

Result:

Quote:
interval: [0E0, 2.35619449019234E0]: S(sin(x))dx = 1.7072E0
Lenny
Reply With Quote
  #12 (permalink)  
Old 09-29-09, 11:57
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Check the result

We can check the result of calculation (less then 1 second).

We know, or you can find in the smart books:
Quote:
S(six(x))dx = -cos(x)

So, on interval: [0E0, 2.35619449019234E0] exact value of integral will be:

Code:
select -cos(2.35619449019234E0) - (-cos(0E0)) 
from sysibm.sysdummy1
E.g.
Quote:
S(six(x))dx = 1.70710678118654E+000
Lenny
Reply With Quote
  #13 (permalink)  
Old 09-29-09, 21:16
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Thank you, Lenny.

But where is my mistake ?

Kara S.

Last edited by DB2Plus; 09-29-09 at 21:21.
Reply With Quote
  #14 (permalink)  
Old 09-29-09, 21:19
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Post

Lenny, thanks.

Now I found my mistake:

(select (Xc + step) as X rom sysibm.sysdummy1 ) ii ....

where has to be:

Code:
(select (Xc + step) as X From sysibm.sysdummy1 ) ii ....
Sorry, everybody!
I have made this query without test, because I don't have DB2 at my home.

Kara S.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On