# Matlab code that does integration as well plot

22 views (last 30 days)
Emma K. on 2 Mar 2021
Commented: Emma K. on 2 Mar 2021
I am trying my hands on matlab coding but not without issues. I have attached the actual equations for the code here; stated as equation 4.7 and 4.8. I am trying to plot f(m)^2 vs m. Any help would be appreciated. Thanks!
clear all
M=6;
n=1:5;%integers
C=1/2*pi;
for m = 0:M;
psi(phi)=2*pi*n/5;
B= (2*n*pi/6)<= phi <(2*(n+1)*pi/6);
fun =(exp(-1i*m*phi)).*(exp(1i*psi(phi)));
E=integral(fun,0,2*pi);
f(m)=C*E;
G=abs(f(m));
H=G^2;
end
plot(m,H)

Jan on 2 Mar 2021
Edited: Jan on 2 Mar 2021
Your code overwrites H in each iteration. So the final PLOT command outside the loop draws 1 point only. Because single points are not visible, but only lines, you do not see a graphic object.
You did not explain in detail, what you want to do instead. Remember that posting the code does not allow to understand, what you want to achieve. I guess:
H(m) = abs(f(m))^2; % Inside the loop
plot(0:M, H) % After the loop
This line must fail also:
psi(phi)=2*pi*n/5;
phi is not defined anywhere. So I guess, it might work to replace all occurrences of "pshi(phi)" by simply "psi".
This line:
B= (2*n*pi/6)<= phi <(2*(n+1)*pi/6)
will not do, what you expect. The left part is evaluated at first:
(2*n*pi/6)<= phi
This is TRUE or FALSE. The next comaprison is:
false < (2*(n+1)*pi/6) % or
true < (2*(n+1)*pi/6)
whereby FALSE is onverted to 0 and TRUE to 1.
But B is not used anywhere in your code, such that this detail does not matter.
In this line:
fun =(exp(-1i*m*phi)).*(exp(1i*psi(phi)))
beside the already mentioned problem with "psi(phi)" and the undefined "phi", you define a value. INTEGRAL expects a function handle. So maybe you mean:
fun = @(phi) (exp(-1i*m*phi)).*(exp(1i*psi))
Emma K. on 2 Mar 2021
Thank you Jan, I will make modifications as you have suggested and I hope it gives me the right results.