Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations pierreick on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vectors with Complex Numbers in Mathcad 1

Status
Not open for further replies.

Sparweb

Aerospace
May 21, 2003
5,156
Hi,
Longtime user of Mathcad (I started at version 5). I've only recently become concerned with complex numbers. Budding interest in electrical engineering, doing Fourier analysis, etc.
Since the Fourier analysis function FFT() delivers a vector containing complex numbers, it would be nice to simply find the maximum absolute valued element of the result, its rank, and then carry on. This isn't working out for me.

To make a simple example, rather than dumping a thousand lines on the screen, I'll provide an example with just 4 complex numbers and demonstrate my problem. I've been using these as a "sanity" check for myself as well as a way to try finding solutions. Still stuck. Take 4 complex numbers like these:

0.31+0.70i
0.21-0.89i
0.36+0.02i
0.44-0.14i

These four have been entered into a Mathcad vector, named "x", and the first number is in rank "0" of the vector (Mathcad numbers its ranks starting from zero by default).
I want to find the MAXIMUM absolute valued number in the vector. Let's do this manually to see where we're going:

|0.31+0.70i| = 0.766
|0.21-0.89i| = 0.914
|0.36+0.02i| = 0.361
|0.44-0.14i| = 0.462

Now that I have done all the work, it's obvious that 0.914 is the maximum absolute value, and it comes from (0.21-0.89i) in rank 1. Now I want to be able to find this with a function, not manually. I will be doing FFT's on 1000's of samples at a time. There should be a function that can find this for me. Wouldn't it be nice if max() would do this. But no.

The function max() sifts through the complex numbers, picks whatever largest real number it can find, then whichever imaginary component it can find, and slaps them together! The User's Guide explicitly says the max(A) function finds the "Largest element in arry A. If A has complex elements, it returns the largest real part plus i times the largest imaginary part". Why would I want it to do this? It corrupts the complex number. Worse, the max() function is used in the Guide's examples for FFT functions! How many users get stumped by this? How many get the garbage from max() and don't realize it?

The result in Mathcad: max(x) = 0.44+0.70i ---this is garbage. It is not one of the four numbers in the vector.

I have tried combining the max() function with || absolute value operators but this spews out more garbage.
For instance:

|max(x)| = 0.827 --- which is just the absolute value of 0.44+0.7i if you were to work it out. more garbage.

max(|x|) = 1.329 --- no idea where that result comes from.

It may be blatantly obvious but I would gladly appreciate any suggestions.

STF
 
Replies continue below

Recommended for you

What you need is max(|x|)= but you have to vectorize that expression, otherwise you get the magnitude of the vector x itself. Vectorization is a powerful operator which very often can be used to great advantage.
BTW, its always better to attach a worksheet rather than describing what you have typed. It makes it also easier for people to reply with a fixed sheet and also gives a hint as to the version the question is aimed for.

 
Hi rmix22,

x already is a vector. When I vectorize it, the numbers are changed into real numbers. The UG tells me that vectorizing a vector returns a vector of the sine of each value.

The worksheet has become messy, with all my little experiments, so it's very hard to read, and only 2% of it has the complex number problem.
But if it really helps, I can chop it down for you.



STF
 
More playing around with "vectorizing".

The order in which it occurs matters. I was vectorizing the vector, then taking the absolute value. No, I had to use the absolute value operator, then vectorize the result of that, then find the max() of that.
I can't just type this in ASCII on the screen, but the point is that I have to get the operators in the right order for it to work.

I think I got it, thanks!

STF
 
Vectorizing has nothing to do with the sine function - maybe it was an example in the help using sine.

If you vectorize an expression and the expression contains a vector (or more of equal dimension; no matter if real or complex), the expression is evaluated for each vector element one by one and the results are collected in a new vector. If an operation (like squaring, divison, sin(vector)) which is not defined for vectors is used, Mathcad automatically applies an implicit vectorization. But magnitude (|x|) or multiplication are defined for vectors and so these are used if not otherwise stated by using explicit vectorization.

SparWeb said:
I can't just type this in ASCII on the screen,
Thats exactly why I suggested to attach a worksheet. A small sample sheet like the attached to this post would have sufficed. We wouldn't have needed the huge original sheet.
 
 http://files.engineering.com/getfile.aspx?folder=1c58501f-8a34-48f5-bb5c-9f0ffb6a4659&file=complex_vectorize.xmcdz
If you vectorize an expression and the expression contains a vector (or more of equal dimension; no matter if real or complex), the expression is evaluated for each vector element one by one and the results are collected in a new vector. If an operation (like squaring, divison, sin(vector)) which is not defined for vectors is used, Mathcad automatically applies an implicit vectorization. But magnitude (|x|) or multiplication are defined for vectors and so these are used if not otherwise stated by using explicit vectorization.

Yup, that's exactly it. Thanks for pointing this out because that was what I was missing.

PS, I'm currently using MC13 version 13.1. I lost the User's Guide from 13 but I still have the Guide from version 7. In terms of the math that I use it for, I see no difference between MC 13 and MC7, or MC5 for that matter. Upgrades have been driven solely by operating system compatibility, and avoidance of unstable versions (such as MC6).


Further to my FFT experiments, I discovered that the complex "CFFT()" rather than the discrete FFT(), is more likely to give the right max() value if calculated without the vectorize operator (as demonstrated in the UG example). That's because the array that the CFFT function spits out is twice as long as the array given by the FFT. The additional values in the array are the same as the first ones, but the sign of the imaginary component is reversed. So if the value in the array at j=50 is 0.277-0.38i, then you can go to N-j and find 0.277+0.38i, which greatly increases the odds that max() will find the right answer.

As for the sin() question, what I'm reading in the UG is confusing, but irrelevant to what I'm trying to do. Some finger trouble on my end didn't help. The book really does say "if v is a vector, sin(v) is an illegal expression" Very strange, but never mind. I'm not going to worry about it because obviously it works just fine when I try to do it now.

STF
 
Actually sin(v) would not be a valid expression unless vectorization is applied. I'm not sure when implicit vectorization was introduced - maybe after version 7.
You could search the net for a pdf of the user guide for version 11. This was the last one which was complete including a complete reference. Beginning with version 12 (the worst version ever, apart from Prime, of course) the user guide was significantly truncated (and with Prime there is no UG whatsoever).

Not sure what you are working on but it could be that you find that thread in the main Mathcad forum of interest: [URL unfurl="true"]http://communities.ptc.com/message/212019#212019[/url]

Find attached the sheet from my last post in MC13 format.
 
"implicit vectorization" Yes that would be why the UG says one thing and I can do the other. So now I actually am doing math where one version is different from another.

I haven't been paying attention to the Mathsoft -> PTC business. I did download PTC prime but I don't like to use it.
I will pursue the links and the attachment you provided later (dinnertime in my time zone) but thanks again for the very quick responses.


STF
 
SparWeb said:
I did download PTC prime but I don't like to use it.
Neither do I. The best on Prime is, that you get Mathcad 15 for free (buy the tail and get the dog for free). Otherwise Prime is just disappointing up to unuseable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor