how can a symbolic derivative be vectorized automatically?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Marko
le 20 Oct 2023
Réponse apportée : Walter Roberson
le 20 Oct 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 commentaires
Réponse acceptée
Stephen23
le 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 commentaires
Plus de réponses (2)
Dyuman Joshi
le 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 commentaires
Walter Roberson
le 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 commentaires
Voir également
Catégories
En savoir plus sur Assumptions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!