Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve Math Functions PL/SQL

I need to resolve a math equation/function in pl/sql.
What kind of math operations/functions are available in oracle pl/sql that could help me resolve a math function like this :

(3.5/(1+x))+(3.5/(1+x)^2)+(3.5/(1+x)^3)+(3.5/(1+x)^4)+(100/(1+x)^4)=101.55

I want a function to resolve this statement and find out the value of x.

Something like this is what I am looking for

Any help? Thanks.

like image 589
Hélder Gonçalves Avatar asked Feb 06 '13 11:02

Hélder Gonçalves


2 Answers

Alas the Oracle database is not a mathematical tool. It has lots of arithmetical and statistical functions but it doesn't have built-in functionality capable of interpreting equations. Sorry.


By sheer coincidence Marc (AKA Odie_63) has recently published a Reverse Polish Notation calculator which he has written in PL/SQL. It doesn't do precisely what you want but I'm including a link for the benefit of any seekers who may stumble upon this thread in the future. Find out more.

like image 107
APC Avatar answered Nov 18 '22 10:11

APC


As APC said, there is no built-in functionality to do this. But you can use the WolframAlpha API from PL/SQL:

declare
    v_equation varchar2(32767) := 
        '(3.5/(1+x))+(3.5/(1+x)^2)+(3.5/(1+x)^3)+(3.5/(1+x)^4)+(100/(1+x)^4)=101.55';
    v_escaped_url varchar2(32767);
    v_uri httpuritype;
    v_xml xmltype;
    v_count number := 1;
begin
    --Escape the URL.
    --I used chr(38) for ampersand, in case your IDE think it's a substitution variable
    v_escaped_url :=
        'http://api.wolframalpha.com/v2/query?appid=EQGHLV-UYUEYY9ARU'||chr(38)||'input='
        ||utl_url.escape(v_equation, escape_reserved_chars => true)
        ||chr(38)||'format=plaintext';

    --Create an HTTPURIType, and get the XML
    v_uri := httpuritype.createUri(v_escaped_url);
    v_xml := v_uri.getXML;

    --Complex solutions
    while v_xml.existsNode('/queryresult/pod[@title="Complex solutions"]/subpod['||v_count||']') = 1 loop
        dbms_output.put_line(v_xml.extract('/queryresult/pod[@title="Complex solutions"]/subpod['||v_count||']/plaintext/text()').getStringVal());
        v_count := v_count + 1;
    end loop;

    --Real solutions
    v_count := 1;
    while v_xml.existsNode('/queryresult/pod[@title="Real solutions"]/subpod['||v_count||']') = 1 loop
        dbms_output.put_line(v_xml.extract('/queryresult/pod[@title="Real solutions"]/subpod['||v_count||']/plaintext/text()').getStringVal());
        v_count := v_count + 1;
    end loop;
end;
/

Results:

x = -1.00006-0.996229 i
x = -1.00006+0.996229 i
x = -1.99623
x = 0.0308219

There are a lot of potential downsides to this approach. It will be very slow, and the API is not free. My example works because I used my free developer appid, but it's only good for a small number of calls.

like image 32
Jon Heller Avatar answered Nov 18 '22 10:11

Jon Heller