What exactly am i suppose to do to run this code

5 ビュー (過去 30 日間)
Basil Mwila
Basil Mwila 2021 年 5 月 14 日
コメント済み: Basil Mwila 2021 年 5 月 14 日
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
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?
Basil Mwila
Basil Mwila 2021 年 5 月 14 日
No

サインインしてコメントする。

採用された回答

the cyclist
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.
  1 件のコメント
Basil Mwila
Basil Mwila 2021 年 5 月 14 日
Thank you so much this was helpful....

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by