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 > Equation Solver (plain DB2)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-09, 15:38
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Equation Solver (plain DB2)

Using the DB2 power it is a very easy to create the simple Equation Solver.

For example you have to find the root of Equation 2 * SIN(X) = X.

Transform this equation to f(X) = 2 * SIN(X) - X = 0.

One root could be find between 1 and 2 because of
f(1) * f(2) = (2 *Sin(1) - 1) * (2 * Sin(2) - 2) = -1.23889187945779E-001 < zero.

So Xleft = 1 and Xright = 2....

When we find the boundaries we will iterate by finding the middle of the interval:

So the first iteration of the X will be (1 + 2) / 2 = 1.5.

Then we find where f(X) * f(Xright), or f(X) * f(Xleft) is less then 0.

If f(X) * f(Xright) > 0 we change Xright on (Xleft + Xright) / 2,
If f(X) * f(Xleft) > 0 we change Xleft on (Xleft + Xright) / 2.

We will finish the process when abs (Xleft - Xright) <= eps


Quote:
with control_tbl (Equation, Xleft, Xright, eps) as
(select '2 * sin(X) = X ==> 2 * sin(X) - X = 0', double(1), double(2),
double(1.e-12)
from sysibm.sysdummy1
)
,
eq_solution (Equation, Xleft, Xright, eps, X) as
(select ct.*, (Xleft + Xright) / 2
from control_tbl ct
union all
select Equation,
case
when (2 * sin ((Xleft + Xright) / 2) - (Xleft + Xright) / 2) *
(2 * sin (Xleft) - Xleft) > 0
then (Xleft + Xright) / 2
else Xleft
end,
case
when (2 * sin ((Xleft + Xright) / 2) - (Xleft + Xright) / 2) *
(2 * sin (Xright) - Xright) > 0
then (Xleft + Xright) / 2
else Xright
end,
eps,
(Xleft + Xright) / 2
from eq_solution
where abs(Xleft - Xright) > eps
)
select Equation, abs(Xleft - Xright) "Real Accurancy", X "Equation Solution"
from eq_solution
where abs(Xleft - Xright) <= eps
Lenny K.
Reply With Quote
  #2 (permalink)  
Old 08-10-09, 10:50
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
We can make better and compacter view with the small transform of the SQL statement:

Quote:
with control_tbl (Equation, Xleft, Xright, eps) as
(select '2 * sin(X) = X ==> 2 * sin(X) - X = 0',
double(-2), double(-1), double(1.e-12)
from sysibm.sysdummy1
)
,
eq_solution (Equation, Xleft, Xright, eps, X) as
(select ct.*, (Xleft + Xright) / 2
from control_tbl ct
union all
select Equation,
case
when (md.E_Function * lf.E_Function ) > 0
then (Xleft + Xright) / 2
else Xleft
end,
case
when (md.E_Function * rt.E_Function ) > 0
then (Xleft + Xright) / 2
else Xright
end,
eps,
(Xleft + Xright) / 2
from eq_solution eq,
table(select 2 * sin (X) - X as E_Function
from (select Xleft as X from sysibm.sysdummy1 ) ii) lf,
table(select 2 * sin (X) - X as E_Function
from (select Xright as X from sysibm.sysdummy1 ) ii) rt,
table(select 2 * sin (X) - X as E_Function
from (select (Xleft + Xright) / 2 as X from sysibm.sysdummy1 ) ii) md

where abs(Xleft - Xright) > eps
)
select Equation, abs(Xleft - Xright) "Real Accurancy", X "Equaty Solution"
from eq_solution
where abs(Xleft - Xright) <= eps
Result: X = -1.89549426703434

Lenny
Reply With Quote
  #3 (permalink)  
Old 10-03-09, 22:52
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Not bad, I have checked.

But what we have to do with equation
SIN(X) = 0 on interval [p/2, p/2 + 2pi] having the same sign on the ends:

SIN(pi/2) = +1 and SIN(p/2 + 2pi) = +1 ?

Same time this equation has 2 roots on this interval:
SIN(pi) = 0 and SIN(2pi) = 0 are solutions.

Kara S.
Reply With Quote
  #4 (permalink)  
Old 10-05-09, 16:08
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question How to define the step to locate the root

Quote:
Originally Posted by DB2Plus
Not bad, I have checked.

But what we have to do with equation
SIN(X) = 0 on interval [p/2, p/2 + 2pi] having the same sign on the ends:

SIN(pi/2) = +1 and SIN(p/2 + 2pi) = +1 ?

Same time this equation has 2 roots on this interval:
SIN(pi) = 0 and SIN(2pi) = 0 are solutions.

Kara S.
This change is not too complicated I think.

Problem is in definition of step length to locate the root.

Lenny.
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