What exactly am i suppose to do to run this code

5 visualizaciones (últimos 30 días)
Basil Mwila
Basil Mwila el 14 de Mayo de 2021
Comentada: Basil Mwila el 14 de Mayo de 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 comentarios
the cyclist
the cyclist el 14 de Mayo de 2021
Editada: the cyclist el 14 de Mayo de 2021
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?

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 14 de Mayo de 2021
Editada: the cyclist el 14 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by