Error when multiplying complex numbers

50 views (last 30 days)
Hello, I'm having trouble multiplying complex numbers, and I have no idea why. I created a loop (for i=1:1:24) in which I calculate (among others) two complex numbers. The result being completely off, I tried running the calculations through the command window.
---------------------------------------------------------------------------------------------
>> rho
rho =
64.4787 +57.6367i
>> wp
wp =
0.0043 + 0.0049i
>> rho*wp
ans =
0.0000 + 0.5642i
%which is, of course, false. However, when I submit this calculation without resorting to values to Matlab through the command window:
>> (64.4787 +57.6367i)*(0.0043 + 0.0049i)
ans =
-0.0052 + 0.5638i
%No error here
---------------------------------------------------------------------------------------------
I don't understand how that can happen. Granted, rho, and wp will change value on each iteration, however, if I ask Matlab for their final value and multiply them out of the loop, why is the result wrong?
---------------------------------------------------------------------------------------------
Here is the complete code attached, if you run it, and try to multiply rho and wp, I suspect the result of the multiplication of rho and wp will be off.

Accepted Answer

Roger Stafford
Roger Stafford on 29 Apr 2014
Pascal, your difficulty is almost certainly due to displaying your numbers with the 'short' format. You aren't displaying the numbers you have computed accurately enough.
For example, suppose you do this:
format short
rho = 64.4787+57.6367i;
wp = 0.004348+0.004864i;
rho
wp
rho*wp
You will get:
rho =
64.4787+57.6367i
wp =
0.0043 + 0.0049i
ans =
0.0000 + 0.5642i
which is just the "wrong" result you described. However if you use "format long" instead of "format short" with these same numbers, you will see that the multiplication is actually quite accurate. In fact it is accurate to approximately sixteen decimal places.
  2 Comments
Pascal
Pascal on 29 Apr 2014
Thank you, that seems to work, however, I don't understand why the results using short are so "inconsistent". Indeed, here are the values of wp and rho I get for the 24th iteration.
FORMAT: SHORT
wp =0.0043 + 0.0049i rho =64.4787 +57.6367i rho*wp=0.0000 + 0.5642i (0.0043 + 0.0049)*(64.4787 +57.6367)=-0.0052 + 0.5638i
FORMAT: LONG
wp=0.004347979670077 + 0.004863476366942i
rho=64.478705846869218 +57.636677888654823i
wp*rho=0.000037481394524 + 0.564193765767921i
(0.004347979670077 + 0.004863476366942i)*(64.478705846869218 +57.636677888654823i)=0.000037481394541 + 0.564193765767901i
The results are, here, correct. I don't quite understand why Matlab makes "mistakes" when doing such "simple" calculations. Is the short format THAT limited?
Thank you
Roger Stafford
Roger Stafford on 29 Apr 2014
I think you have a misconception about the significance of matlab's 'format' function, Pascal. You should understand that the format used has no effect whatever on the accuracy of computations. Those are being done with binary numbers. It only affects the way variables' values are displayed to the user. If you look at the documentation for 'format', you will see the key statement: "The format function affects only how numbers display in the Command Window, not how MATLAB® computes or saves them."
Thus when you use 'format short' in your example, it isn't matlab's computations that are in error, it is the fact that the values displayed for your benefit aren't being shown to you with sufficient accuracy to ensure that the computation looks correct to you, the user.
As another example, suppose you do this:
format short
a = 33/64;
b = 49/64;
c = 37/64;
d = a+b+c;
a
b
c
d
Then it will look as though matlab doesn't know how to add properly:
0.5156
+ 0.7656
+ 0.5781
------
1.8594
This answer is off by one in the least digit. However if you allow matlab to exhibit six or more decimal places after the decimal point, as with 'format long', the computation will look like this:
0.515625
+ 0.765625
+ 0.578125
--------
1.859375
which is exactly correct. Thus, matlab made no errors in its computation. The problem was in informing the user accurately by way of the displays what numbers were being used.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!