Matlab code that does integration as well plot

조회 수: 4 (최근 30일)
Emma K.
Emma K. 2021년 3월 2일
댓글: Emma K. 2021년 3월 2일
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)

답변 (1개)

Jan
Jan 2021년 3월 2일
편집: Jan 2021년 3월 2일
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))
  댓글 수: 1
Emma K.
Emma K. 2021년 3월 2일
Thank you Jan, I will make modifications as you have suggested and I hope it gives me the right results.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by