What exactly am i suppose to do to run this code
조회 수: 5 (최근 30일)
이전 댓글 표시
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
the cyclist
2021년 5월 14일
편집: the cyclist
2021년 5월 14일
Here is the re-formatted code:
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);
end
I notice you define
n = 6;
but also have a hard-coded value of 6 in the code. Guessing maybe that is not intended?
채택된 답변
the cyclist
2021년 5월 14일
편집: the cyclist
2021년 5월 14일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 General Applications에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!