# Strange behavior of diff function with symbolic variables

1 ビュー (過去 30 日間)
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 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

#### 1 件のコメント

Birdman 2019 年 2 月 20 日