Plotting magnitude of complex expression
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kishan Rajah
il 13 Mar 2021
Modificato: Srinidhi
il 14 Mar 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.
0 Commenti
Risposta accettata
the cyclist
il 13 Mar 2021
Modificato: the cyclist
il 13 Mar 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!")
Also, I made the increment of k smaller, for a smoother plot.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
