Matrix inversion and summation

Hi all, can anyone help me to explain the mistake in my code below? The problem is as follow:
  • The following parameter is assumed,
lma = 3;
ma = zeros(1,lma);
ma(2) = -0.2;
ma(lma) = -0.01;
  • The parameter is then used to calculate the coordinate z using the following equation,
where z = x + i*y (complex number)
dumZ = 0;
sumdumZ = 0;
theta = linspace(0,2*pi,37);
xi = exp(1i.*theta);
for ii=1:length(theta)
for jj=1:lma
dumZ = ma(jj).*xi(ii).^jj;
sumdumZ = sumdumZ + dumZ;
end
zcon(ii) = 1./xi(ii)+sumdumZ;
dumZ = 0;
sumdumZ = 0;
end
zcon=zcon';
The following profile is obtained by plotting zcon,
  • Now, using the hypothetical data above, i.e. the zcon, I want to back-calculate the variable "ma" or "alpha" as shown in the equation above. To do this, I first created a matrix A, which essentially a matrix containing all the constants. The following code is used to generate matrix A with 370 terms,
lx = length(zcon);
N = 10*37;
A = zeros(lx,N);
for ii=1:lx
for jj=1:N
A(ii,jj) = xi(ii).^jj;
end
end
A=[1./xi' A]; % insert a new colum to the left containing the 1/xi(ii) into the existing matrix A;
  • Then the variable "ma" or "alpha" can be easily obtained using the following operation in Matlab,
maiter = A\zcon;
which give me a [370 x 1] matrix. To validate this, I again calculate the "predicted" z using the following code:
zdirect = A*maiter;
Which results an exactly the same curve (as expected) as shown below,
Now, coming to my question. Instead of using a direct calculation as shown above, i.e. the code "zdirect = A*maiter, instead I would like to manually use summation to calculate the "z" for each "theta" using the code below. Somehow, I could not get it right and cannot find the mistake. Below is the code that I used to calculate "z"
dumZ = 0;
sumdumZ = 0;
pow = [-1 1:1:N];
for ii=1:length(theta)
for jj=1:length(maiter)
dumZ = maiter(jj).*xi(ii).^pow(jj);
sumdumZ = sumdumZ + dumZ;
end
ziter(ii) = sumdumZ;
dumZ = 0;
sumdumZ = 0;
end
Instead, this is what I got,
Can someone please help me?

 采纳的回答

David Goodmanson
David Goodmanson 2018-6-29
Hi Bee,
Are you plotting the right variable? I ran your code, and zcon, zdirect and ziter all reproduce the same curve. Only the simple exponential variable xi gives the red circle in the second plot.

4 个评论

Hi @David Goodmanson,
Thanks for your reply. I am sure I plotted the correct one. Below is the numeric output of the three parameters that got using the same parameters.
One thing that I notice is that the real part of all the z is the same. However, the imaginary part of ziter is different with zcon and zdirect.
zcon zdirect ziter
0.7900 + 0.0000i 0.7900 - 0.0000i 0.7900 + 0.0000i
0.7882 + 0.2471i 0.7882 + 0.2471i 0.7882 + 0.1002i
0.7815 + 0.4792i 0.7815 + 0.4792i 0.7815 + 0.2048i
0.7660 + 0.6832i 0.7660 + 0.6832i 0.7660 + 0.3168i
0.7363 + 0.8484i 0.7363 + 0.8484i 0.7363 + 0.4372i
0.6862 + 0.9680i 0.6862 + 0.9680i 0.6862 + 0.5641i
0.6100 + 1.0392i 0.6100 + 1.0392i 0.6100 + 0.6928i
0.5039 + 1.0633i 0.5039 + 1.0633i 0.5039 + 0.8161i
0.3666 + 1.0446i 0.3666 + 1.0446i 0.3666 + 0.9251i
0.2000 + 0.9900i 0.2000 + 0.9900i 0.2000 + 1.0100i
0.0093 + 0.9077i 0.0093 + 0.9077i 0.0093 + 1.0619i
-0.1975 + 0.8061i -0.1975 + 0.8061i -0.1975 + 1.0733i
-0.4100 + 0.6928i -0.4100 + 0.6928i -0.4100 + 1.0392i
-0.6167 + 0.5741i -0.6167 + 0.5741i -0.6167 + 0.9580i
-0.8058 + 0.4545i -0.8058 + 0.4545i -0.8058 + 0.8311i
-0.9660 + 0.3368i -0.9660 + 0.3368i -0.9660 + 0.6632i
-1.0879 + 0.2221i -1.0879 + 0.2221i -1.0879 + 0.4619i
-1.1641 + 0.1102i -1.1641 + 0.1102i -1.1641 + 0.2371i
-1.1900 + 0.0000i -1.1900 + 0.0000i -1.1900 + 0.0000i
-1.1641 - 0.1102i -1.1641 - 0.1102i -1.1641 - 0.2371i
-1.0879 - 0.2221i -1.0879 - 0.2221i -1.0879 - 0.4619i
-0.9660 - 0.3368i -0.9660 - 0.3368i -0.9660 - 0.6632i
-0.8058 - 0.4545i -0.8058 - 0.4545i -0.8058 - 0.8311i
-0.6167 - 0.5741i -0.6167 - 0.5741i -0.6167 - 0.9580i
-0.4100 - 0.6928i -0.4100 - 0.6928i -0.4100 - 1.0392i
-0.1975 - 0.8061i -0.1975 - 0.8061i -0.1975 - 1.0733i
0.0093 - 0.9077i 0.0093 - 0.9077i 0.0093 - 1.0619i
0.2000 - 0.9900i 0.2000 - 0.9900i 0.2000 - 1.0100i
0.3666 - 1.0446i 0.3666 - 1.0446i 0.3666 - 0.9251i
0.5039 - 1.0633i 0.5039 - 1.0633i 0.5039 - 0.8161i
0.6100 - 1.0392i 0.6100 - 1.0392i 0.6100 - 0.6928i
0.6862 - 0.9680i 0.6862 - 0.9680i 0.6862 - 0.5641i
0.7363 - 0.8484i 0.7363 - 0.8484i 0.7363 - 0.4372i
0.7660 - 0.6832i 0.7660 - 0.6832i 0.7660 - 0.3168i
0.7815 - 0.4792i 0.7815 - 0.4792i 0.7815 - 0.2048i
0.7882 - 0.2471i 0.7882 - 0.2471i 0.7882 - 0.1002i
0.7900 - 0.0000i 0.7900 + 0.0000i 0.7900 - 0.0000i
BeeTiaw
BeeTiaw 2018-6-30
编辑:BeeTiaw 2018-6-30
@David Goodmanson, can you please copy-paste your result that in here? so that we can compare the real and imaginary part.
Hi Bee,
Below are the first few rows of [zcon zdirect ziter]. The only change I made to your code was
zcon = zcon.' instead of zcon = zcon'
so that the complex conjugate of zcon is not taken. That means that the table I get is the complex conjugate of yours. I also added
ziter = ziter.'
so that ziter is a column vector. Of course neither of these changes/additions affects the shape of the plot, 'triangle' vs. circle.
0.7900 + 0.0000i 0.7900 + 0.0000i 0.7900 + 0.0000i
0.7882 - 0.2471i 0.7882 - 0.2471i 0.7882 - 0.2471i
0.7815 - 0.4792i 0.7815 - 0.4792i 0.7815 - 0.4792i
0.7660 - 0.6832i 0.7660 - 0.6832i 0.7660 - 0.6832i
0.7363 - 0.8484i 0.7363 - 0.8484i 0.7363 - 0.8484i
0.6862 - 0.9680i 0.6862 - 0.9680i 0.6862 - 0.9680i
0.6100 - 1.0392i 0.6100 - 1.0392i 0.6100 - 1.0392i
0.5039 - 1.0633i 0.5039 - 1.0633i 0.5039 - 1.0633i
0.3666 - 1.0446i 0.3666 - 1.0446i 0.3666 - 1.0446i
0.2000 - 0.9900i 0.2000 - 0.9900i 0.2000 - 0.9900i
0.0093 - 0.9077i 0.0093 - 0.9077i 0.0093 - 0.9077i
-0.1975 - 0.8061i -0.1975 - 0.8061i -0.1975 - 0.8061i
-0.4100 - 0.6928i -0.4100 - 0.6928i -0.4100 - 0.6928i
-0.6167 - 0.5741i -0.6167 - 0.5741i -0.6167 - 0.5741i
-0.8058 - 0.4545i -0.8058 - 0.4545i -0.8058 - 0.4545i
Hi David Goodmanson,
Thanks for your answer!
I got it correct now after change it as per your suggestion. Thanks a lot!
It seems that it has something to do with consistency of the conjugate/non-conjugate of the complex number. Although I still do not clearly understand it.
But I would consider the problem is solved now.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by