MATLAB Answers

What exactly am i suppose to do to run this code

1 view (last 30 days)
Basil Mwila
Basil Mwila on 14 May 2021
Commented: Basil Mwila on 14 May 2021
function Ylm = sh(l, m); % function Ylm = sh(l, m); % Draw the equipotential surface of a multipole potential based on % Re Ylm, a spherical harmonic, degree l, order m % Also return numerical values evenly spaced in angle of Re Ylm. n = 6; % Number of samples per half wavelength N = max([30, l*6]); theta = [0 : N]' *pi/N; phi =[-N : N] *pi/N;
% Create the surface of Re Ylm(theta,phi)/r^(l+1) = 1; % Solves for r on a theta, phi grid gamma = 1/(l+1); all = legendre(l, cos(theta)); if (l == 0) all=all'; end % Compensate for error in legendre Ylm = all(m+1, :)' * cos(m*phi); r = abs(Ylm) .^ gamma;
% Convert to Cartesian coordinates X = r.* (sin(theta)*cos(phi)) ; Y = r.* (sin(theta)*sin(phi)); Z = r.* (cos(theta)*ones(size(phi)));
% Color according to size and sign of Ylm C = Ylm;
surf(X, Y, Z, C) axis equal
colormap hot ti=['Surface Y_l^m/r^{l+1}=1 with l=' int2str(l) ', m=' int2str(m)]; title(ti);
  4 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 14 May 2021
Edited: the cyclist on 14 May 2021
You can run that code by putting it into a file (that would canonically be called sh.m), and then calling that function from the workspace, for example as
l = 2;
m = 1;
Ylm = sh(l,m)
I would recommend calling the function something other than sh, to avoid confusion with shell commands. Also, I recommend against using the variable name all, because that is a MATLAB command.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!