why do we get different values by angle command

1 次查看(过去 30 天)
When i run this code i am getting the result as shown below
clc;clear all; close all;
N=input('Enter the fundamental period of the signal (N)');
M=input('Enter how many points of Fourier series to compute(m>N and m=multiples of N) ')
n=0:N-1;
x=[sin(2*pi*n/6)];
for k=0:M-1;
Sum=0;
for n=0:N-1;
Ck(k+1)=x(n+1)*exp(-j*2*pi*k*n/N);
Sum=Sum+Ck(k+1);
end
Ck(k+1)=Sum/N;
end
Mag=abs(Ck);Phase_ang=angle(Ck);
disp(Ck);disp(Mag);disp(Phase_ang);
Output
Enter the fundamental period of the signal (N)6
Enter how many points of Fourier series to compute(M=N or M=multiples of N) 6
M =
6
0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i
0.0000 0.5000 0.0000 0.0000 0.0000 0.5000
0 -1.5708 2.9229 1.5075 0.0588 1.5708
The values of phase angle are supposed to be
0 -1.5708 0 0 0 1.5708
But for the same array if i use command window i get the proper result
>>angle([0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i])
ans =
0 -1.5708 0 0 0 1.5708
  2 个评论
Stephen23
Stephen23 2023-6-7
编辑:Stephen23 2023-6-7
"But for the same array..."
No, that is a different array, with different values. The values displayed (by default with four decimal places) are not the same as those stored in memory. So if you simply copy the displayed data, then you will have different values.
N=6;
M=6;
n=0:N-1;
x=sin(2*pi*n/6); % got rid of the superfluous square brackets
for k=0:M-1;
Sum=0;
for n=0:N-1;
Ck(k+1)=x(n+1)*exp(-j*2*pi*k*n/N);
Sum=Sum+Ck(k+1);
end
Ck(k+1)=Sum/N;
end
format long G
Ck
Ck =
Columns 1 through 3 5.55111512312578e-17 + 0i 5.55111512312578e-17 - 0.5i -1.66533453693773e-16 + 3.70074341541719e-17i Columns 4 through 6 1.85037170770859e-17 + 2.91747532808639e-16i 3.14563190310461e-16 + 1.85037170770859e-17i 2.22044604925031e-16 + 0.5i
Kavita Guddad
Kavita Guddad 2023-6-9
Thank you very much stephen for the answer.Got my problem solved.

请先登录,再进行评论。

采纳的回答

Sandeep Mishra
Sandeep Mishra 2023-6-8
Hello Kavita,
I understand that you are trying to create a Fourier series with M points, fundamental frequency N and you are also trying to find the phase angle and magnitude of the series.
In MATLAB, "angle" provides you with the functionality to find the phase angle of the complex array in [-π,π] interval.
In MATLAB, the default data type of numerical value is "double-precision floating-point" which requires 64 bits, So the value stored in your Sum variable and Ck series has more than 4 decimal points and it is different from your command line's input array.
You can round off the Ck array to 4 decimal places to get your desired results and compare the generated series by their difference as below.
clc;clear all; close all;
N= 6;
M= 6;
n=0:N-1;
x=[sin(2*pi*n/6)];
for k=0:M-1
Sum=0;
for n=0:N-1
Sum=Sum+x(n+1)*exp(-1j*2*pi*k*n/N);
end
Ck(k+1)=Sum/N;
end
Mag=abs(Ck);Phase_ang=angle(Ck);
% Input array
inputArray = [0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i];
% Difference between input array and generated result
difference = Ck - inputArray;
disp(difference);
1.0e-15 * 0.0555 + 0.0000i 0.0555 + 0.0555i -0.1665 + 0.0370i 0.0185 + 0.2917i 0.3146 + 0.0185i 0.2220 + 0.0000i
% Rounding off the value
Ck = round(Ck, 4);
disp(Ck);
0.0000 + 0.0000i 0.0000 - 0.5000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i
% Angle calculated from input array
angleInput = angle(inputArray);
disp(angleInput)
0 -1.5708 0 0 0 1.5708
% Angle calculated from generated series
angleGiven = angle(Ck);
disp(angleGiven)
0 -1.5708 0 0 0 1.5708
You can refer to the documentation below to learn more about "angle", "double-precision floating-point", and "data-types" in MATLAB.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by