General function in symbolic toolbox
16 次查看(过去 30 天)
显示 更早的评论
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.)
0 个评论
采纳的回答
Star Strider
2014-5-20
编辑:Star Strider
2014-5-20
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 个评论
更多回答(2 个)
Martin Ahrnbom
2020-6-16
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!