How to convert a matrix/vector of symbolic variable into an anonymous function (matlabFunction) ?
Mostrar comentarios más antiguos
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 comentario
Torsten
el 23 de Mayo de 2017
Using the 'File'-option of "matlabFunction" might help.
Best wishes
Torsten.
Respuesta aceptada
Más respuestas (2)
Torsten
el 23 de Mayo de 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 comentarios
Karan Gill
el 25 de Mayo de 2017
Yes, I'm curious to know this answer doesn't work for you. Commenting so I can track this.
Punnag Chatterjee
el 2 de Feb. de 2018
Walter Roberson
el 2 de Feb. de 2018
0 votos
For use with ode*() functions, see odeFunction() and read through the example to see the workflow to use with the other associated functions.
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!