Plotting magnitude of complex expression

11 ビュー (過去 30 日間)
Kishan Rajah
Kishan Rajah 2021 年 3 月 13 日
編集済み: Srinidhi 2021 年 3 月 14 日
The code I have used is as follows:
%Define constants
A_1=1;
A_2=2;
L=1;
%Specify range of k values to plot on x axis
k=[0:1:10];
%complex function to plot
t=(2*(A_1*A_2*cos(L*k) + A_1*A_2*sin(L*k)*1i))/(sin(L*k)*A_1^2*1i + 2*cos(L*k)*A_1*A_2 + sin(L*k)*A_2^2*1i);
%Finding magnitude of complex values
U=abs(t);
%Plot curve of U over specified range of k
plot(k,U);
I am looking to plot a graph of U on the y-axis, with k varying between 0 and 10 on the x axis. Having generated a matrix k of 11 integer values, the function t is only evaluated as a single complex number, rather than a matrix of complex numbers for each of the 11 input values of k. This means that the graph cannot be plotted since only a single point is generated.
Is there a mistake in my code which leads to t being a single value, rather than a range of values for varying k, which can then be plotted?
Many thanks for your help.

採用された回答

the cyclist
the cyclist 2021 年 3 月 13 日
編集済み: the cyclist 2021 年 3 月 13 日
The reason t ends up as a single value is that you inadvertently did a matrix division in your calculation. Try this instead:
%Define constants
A_1=1;
A_2=2;
L=1;
%Specify range of k values to plot on x axis
k=0:0.1:10;
%complex function to plot
t=(2*(A_1*A_2*cos(L*k) + A_1*A_2*sin(L*k)*1i))./(sin(L*k)*A_1^2*1i + 2*cos(L*k)*A_1*A_2 + sin(L*k)*A_2^2*1i);
%Finding magnitude of complex values
U=abs(t);
%Plot curve of U over specified range of k
plot(k,U);
disp("See that I added a dot just before the / sign!")
See that I added a dot just before the / sign!
Also, I made the increment of k smaller, for a smoother plot.
You might want to read this documentation about the difference between array and matrix operatoins.
  1 件のコメント
Kishan Rajah
Kishan Rajah 2021 年 3 月 13 日
Thanks very much.

サインインしてコメントする。

その他の回答 (1 件)

Srinidhi
Srinidhi 2021 年 3 月 13 日
編集済み: Srinidhi 2021 年 3 月 14 日
A_1=1;
A_2=2;
L=1;
evec = [];
For k = [0:0.1:10]
t=(2*(A_1*A_2*cos(L*k) + A_1*A_2*sin(L*k)*1i))/(sin(L*k)*A_1^2*1i + 2*cos(L*k)*A_1*A_2 + sin(L*k)*A_2^2*1i);
F = abs(t);
evec = [evec F];
U = evec;
end
plot(k',U');
  1 件のコメント
Srinidhi
Srinidhi 2021 年 3 月 13 日
Hope this helps.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by