Plotting magnitude of complex expression

6 Ansichten (letzte 30 Tage)
Kishan Rajah
Kishan Rajah am 13 Mär. 2021
Bearbeitet: Srinidhi am 14 Mär. 2021
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.

Akzeptierte Antwort

the cyclist
the cyclist am 13 Mär. 2021
Bearbeitet: the cyclist am 13 Mär. 2021
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.

Weitere Antworten (1)

Srinidhi
Srinidhi am 13 Mär. 2021
Bearbeitet: Srinidhi am 14 Mär. 2021
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');

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by