Strange behavior of diff function with symbolic variables

조회 수: 1 (최근 30일)
Birdman
Birdman 2019년 2월 20일
댓글: Birdman 2019년 2월 20일
Hello everyone,
There is a strange behavior that I encountered while using diff function with symbolic variables, not functions.
Consider the following symbolic expression:
syms theta1
D=0.31*cos(theta1)^2
diff(D,theta1)
>>
-0.62*cos(theta1)*sin(theta1)
This works as expected but when the expression becomes complicated like
syms theta1 theta2 theta3
D=0.31*cos(theta1)^2 - 2.7e-4*sin(theta2) - 0.88*sin(theta2)*(7.3e-5*cos(theta2) - 0.011*sin(theta2) + 3.1e-4) - 0.88*cos(theta2)*(7.3e-5*sin(theta2) - 4.2e-3*cos(theta2) + 4.6e-4) - 1.0*(0.48*sin(theta3) - 0.88*cos(theta2)*cos(theta3))*(5.1e-4*cos(theta3) - 1.7e-4*sin(theta2) - 0.047*sin(theta3) + 0.088*cos(theta2)*cos(theta3) + 9.5e-4*cos(theta2)*sin(theta3)) - 4.1e-4*cos(theta2) + 0.88*sin(theta2)*(3.2e-4*cos(theta3) + 0.073*sin(theta2) + 9.0e-5*sin(theta3) - 1.7e-4*cos(theta2)*cos(theta3) + 5.9e-4*cos(theta2)*sin(theta3)) + 0.31*sin(theta1)^2 + 1.0*(0.48*cos(theta3) + 0.88*cos(theta2)*sin(theta3))*(0.035*cos(theta3) + 5.9e-4*sin(theta2) - 5.1e-4*sin(theta3) + 9.5e-4*cos(theta2)*cos(theta3) + 0.064*cos(theta2)*sin(theta3)) + 0.33;
diff(D,theta1)
>>
0
I receive zero. What is the cause for this? Appreciate your help.

채택된 답변

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019년 2월 20일
Hi,
Your expression, D contains only 2 theta1 terms(in bold) whose derivatives cancel each other. Differentiation of theta2 and theta3 wrt theta1 is zero. Therefore your answer is zero.
D =0.31*cos(theta1)^2 - 2.7e-4*sin(theta2) - 0.88*sin(theta2)*(7.3e-5*cos(theta2) - 0.011*sin(theta2) + 3.1e-4) - 0.88*cos(theta2)*(7.3e-5*sin(theta2) - 4.2e-3*cos(theta2) + 4.6e-4) - 1.0*(0.48*sin(theta3) - 0.88*cos(theta2)*cos(theta3))*(5.1e-4*cos(theta3) - 1.7e-4*sin(theta2) - 0.047*sin(theta3) + 0.088*cos(theta2)*cos(theta3) + 9.5e-4*cos(theta2)*sin(theta3)) - 4.1e-4*cos(theta2) + 0.88*sin(theta2)*(3.2e-4*cos(theta3) + 0.073*sin(theta2) + 9.0e-5*sin(theta3) - 1.7e-4*cos(theta2)*cos(theta3) + 5.9e-4*cos(theta2)*sin(theta3)) + 0.31*sin(theta1)^2 + 1.0*(0.48*cos(theta3) + 0.88*cos(theta2)*sin(theta3))*(0.035*cos(theta3) + 5.9e-4*sin(theta2) - 5.1e-4*sin(theta3) + 9.5e-4*cos(theta2)*cos(theta3) + 0.064*cos(theta2)*sin(theta3)) + 0.33;
I hope I could help.
Regards

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by