Unrecognized "spharm" for spherical harmonics plot?
Mostrar comentarios más antiguos
Hi, I get the following error when trying to plot
Unrecognized function or variable 'spharm'.
Error in FTyD (line 35)
Ylm = spharm(l, m, Y, X); % Evaluate spherical harmonic Y_lm(x,y)
% Define the function f(t)
f = @(t) 139.85 + (15.8404 + 4.76022i) * exp(-1i * t) + (15.8404 - 4.76022i) * exp(1i * t) + ...
(4.64917 - 3.3024i) * exp(-2i * t) + (4.64917 + 3.3024i) * exp(2i * t) + ...
(7.42191 - 0.300123i) * exp(-3i * t) + (7.42191 + 0.300123i) * exp(3i * t) + ...
(0.340877 - 2.54665i) * exp(-4i * t) + (0.340877 + 2.54665i) * exp(4i * t) + ...
(7.72422 + 6.71332i) * exp(-5i * t) + (7.72422 - 6.71332i) * exp(5i * t) + ...
(3.16511 - 9.14479i) * exp(-6i * t) + (3.16511 + 9.14479i) * exp(6i * t) + ...
(3.31502 + 2.18874i) * exp(-7i * t) + (3.31502 - 2.18874i) * exp(7i * t) + ...
(9.31827 + 6.94538i) * exp(-8i * t) + (9.31827 - 6.94538i) * exp(8i * t) + ...
(5.87173 + 18.8341i) * exp(-9i * t) + (5.87173 - 18.8341i) * exp(9i * t) + ...
(8.75949 + 14.7107i) * exp(-10i * t) + (8.75949 - 14.7107i) * exp(10i * t) + ...
(19.2903 + 7.78329i) * exp(-11i * t) + (19.2903 - 7.78329i) * exp(11i * t) + ...
(-3.39063 - 18.5502i) * exp(-12i * t) + (-3.39063 + 18.5502i) * exp(12i * t) + ...
(3.58427 + 18.7438i) * exp(-13i * t) + (3.58427 - 18.7438i) * exp(13i * t) + ...
(2.66129 + 20.0781i) * exp(-14i * t) + (2.66129 - 20.0781i) * exp(14i * t) + ...
(-8.46335 - 9.96867i) * exp(-15i * t) + (-8.46335 + 9.96867i) * exp(15i * t) + ...
(-8.50037 - 1.20377i) * exp(-16i * t) + (-8.50037 + 1.20377i) * exp(16i * t) + ...
(-1.36102 - 16.9315i) * exp(-17i * t) + (-1.36102 + 16.9315i) * exp(17i * t) + ...
(-5.78964 - 2.96094i) * exp(-18i * t) + (-5.78964 + 2.96094i) * exp(18i * t) + ...
(2.14681 + 8.29635i) * exp(-19i * t) + (2.14681 - 8.29635i) * exp(19i * t) + ...
(-3.91145 - 10.7712i) * exp(-20i * t) + (-3.91145 + 10.7712i) * exp(20i * t);
% Parameters
lmax = 10; % Maximum degree for spherical harmonics expansion
% Create grid in x and y
x = linspace(-10, 10, 100);
y = linspace(-10, 10, 100);
[X, Y] = meshgrid(x, y);
% Compute f(x,y)
Fxy = zeros(size(X));
for l = 0:lmax
for m = -l:l
Ylm = spharm(l, m, Y, X); % Evaluate spherical harmonic Y_lm(x,y)
integral_value = integral(@(t) f(t) .* conj(Ylm), -pi, pi); % Compute integral
Fxy = Fxy + integral_value * Ylm; % Accumulate to f(x,y)
end
end
% Plot
figure;
surf(X, Y, real(Fxy), 'EdgeColor', 'none');
xlabel('x');
ylabel('y');
zlabel('Re(f(x,y))');
title('Method 3: Expansion in Spherical Harmonics');
colorbar;
Cant figure out why...
6 comentarios
Do you have
function y = spharm( ltab, mtab, theta, phi )
% SPHARM - Spherical harmonics.
%
% Usage :
% y = spharm( ltab, mtab, theta, phi )
% Input
% ltab : table of spherical harmonic degrees
% mtab : table of spherical harmonic orders
% theta : polar angle
% phi : azimuthal angle
% Output
% y : spherical harmonic
% convert to column and row vectors
[ ltab, mtab ] = deal( reshape( ltab, [], 1 ), reshape( mtab, [], 1 ) );
[ theta, phi ] = deal( reshape( theta, 1, [] ), reshape( phi, 1, [] ) );
% table of factorials
ftab = factorial( 0 : ( 2 * max( ltab ) + 1 ) );
% dimension output
y = zeros( length( ltab ), length( theta ) );
% loop over unique ltab values
for l = reshape( unique( ltab ), 1, [] )
% full table of Legendre functions
ptab = legendre( l, cos( theta ) );
% index to entries with degree L and
% with angular order smaller than angular order
ind = find( ltab == l );
ind = ind( abs( mtab( ind ) ) <= l );
for i = reshape( ind, 1, [] )
% corresponding spherical order (only absolute value considered first)
m = mtab( i );
% prefactor for spherical harmonics
c = sqrt( ( 2 * l + 1 ) / ( 4 * pi ) * ...
ftab( l - abs( m ) + 1 ) / ftab( l + abs( m ) + 1 ) );
% spherical harmonics
y( i, : ) = c * ptab( abs( m ) + 1, : ) .* exp( 1i * abs( m ) * phi );
% correct for negative orders
if ( m < 0 ), y( i, : ) = ( - 1 ) ^ m * conj( y( i, : ) ); end
end
end
end
on your MATLAB path ?
And conj(Ylm) in your integral is just a constant vector that won't depend on t and the length of which is incompatible with the t-vector supplied by the "integral"-function.
Sergio
el 5 de Jul. de 2024
Sergio
el 5 de Jul. de 2024
Umar
el 5 de Jul. de 2024
Hi Sergio,
Try this
% Add the sphharm function to your existing code % Code from: https://viewer.mathworks.com/?viewer=plain_code&url=https%3A%2F%2Fjp.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2Fe5abb292-4a80-11e4-9553-005056977bd0%2Fd3323a9d-1c0f-f373-1ffc-69bab360c2c3%2Ffiles%2F%40spherefun%2Fsphharm.m&embed=web
% Paste the code from the provided link here
% This code adds the sphharm function to your Matlab environment
Torsten
el 5 de Jul. de 2024
How would the total code look like? Can I just copy that in?
It depends on your MATLAB release. Usually you can just copy it underneath your script. For earlier MATLAB releases, you should save it as "spharm.m" in your working directory.
Respuestas (1)
Umar
el 5 de Jul. de 2024
1 voto
Hi Sergio,
To help resolve your problem, you need to define the spharm function or find an alternative way to evaluate the spherical harmonics.One possible solution is to use the sphharm function from the MATLAB File Exchange. This function provides a way to evaluate spherical harmonics. You can download the sphharm function from the MATLAB File Exchange website and add it to your MATLAB path.
For more information regarding this function, please refer to
https://viewer.mathworks.com/?viewer=plain_code&url=https%3A%2F%2Fjp.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2Fe5abb292-4a80-11e4-9553-005056977bd0%2Fd3323a9d-1c0f-f373-1ffc-69bab360c2c3%2Ffiles%2F%40spherefun%2Fsphharm.m&embed=web
Hope this will help resolve your problem.
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!