General function in symbolic toolbox

Is it possible in the matlab symbolic toolbox to define general functions, i.e. to define functions only as a general mapping from agruments to output without defining the functional form?
E.g. I would like to define f(x). Then id like to use f(x) in a number of symbolic operations. E.g.
syms x y;
%define general function
Equation=f(x)+y;
Grad=gradient(Equation,[x, y])
Now for grad id like to get Grad = [ derivative(f(x),x) , y ]
Then id be able to youse matlabFunction to create a matlab function from that, replacing all references to f(x) by the relevant functional form.
(I could of course assume this functional form from the beginning but then id have to rerun the code for every change in f(x) and in my application it would also be too slow since matlab would subsitute out f(x) everytime its used.)

 Accepted Answer

Star Strider
Star Strider on 20 May 2014
Edited: Star Strider on 20 May 2014
Beginning with R2012a, it has been possible to define functions in the Symbolic Math Toolbox the way you would normally define functions:
syms t x y
f(x) = x^2 + 2*x + 1
g(t) = 10*exp(-t)
h(x,t) = x^2 - t^2
r1 = f(pi)
r2 = g(5)
r3 = h(3,5)
gradh = gradient(h)
grh = matlabFunction(gradh)
All give the expected numeric and symbolic results.

2 Comments

Cedric
Cedric on 20 May 2014
Edited: Cedric on 20 May 2014
+1 -> 1500!
Star Strider
Star Strider on 20 May 2014
Edited: Star Strider on 20 May 2014
THANK YOU, CEDRIC!!

Sign in to comment.

More Answers (2)

dominik
dominik on 20 May 2014
Hi Thanks for the answer, it seems I havent expressed my question clearly.
In what you propose you define the function explicitly. E.g. f(x) = x^2; Then derivative(f(x)) evaluates to 2*x; This is exactly what i do NOT want to do. Since the function and the operations with the functions are so complex that it makes matlab infeasibly slow.
I want to keep the function as a placeholder only. As if it was a constant, only when deriving treat it as a general function of its arguments (and apply the chain rule). something like f(x)=f(x); Then derivative(f(x),x) evaluates to d_f(x); where f(x) and d_f(x) are unknown functions, all we know is the latter is the derivative of the former

17 Comments

I’m lost.
If you don’t want to calculate the derivatives symbolically and then create anonymous functions so you can use them as part of your non-symbolic code (copy-pasting the results from the Command Window), that seems to me to leave numerical finite difference methods as your only other option.
dominik
dominik on 20 May 2014
Edited: dominik on 20 May 2014
In fact i do all this hassle to avoid finite differences ;)
Assume that I have a function f(x) that i have as a .mat function file. Say the function file also provides derivatives if requested. Assume, this function cant be written symbolically. With matlabfunction i would like to generate a code that only contains refernces to f(x) and its derivatives. Once i have this automatically generated code, i would manually substitute these references by the function itself in the generated code...
Just to explain the background: My f(x) is indeed not implementable symbolically (min, max, if). Furthermore it is highly recursive (chebychev polynomial). Symbolic calculations would substitute out this recursivity and make the function very complicated (so complicated that MATLAB gets far to slow doing the symbolic math).
I went back and refreshed my memory with regard to Chebyshev polynomials. I don’t understand why you’re using symbolic calculations for them at all.
The derivatives (I assume w.r.t. x) are easy enough to calculate in the Symbolic Toolbox, that can then turn them into anonymous functions you can copy-paste from the Command Window into your script file. The Symbolic Math Toolbox isn’t intended for recursive stuff anyway. It’s way too slow.
dominik
dominik on 20 May 2014
Edited: dominik on 20 May 2014
Hi Star Strider, thanks for bearing with me for so long.
I agree, the symbolic toolbox is not for recursive stuff. That's why i want the toolbox to consider the chebychev polynomial function f(x) something like a black box (i.e. a nonspecified function f that maps from x to -say- R).
But f(x) is part of a big equation system G(f(x),x), which (apart from f(x)) is perfectly suitable for the toolbox. So i esenntially want to use the toolbox on the big system G but i dont want the toolbox to "look into" f. Wherever f(x) appears the toolbox should simply consider it a placeholder, wherever i ask the toolbox to derive f(x) the toolbox should simply set a new placeholder d_f(x). Later ill replace these placeholders...
I’m now completely lost.
Why not simply set f(x)=1 for multiplication or division, and f(x)=0 if you use it in an addition or subtraction operation, then keep it in the subsequent equations as a variable? So in the ‘default’ situation, it evaluates to 1 or 0, and if you define it otherwise it evaluates to whatever the function is you’ve defined it to be. That way it doesn’t need to be a ‘placeholder’, and you don’t need the Symbolic Math Toolbox other than to derive your functions and their derivatives.
dominik
dominik on 20 May 2014
Edited: dominik on 20 May 2014
Not sure if i understand your last comment.
Let me describe exactly what im trying to do.
I have to solve an equation system numerically. To do so i want to provide derivatives to the solver. These derivatives I want to calculate symbolically to avoid mistakes.
So first i define my equation syste in the symbolic toolbox. Equ1=G1(x,f(x)), Equ2=G2(x,f(x)) etc where x is a vector and G are functions whose functional form i type down, e.g. Equ1=G1(x,f(x))=x(1)+x(2)^2/f(x)
Then i find the derivatives Grad1=Jacobian(E1,x), Grad2=Jacobian(E2,x) etc.
Then i use matlabfunction to create a function with the outputs [Equ1 Equ2...] and [Grad1 Grad2 ...]
Now the tricky bit is the f(x). If f(x) was a easy function -say log(x(1))- it wouldnt be a problem. I'd define a either symbolic function f=symfun(x(1),x) or equivalently define f=log(x(1)); Matlab would subsitute out f and provide me the gradients.
Yet my f is a function of many lines, it is recursive (hence not very suitable for he toolbox) and furthermore it contains max mins and if conditions(hence cant be written in the toolbox), so i cant just define f. Thats why i would like matlab to simply write f wherever f appears and to write d_f whenever it has to derive f. Once i have the matlabfunction code i can then replace f and d_f using the complicated (but fast since recursive) external function.
I dont understand what you meant with setting f(x)=1 or 0 depending on the operation. I could replace the function f simply by the symbol f. This would be fine if i didnt derive or integrate. But since i need the derivatives, i cant do that because f would get lost since it would be considered a constant...
It would be only considered a constant as long as you needed it as a placeholder. When you want to define it as a function, just do that.
I’m getting more lost with each iteration. I guess I need a more concrete example rather than hypothetical situations.
dominik
dominik on 20 May 2014
Edited: dominik on 20 May 2014
I just saw that mathematica has the concept of an unspecified function. Im looking for this concept in matlab. Maybe, if you are familiar with mathematica as well, the comparison helps.
Here is a 1 line example: Mathematica finds the derivative of a function that contains the undefined function f.
In[8]:= D[x + 2 x + x^2 + f[x], x]
Out[8]= 3 + 2 x + Derivative[1][f][x]
This code:
syms x f(x)
Z = diff(x + 2*x + x^2 + f(x), x)
produces this result:
Z =
2*x + diff(f(x), x) + 3
Does that do what you want?
YES!
I was not aware that it was possible to declare the function f(x) without assigning a functional form. Im very sorry I didnt express myself clearly in the beginning. Thanks a lot for your patience, im happy you provided me with such a simple solution!
My pleasure!
That’s a recent addition — probably R2012a or later.
It’s not uncommon that it’s difficult to describe a problem. That’s usually the reason it takes a few iterations here on MATLAB Answers to figure out the best solution.
Hi, I have a follow up question to yesterday. Tha matlabFunction seems to do a mistake with these undefined functions.
It seems to work all right if there is only one reference to the unknown function, but not if there is more than one. Test this code: only the first call of matlabFunction gives me the right output.
syms x y z f(x,y,z)
E=x + y + f(x,y,z)
D1= diff(E,x)
D2= [diff(E,x) diff(E,y) diff(E,z)]
D3= jacobian(E)
%note D2=D3
matlabFunction(D1,'file','NlconFunc','vars',{x,y,z});
matlabFunction(D2,'file','NlconFunc','vars',{x,y,z});
matlabFunction(D3,'file','NlconFunc','vars',{x,y,z});
For the last two calls of matlabfunction the enerated code is (matlab 2014a)
function D3 = NlconFunc(x,y,z)
%NLCONFUNC
% D3 = NLCONFUNC(X,Y,Z)
% This function was generated by the Symbolic Math Toolbox version 6.0.
% 21-May-2014 17:03:56
t2 = f(x,y,z);
D3 = [1.0,1.0,0.0];
matlabFunction doesn’t seem to embrace uncertainties:
Warning: Function "f" is not verified to be a valid MATLAB function.
I haven’t used matlabFunction with undefined functions, so this is all new to me. The only difference I can see between D1 and the others is that E is an explicit function of x and y but not z. Seems diff(E,z) could be the problem.
Strangly I dont get the warning you report...
"Seems diff(E,z) could be the problem." Unfortunatley not. Defining
E=x + y + z + f(x,y,z)
Doesnt solve the issue. Do you know someone who might know?
Yes, actually, but not personally: Contact Support!
Hi Dominik. Were you ever able to get a resolution of this issue? I have the same one. Thanks! leo

Sign in to comment.

I know this is an old question, but I was looking for this too and it took me a while to figure it out. The proper solution is the following:
syms x f(x)
Then you can do stuff like
fp = diff(f, x); % Compute derivative
And if you do end up creating an actual function later, like
F = x.^2 + 3;
then you can use the 'subs' function to substitute your general function with a specific one, like
subs(fp, f, F) % outputs 2*x

Categories

Community Treasure Hunt

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

Start Hunting!