MATLAB Answers

# How to convert a matrix/vector of symbolic variable into an anonymous function (matlabFunction) ?

50 views (last 30 days)
Punnag Chatterjee on 23 May 2017
Answered: Walter Roberson on 2 Feb 2018
Suppose I have the following:
syms x y
Vec_sym = [1/x, x/(x^2 + y^2), y/x]; %1x3 symbolic vector
Matrix_sym = [1/x, x/(x^2 + y^2), y/x;
2/x, x/(2*x^2 + y^2), y/(6*x);
9/x, 4*x/(x^2 + 2*y^2), 2*y/x];%3x3 symbolic matrix
Vec_func = matlabFunction(Vec_sym ); %Converting to matlabFunction
Matrix_func = matlabFunction(Matrix_sym ); %Converting to matlabFunction
The output of 'Vec_func' or 'Matrix_func' is always a 1x1 matlabFunction, which I do not want. How can I modify this code such that by entering the following, I get:
Vec_func(1,1) = [1, 0.5, 1];
Matrix_func(1,1) = [1, 0.5, 1;
2, 0.333, 0.167;
9, 1.333, 2] %please ignore the precision after decimal
I want to put this function in ode solver, ans using 'subs' and other symbolic operators take a lot of time, which is agonizing. Note that the variables x, y change/update within the ode code and the above is just a simplified example, in the real scenario I have a, say 10x1 vector and 5x5 matrix. Thanks a lot in advance, as the documentation of 'matlabFunctions' did not help me much.
##### 1 CommentShowHide None
Torsten on 23 May 2017
Using the 'File'-option of "matlabFunction" might help.
Best wishes
Torsten.

Sign in to comment.

### Accepted Answer

Steven Lord on 2 Feb 2018
You're confusing two different sizes. The output of matlabFunction is a function handle. Non-scalar function handle arrays are not allowed. But the fact that Vec_func and Matrix_func are scalar function handles is irrelevant in this case. You're not interested in the size of the function handle, you're interested in the size of the array the function handle returns when it is called. That can be non-scalar, and for your particular function handles those arrays are non-scalar.
syms x y
Vec_sym = [1/x, x/(x^2 + y^2), y/x]; %1x3 symbolic vector
Matrix_sym = [1/x, x/(x^2 + y^2), y/x;
2/x, x/(2*x^2 + y^2), y/(6*x);
9/x, 4*x/(x^2 + 2*y^2), 2*y/x];%3x3 symbolic matrix
Vec_func = matlabFunction(Vec_sym ); %Converting to matlabFunction
Matrix_func = matlabFunction(Matrix_sym ); %Converting to matlabFunction
szVec = size(Vec_func)
szMat = size(Matrix_func)
That code displays (in format compact, which just gets rid of a few blank lines):
szVec =
1 1
szMat =
1 1
What happens when you evaluate Vec_func and Matrix_func?
>> Vec_func(1, 1)
ans =
1.0000 0.5000 1.0000
>> Matrix_func(1, 1)
ans =
1.0000 0.5000 1.0000
2.0000 0.3333 0.1667
9.0000 1.3333 2.0000
##### 1 CommentShowHide None
Punnag Chatterjee on 2 Feb 2018
thanks a lot for the explanation.

Sign in to comment.

### More Answers (2)

Torsten on 23 May 2017
I wonder why you don't just define
Vec_func = @(x,y)[1/x, x/(x^2 + y^2), y/x];
Matrix_func = @(x,y) [1/x,x/(x^2 + y^2),y/x ; 2/x, x/(2*x^2 + y^2), y/(6*x); 9/x, 4*x/(x^2 + 2*y^2),2*y/x];
I think the problem with "matlabFunction" is the case when x and y are vectors, not scalars.
Then your (3x3) object becomes a 3x(3*N) object - a case MATLAB doesn't want to handle.
Best wishes
Torsten.
##### 2 CommentsShowHide 1 older comment
Punnag Chatterjee on 2 Feb 2018
Hi, The matrix provided in the question is an example. What I am dealing with is a way a bigger matrix of size, say, 250x3, and I am updating that in ode solver. Is there a way to automate this process of writing the expression as shown below and suggested by Torsten ?
Matrix_func = @(x,y) [1/x,x/(x^2 + y^2),y/x ; 2/x, x/(2*x^2 + y^2), y/(6*x); 9/x, 4*x/(x^2 + 2*y^2),2*y/x];

Sign in to comment.

Walter Roberson on 2 Feb 2018
For use with ode*() functions, see odeFunction() and read through the example to see the workflow to use with the other associated functions.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!