Strange behavior of diff function with symbolic variables

1 view (last 30 days)
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.

Accepted Answer

Yasasvi Harish Kumar
Yasasvi Harish Kumar on 20 Feb 2019
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

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!