how can a symbolic derivative be vectorized automatically?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marko
el 20 de Oct. de 2023
Respondida: Walter Roberson
el 20 de Oct. de 2023
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 comentarios
Respuesta aceptada
Stephen23
el 20 de Oct. de 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 comentarios
Más respuestas (2)
Dyuman Joshi
el 20 de Oct. de 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 comentarios
Walter Roberson
el 20 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Assumptions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!