How can I get Matlab to display a sinusoid in Acos(wt+θ) form instead of Asinwt+Bcoswt form using the Symbolic Toolbox?
4 views (last 30 days)
Show older comments
Kevin Oliver
on 21 Nov 2016
Commented: Star Strider
on 23 Nov 2016
I tried searching for any kind of trig collect function like my TI-89 has but could not find anything.
For example, using the Symbolic Toolbox I found the inverse laplace transform of a transfer function and it gave me the answer as a sum of a constant, a sin term, and a cos term. I was wondering how I could convert this answer into a constant, and a single cos term with an amplitude and a phase shift. Here's what I have:
syms s
T1 = 24.542/(s*(s^2+4*s+24.542));
t1 = vpa(ilaplace(T1),4)
The answer I receive is:
t1 = 1.0 - 1.0*exp(-2.0*t)*(cos(4.532*t) + 0.4413*sin(4.532*t))
The answer should look something like:
t1 = 1.0 - 1.09*exp(-2.0*t)*cos(4.532*t-23.8°)
EDIT: After a ton of testing I found a solution but I'm unsure why this specific set of commands works. It only works if the simplification steps are 307 or higher. Also, both vpa functions are absolutely required. Removing either will make the code no longer function. The entire code is now:
syms s
T1 = 24.542/(s*(s^2+4*s+24.542));
t1 = vpa(simplify(vpa(ilaplace(T1),4),307),4)
Which results in:
t1 = 1.0 - 1.093*cos(4.532*t - 0.4156)*exp(-2.0*t)
Could someone please explain to me why this works? Also, if there is a cleaner way to accomplish this goal I would be grateful if someone pointed me in the correct direction.
EDIT: This solution did not work for a slightly more complex transfer function:
T2 = 245.42/(s*(s+10)*(s^2+4*s+24.542));
t2 = vpa(simplify(vpa(ilaplace(T2),4),1000),4)
Results in:
t2 = 1.0 - 0.7097*exp(-2.0*t)*(cos(4.532*t) + 1.344*sin(4.532*t)) - 0.2903*exp(-10.0*t)
5 Comments
Karan Gill
on 22 Nov 2016
The reason is works with vpa is that, in short, the conversion to floating point simplifies the expression in the first place.
Without vpa, 0.4156 is a complicated symbolic expression, and even if simplify manages to find its symbolic equivalent, that equivalent is complex enough that it's ignored.
Also, I could not replicate Star Strider's result on 16b. I get OP's result. Strange.
Accepted Answer
Star Strider
on 23 Nov 2016
I couldn’t initially reproduce my earlier result either this morning when I ran your code as you posted it. I got the same initial result you did.
When I did some experiments, and this code:
syms s
T1 = 24.542/(s*(s^2+4*s+24.542));
t1 = vpa(ilaplace(T1),4)
t1 = vpa(simplify(rewrite(t1, 'cos'), 'Steps', 10), 4)
reproducibly yields this result:
t1 =
1.0 - 1.0*exp(-2.0*t)*(cos(4.532*t) + 0.4413*sin(4.532*t))
t1 =
1.0 - 1.093*cos(4.532*t - 0.4156)*exp(-2.0*t)
The only difference is adding:
t1 = vpa(simplify(rewrite(t1, 'cos'), 'Steps', 10), 4)
that seems to produce the desired result (in R2016b).
4 Comments
Star Strider
on 23 Nov 2016
It may be. I can’t figure out why it won’t reduce to a phasor representation. (I also attempted to use collect but that didn’t help so I didn’t include it here.)
We have Karan Gill’s attention, so MathWorks is already following this thread.
For fun, I decided to see what Wolfram Alpha would do. It didn’t do much better. (Click on the link to bring up your problem as I entered it there.)
I’ve given some thought to getting Maple (that was the symbolic engine up to 2008 or so if I remember correctly), but that would cause problems for me answering questions on the MuPad engine. I may do it on one of my other laptops with MATLAB installed to see if there’s a difference in the symbolic results. I could then easily transfer the results over my WLAN.
Thank you!
More Answers (1)
KSSV
on 22 Nov 2016
What you have followed is correct....you need to simplify it and then use variable-precision floating-point arithmetic (VPA) to evaluate each symbolic input x to significant digits.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!