how can a symbolic derivative be vectorized automatically?
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2023년 10월 20일
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
추가 답변 (2개)
Dyuman Joshi
2023년 10월 20일
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
Walter Roberson
2023년 10월 20일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!