Unrecognized "spharm" for spherical harmonics plot?
10 views (last 30 days)
Show older comments
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 Comments
Umar
on 5 Jul 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
on 5 Jul 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.
Answers (1)
Umar
on 5 Jul 2024
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.
0 Comments
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!