how can a symbolic derivative be vectorized automatically?
3 views (last 30 days)
Show older comments
Hello,
here is the simplified problem.
I have a symblic function with three arguments. And the derivative of f wrt. b
syms a b c
f = a*sin(b)*exp(c)
df = diff(f,b)
The solution is:
df = a*exp(c)*cos(b)
Now i will use this derivatve in a numeric script. And the argumetns a,b,c are vectors:
a=rand(10,1);
b=rand(10,1);
c=rand(10,1);
My current approach is to modify the derivative manualy: (adding "dots")
df = a.*exp(c).*cos(b)
Is there a way how i can change a symblic expression automatically?
My equations are has 80 or more characters and the derivatives has 2x to 3x the amount of characters.
So an automatic transform into an vectorized version would help me a lot.
Best regards,
MJ
0 Comments
Accepted Answer
Stephen23
on 20 Oct 2023
Use MATLABFUNCTION:
syms a b c
f = a*sin(b)*exp(c)
df = diff(f,b)
mf = matlabFunction(df)
a=rand(10,1);
b=rand(10,1);
c=rand(10,1);
mf(a,b,c)
0 Comments
More Answers (2)
Dyuman Joshi
on 20 Oct 2023
Convert it to a function handle -
syms a b c
f = a*sin(b)*exp(c)
df = diff(f,b)
%Convert the symbolic function to an anonymous function
fun = matlabFunction(df)
a=rand(10,1);
b=rand(10,1);
c=rand(10,1);
fun(a,b,c)
0 Comments
Walter Roberson
on 20 Oct 2023
format long g
syms a b c
f(a,b,c) = a*sin(b)*exp(c)
df = diff(f,b)
A = rand(10,1);
B = rand(10,1);
C = rand(10,1);
D = df(A, B, C)
vpa(D)
double(D)
0 Comments
See Also
Categories
Find more on Assumptions 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!