Search code examples
matlabvariablessymbolic-mathdeclaresymbolic-computation

Declare variables returned with symvar (MATLAB)


I'm trying to declare a function (n-dimensional) and its variables from an input, regardless of how many variables I have in this function. Example:

input_func = '3*x*y^2 + x^3 -3*x'

I use symvar to get these variables and it works just fine:

>> test = symvar('3*x*y^2 + x^3 -3*x')

test = 

    'x'
    'y'

Unfortunately, I can't use this output to declare these variables directly. Could you help me with that?

Btw, I'm using MATLAB R2013a


Solution

  • How about:

    >> vars = symvar('3*x*y^2 + x^3 -3*x');
    >> syms(vars{:})
    >> whos x y
      Name      Size            Bytes  Class    Attributes
    
      x         1x1               112  sym                
      y         1x1               112  sym