6

For example, $a=(2,\pi/4)$, $b=(3,\pi/3)$, if we want to get the result of $a+b$, using + operator will not do, because mathematica will ignore the polar form, and regard a and b two two-dimension vectors in Cartesian coordinates. Polar vectors are used in phasor diagram.

m0nhawk
  • 3,867
  • 1
  • 20
  • 35
Sean Patrick
  • 147
  • 7
  • 8
    So, create a special object, call it Phasor[r, θ], and then define a way to add two Phasor[] objects: Phasor /: Phasor[r1_, θ1_] + Phasor[r2_, θ2_] := (* stuff *)Abs[] and Arg[] will be useful, of course. – J. M.'s missing motivation Jun 20 '15 at 09:35
  • Norm[] and Abs[] are equivalent for complex number argument. – J. M.'s missing motivation Jun 20 '15 at 09:54
  • Oh yes...so I guess my problem has been solved.Thanks:D – Sean Patrick Jun 20 '15 at 10:02
  • 1
    Sean, if you made it work go ahead and post the solution as an answer. (Otherwise the question will keep popping up as unanswered) – george2079 Jun 20 '15 at 13:32
  • how to ......@george2079 – Sean Patrick Jun 20 '15 at 16:48
  • @SeanPatrick using the form here below and clicking the button that says "post your answer". It would be great if you did that. – C. E. Jun 20 '15 at 20:20
  • @Pickett think I did it,any suggestion for my first answer in stackexchange?:D – Sean Patrick Jun 21 '15 at 04:45
  • There are a couple of things that would improve the post. 1) Post your code as code, not as an image. You can see in other posts how they format answers. 2) When you do this greek letters are going to be replaced by codes such as \[rho]. In order to convert these back to greek letters you can use this tool. 3) You can write it as a reusable function that people can copy into their own projects, using Guess who it is' Phasor definition above. 4) "c" in "change" should be capitalized. I already gave you my vote though. – C. E. Jun 21 '15 at 10:47
  • @Pickett After my final exams I'll improve my answer soon....Asking a question is really much easier than answering one. I have a question:are we encouraged to post an answer to our own question? It seem to me most people comment on questions instead of answering them.... – Sean Patrick Jun 26 '15 at 15:51
  • @Guesswhoitis. Maybe it is just coincidence.... do I need to attribute to you? – Sean Patrick Jun 26 '15 at 15:56
  • It's entirely your call, but proper attributions are always appreciated. – J. M.'s missing motivation Jun 26 '15 at 16:05
  • @SeanPatrick Yes, if you can answer your own question then you are encouraged to do so because we don't like to leave questions unanswered and the answer might help someone in the future. It is also very discouraged to answer questions with comments, for this reason, but sometimes it happens. There is a discussion about this here. – C. E. Jun 26 '15 at 16:11
  • I've post a new answer,I don't know if I am doing better than last time,and I 'm not sure if I attribute to you properly....Actually I have two new questions: 1)" /:" defines only the operating between two Phasors,what if we need to add more than two? 2)if I want to have a format for the final answer,how to?@Guesswhoitis. – Sean Patrick Jul 01 '15 at 19:18
  • AnglePath might interest you. – Greg Hurst Jul 02 '15 at 02:06
  • @ChipHurst The difference between phasor addition and AnglePath is that the angles there are relative rotations, which isn't the same as complex addition. – Jens Jul 02 '15 at 05:50
  • This should not be this complicated... There should a built in function for this. – polarka Mar 29 '21 at 11:55

2 Answers2

5

Here is a definition that works for arbitrary number of phasors:

ClearAll[phasor]

phasor /: Plus[p : _phasor ..] := 
 phasor @@ 
  ToPolarCoordinates@
   Total[{p} /. phasor -> (FromPolarCoordinates[{##}] &)]

Example:

phasor[1, Pi] + phasor[2, Pi/4] + phasor[3, Pi/3]

$$\text{phasor}\left(\sqrt{\left(\frac{1}{2}+\sqrt{2}\right)^2+\left(\sqrt{2}+\frac{3 \sqrt{3}}{2}\right)^2},\tan ^{-1}\left(\frac{\sqrt{2}+\frac{3 \sqrt{3}}{2}}{\frac{1}{2}+\sqrt{2}}\right)\right)$$

It uses the new functions (in version 10.1) FromPolarCoordinates and ToPolarCoordinates.

To make it work with arbitrary number of phasors, I use a named pattern p with Repeated, which collects all phasors appearing in a sum. They are then converted to Cartesian coordinates, added with Total, and converted back to phasor form (the last step could be omitted if you want the output to be in Cartesian coordinates).

Jens
  • 97,245
  • 7
  • 213
  • 499
3

Phasor /: Phasor[p1_, r1_] + Phasor[p2_, r2_] := p1E^(I r1) + p2E^(I r2) Infix[{Abs[Phasor[3, Pi/3] + Phasor[3, Pi/6]], Arg[Phasor[3, Pi/3] + Phasor[3,Pi/6]]}, ","]


Two lines of code is really enough to solve problem. Thanks for J. M.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Sean Patrick
  • 147
  • 7
  • …well, what you did is in fact what you're supposed to replace (* stuff *) with in my very first comment. :) – J. M.'s missing motivation Jun 21 '15 at 06:39
  • You can use Module[] to encapsulate your procedure… – J. M.'s missing motivation Jul 01 '15 at 19:12
  • Can you do an example for me? I have trouble understanding Module although Mathematica already gave some examples.This almost drives me crazy!!I’ve been working on this problem and also been modling Foucault's pendulum using mathematica for hours and It's 3:40 am now!!I'have to go to sleep and I'll check news of this problem tomorrow . – Sean Patrick Jul 01 '15 at 19:33