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)
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
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.
Kevin Oliver
Kevin Oliver on 23 Nov 2016
So I upgraded Matlab to 2016b and still get the original answer. Maybe there is some kind of setting that lets you choose the default display of sinusoids?

Sign in to comment.

Accepted Answer

Star Strider
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
Kevin Oliver
Kevin Oliver on 23 Nov 2016
Is this just a Matlab limitation? My TI-89 can combine the cos and sin terms easily but it cannot do the inverse laplace transform. I'll accept your answer because of all your effort but I'm sad that Matlab cannot do something that seems to me to be pretty easy. Something I can do by hand in about 2 minutes. :/
Star Strider
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!

Sign in to comment.

More Answers (1)

KSSV
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.
  1 Comment
Kevin Oliver
Kevin Oliver on 22 Nov 2016
You also need to use vpa before you simplify it for some reason. I was asking if there was a simpler way to do this. I don't want to guess and test the simplification steps each time I do a problem like this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!