General function in symbolic toolbox
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
dominik
am 20 Mai 2014
Beantwortet: Martin Ahrnbom
am 16 Jun. 2020
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 Kommentare
Akzeptierte Antwort
Star Strider
am 20 Mai 2014
Bearbeitet: Star Strider
am 20 Mai 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 Kommentare
Weitere Antworten (2)
Martin Ahrnbom
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Conversion Between Symbolic and Numeric finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!