How automatic insert (once defined) symbolic expression from script or workspace to function?

Hi. I have symbolic expressions in a script file (that represent calculated partial derivatives. Because I need, at each iteration, that get the numeric value of the symbolic expression depending on the new set values of variables, I created a function, that compute this) Since that symbolic expression is different from case to case, is there a possibility that the expression automatic import in function without manual typing these expressions. I want to define this expression in one place in the script file, and than when you call functions, this expression be automatically loaded in function. Thank you in advance.

 Réponse acceptée

Perhaps you want to use matlabFunction() ?

5 commentaires

I want to explain may problem. In script file I defined 'fs' as symbolic vector on this way.
% symbolic variables are x1s, x2s, p1s, p2s, p3s, p4s and p5s
syms x1s, x2s, p1s, p2s, p3s, p4s, p5s
fs=[sym(p1s*x1s^2+sin(p3s*x2s)); sym(p2s*exp(-x1s)*x2s)];
In each iteration, I want to compute numeric value of this expression for different values of symbolic variables. I first try to solve my problem with symbolic substitution, subs(old, new), and this work, but it is so slow for my purposes. For example, for 3000 iterations, whole script file executes for 1.5 [min].
Because I formed user function
fk=state(xk(1,k-1), xk(2,k-1), xk(3,k-1), xk(4,k-1), xk(5,k-1), xk(6,k-1), xk(7,k-1));
where xk is vector with 7 values of variables
x1s - xk(1,k-1);
x2s - xk(2,k-1);
p1s - xk(3,k-1);
.
.
.
;p5s - xk(7,k-1);
Then I formed user function as
function [f]=state(x1s, x2s, p1s, p2s, p3s, p4s, p5s)
f=[p1s*x1s^2+sin(p3s*x2s); p2s*exp(-x1s)*x2s];
end
On this way I avoid difficulty in calculating with symbolic expression, and this work very fast, etc. whole algorithm is executed for only 1-2 [sec].
Now it would mean a lot to me if I find way that Matlab automatic call this vector function 'f' from script file, without my typing this expression. Since expression 'fs' represents some partial derivative, which is different from case to case, it would be nice that this vector function 'fs' be called from one place in script file.
I try with matlabFunction() but, it doesn't work.
syms x1s, x2s, p1s, p2s, p3s, p4s, p5s
fs=[sym(p1s*x1s^2+sin(p3s*x2s)); sym(p2s*exp(-x1s)*x2s)];
state = matlabFunction( fs, 'vars', {x1s, x2s, p1s, p2s, p3s, p4s, p5s} );
Thank you dear Walter. The same can be achieved if we take
f = matlabFunction(fs);
If we then use command for automatic save and form function 'state'
g=matlabFunction(f, 'file','state');
fk=state(xk(1,k-1), xk(2,k-1), xk(3,k-1), xk(4,k-1), xk(5,k-1), xk(6,k-1), xk(7,k-1));
Thank you again, you sent me to the right way.
Be careful, matlatFunction idea of which order the variables should be in are not necessarily the same as yours. To avoid surprises I recommend using the 'vars' parameter if there are multiple variables.
I tried it and I got burned. You're right, it must be noted the order of the variables using the 'vars'. Greetings!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by