Clear Filters
Clear Filters

Why does matlab drop the negative sign when evaluating symbolic derivative of x(t) = cos(Wn*t+phi)?

13 views (last 30 days)
I wrote a short script to take the derivatives of some symbolic functions to check my math, however I get a slightly different result than when I take the derivatives by hand, and I believe my hand results are correct. Perhaps MATLAB is handling one of my symbols (possibly phi) or the function in a way I don't understand as a relative newb to MATLAB.
Bottom line, it seems to be dropping the negative sign when evaluating x'(t), where x=Acos(Wn*t-phi). When I do it by hand I get x'(t)=-A*Wn*sin(Wn*t-phi), (because derivative of cosine is negative sine, and pulling out the Wn by the chain rule), but when MATLAB does diff(x,t) it does not give the negative sign, just x'(t)=A*Wn*sin(Wn*t-phi). Note the negative in front of the A is not there.
I'm guessing it has something to do with how I'm defining the function, the symbol phi or Wn, or maybe I have something else wrong, but if someone has some insight, I would love to hear what it is so I can understand what's going on and do this more reliably and with more trust in the result in the future. Code below for reference. Thanks!
syms A zunderdamped zoverdamped Xo Vo Wn Wd t phi;
xundamped = A*cos((Wn*t)-phi);
diffxundamped = diff(xundamped,t)
%this gives the result: diffxundamped = A*Wn*sin(phi - Wn*t)
%which is incorrect, actual x'(t) = -A*Wn*sin(Wn*t-phi)
xunderdamped = A*(exp(-zunderdamped*Wn*t))*cos(Wd*t-phi);
diffxunderdamped = diff(xunderdamped, t)
%this gives diffxunderdamped = A*Wd*exp(-Wn*t*zunderdamped)*sin(phi - Wd*t) - A*Wn*zunderdamped*cos(phi - Wd*t)*exp(-Wn*t*zunderdamped)
%which is also incorrect, it dropped the negative again, actual x'(t) = -A*Wd*exp(-Wn*t*zunderdamped)*sin(phi - Wd*t) - A*Wn*zunderdamped*cos(phi - Wd*t)*exp(-Wn*t*zunderdamped)
xcriticallydamped = (Xo+(Vo+Wn*Xo)*t)*exp(-Wn*t);
diffxcriticallydamped = diff(xcriticallydamped,t)
%this gives diffxcriticallydamped = exp(-Wn*t)*(Vo + Wn*Xo) - Wn*exp(-Wn*t)*(Xo + t*(Vo + Wn*Xo))
%which is correct (no derivative of cosine evaluated in this one though)

Accepted Answer

Walter Roberson
Walter Roberson on 8 Feb 2018
sin(-x) = -sin(x) so A*Wn*sin(phi - Wn*t) is the same as -A*Wn*sin(Wn*t-phi) -- notice that the sign of the inside of the sin() is negated.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!