MATLAB Answers


Matrix dimensions must agree for computing spherical harmonics !!!!

Asked by Ano
on 9 Mar 2017
Latest activity Answered by Ano
on 10 Mar 2017

Hi i am trying to calculate the spherical harmonic but I get an error saying that the matrices dimensions must agree, here is the code, any suggesions are very welcomed!

    theta  =linspace(0,pi,5); %theta ranges from 0 to pi
    phi    =linspace(0,  2*pi,5);  %phi ranges from  0 to 2*pi
    for m=-L:L 
       cst  = sqrt((2*L+1)*factorial(L-m)/((4*pi)*factorial(L+m))); %constant of spherical harmonics
       P   = legendre(L,cos(theta));
       Y   = cst.*P.*exp(-1j.*m.*phi);


I'm not getting any errors when I run your code in Matlab (R2016b). Which line were you having trouble with?

cst.*P.*exp(-1j.*m.*phi) In this P is 5x5 matrix, exp(-1j*m*phi) is expect output to be a matrix or array?

Log in to comment.

1 Answer

Answer by Ano
on 10 Mar 2017

the output should be a matrix I have solve it by using the transpose of P now it works! Thank you!!


Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today