3

How to convert a symbolic expression to a Octave function from the Symbolic Package?

After installing the symbolic package on octave with pkg install -forge symbolic. Using the symbolic package on octave I may write this:

octave> pkg load symbolic;
octave> a = sym( "a" );
octave> int ( a^2 + csc(a) )

which will result in:

ans = (sym)

   3
  a    log(cos(a) - 1)   log(cos(a) + 1)
  -- + --------------- - ---------------
  3           2                 2

But how to do this Integral (int(1)) symbolic result just above to became a valuable function like this below?

function x = f( x )

    x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2

end

f(3)

# Which evaluates to: 11.6463 +  1.5708i

I want to take the symbolic result from int ( a^2 + csc(a) ), and call result(3), to compute it at 3, i.e., return the numeric value 11.6463 + 1.5708i, from the symbolic expression integral a^2 + csc(a). Basically, how to use the symbolic expression as numerically evaluable expressions? It is the as this other question for Matlab.

References:

  1. http://octave.sourceforge.net/symbolic/index.html
  2. How do I declare a symbolic matrix in Octave?
  3. Octave symbolic expression
  4. Julia: how do I convert a symbolic expression to a function?
  5. What is symbolic computation?
Community
  • 1
  • 1
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • 1
    This value `f(3)` is pretty meaningless since you have no control over the integration constant. You could as well get the mostly real expression `x^3/3 + log( 1 - cos(x) )/2 - log( 1 + cos(x) )/2` as result. Only definite integrals, like the difference `f(3)-f(0)` have results that are independent from the vagaries of the integration algorithm. – Lutz Lehmann Nov 30 '16 at 13:06
  • You are correct, this was just a random example I think of. The goal was to learn how to numerically evaluates symbolic expressions. – Evandro Coan Nov 30 '16 at 13:09

2 Answers2

4

You can use pretty.

syms x;
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2;
pretty(x)

which gives this:

                                     3
log(cos(x) - 1)   log(cos(x) + 1)   x
--------------- - --------------- + --
       2                 2           3

Update (Since the question is edited):

Make this function:

function x = f(y)
    syms a;
    f(a) = int ( a^2 + csc(a) );
    x = double(f(y));
end

Now when you call it using f(3), it gives:

ans =
  11.6463 + 1.5708i
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
2

it seems like you answered your own question by linking to the other question about Matlab.

Octave has an implementation of matlabFunction which is a wrapper for function_handle in the symbolic toolbox.

>> pkg load symbolic;
>> syms x;
>> y = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
y = (sym)

   3
  x    log(cos(x) - 1)   log(cos(x) + 1)
  -- + --------------- - ---------------
  3           2                 2

>> testfun = matlabFunction(y)
testfun =

@(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2

testfun(3)

>> testfun(3)
ans =  11.6463 +  1.5708i

>> testfun([3:1:5]')
ans =

   11.646 +  1.571i
   22.115 +  1.571i
   41.375 +  1.571i

>> testfun2 = matlabFunction(int ( x^2 + csc(x) ))
testfun2 =

@(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2

>> testfun2(3)
ans =  11.6463 +  1.5708i
>> testfun2([3:1:5]')
ans =

   11.646 +  1.571i
   22.115 +  1.571i
   41.375 +  1.571i

I'm sure there are other ways you could implement this, but it may allow you to avoid hardcoding your equation in a function.

Community
  • 1
  • 1
Nick J
  • 1,310
  • 12
  • 25