General function in symbolic toolbox
Show older comments
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
More Answers (2)
dominik
on 20 May 2014
0 votes
17 Comments
Star Strider
on 20 May 2014
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.
Star Strider
on 20 May 2014
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.
Star Strider
on 20 May 2014
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
on 20 May 2014
Star Strider
on 20 May 2014
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.
Star Strider
on 20 May 2014
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?
dominik
on 21 May 2014
Star Strider
on 21 May 2014
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.
Star Strider
on 21 May 2014
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.
dominik
on 21 May 2014
Star Strider
on 21 May 2014
Martin Ahrnbom
on 16 Jun 2020
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
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!