File Exchange

## Bessel Zero Solver

version 1.1 (31.7 KB) by
Finds the zeros of besselj and bessely in an ordered fashion. No zeros are missed.

Updated 18 May 2018

View Version History

besselzero.m is the function. Other code is documentation.
This code is valid for abs(order) less than or equal 146222.16674537213 and 370030.762407380 for Bessel function of the first and second kind respectively. It is very fast and flexible too. 2 million zeros (0:10000 orders and 200 roots per order) can be found in 200 seconds on standard laptop. Accuracy is set to a default of 12 significant digits.
This code is based on another submission. I fixed the bugs, improved the documentation, improved the code quality, and improved the speed of the code. If you have problems, let me know. If I have time, I will fix them.
How it works:
the first three roots of any order Bessel can be approximated by a simple equations. These equations were generated using a least squares fit of the roots from orders of n=0:10000. The approximation is used to start the iteration of Halley's method. The 4th and higher roots can be approximated by understanding the roots are regularly spaced for a given order. Once the 2nd and 3rd roots are found, the spacing can be approximated by the distance between the 2nd and 3rd root. Then again Halley's method can be applied to precisely locate the root.Because the algorithm depends on good guesses of the first three zeros, if the guess is to far away then Halley's method will converge to the wrong zero which will subsequently cause any other zero to be incorrectly located. Therefore, a limit is put on Bessel order, abs(n), of 146222.16674537213 and 370030.762407380 for first and second kind respectively. If n is specified above these limits, then an error is thrown.

### Cite As

Jason Nicholson (2021). Bessel Zero Solver (https://www.mathworks.com/matlabcentral/fileexchange/48403-bessel-zero-solver), MATLAB Central File Exchange. Retrieved .

José Irusta Solera

Olufolahan Ogidan

Thank you for your help, and yes I am new to MATLAB.

Jason Nicholson

1. Yes you can set the path in an educational version. I cannot troubleshoot this via the comment section because it could be some many things ranging from your IT department to windows or even some other things. I am not sure.
2. From your posts here and what you are struggling with, you need help with learning MATLAB. I think you should aim to get help with MATLAB. I would recommend calling Mathworks if you have a license that will allow this (it does not seem like you do but I am not sure). I would also recommend MATLAB Answers: https://www.mathworks.com/matlabcentral/answers/index/?s_tid=gn_mlc_an

Olufolahan Ogidan

I have tried putting it in the same directory and its not working, I am tasing the code in the editor tab . I also cannot set the path as it is an educational license, for the school am running MATLAB on.

Jason Nicholson

Just put besselzero in the same directory as your script. Or if you are using the command line, make the current directory the directory where besselzero is located.

Olufolahan Ogidan

thank you for your quick response Jason, can you please direct me as to adding the besseslzero() function on my MATLAB path. I guessed that was the issue and tried checking to see how to add it but was not able to. thanks again

Jason Nicholson

Olufolahan Ogidan, the code snippet you gave me runs on my computer with MATLAB 2018b. The code I used is repeated again for directness and clarity:

clc; clear; close all;
n = (1:2)';
k = 10;
kind = 1;
z = besselzero(n, k, kind);
x = linspace(0, z(end), 1000);
y = nan(2, length(x));
y(1,:) = besselj(n(1), x);
y(2,:) = besselj(n(2), x);
nz = nan(size(z));
nz(1,:) = besselj(n(1), z(1,:));
nz(2,:) = besselj(n(2), z(2,:));
plot(x, y, z, nz,'kx')

I think that besselzero() function isn't on your MATLAB path.

Olufolahan Ogidan

I tried running this and the example on this discussion. am trying to calculate the Bessel zeros of the first and second kind.
here is the code
n = (1:2)';
k = 10;
kind = 1;
z = besselzero(n, k, kind);
x = linspace(0, z(end), 1000);
y = nan(2, length(x));
y(1,:) = besselj(n(1), x);
y(2,:) = besselj(n(2), x);
nz = nan(size(z));
nz(1,:) = besselj(n(1), z(1,:));
nz(2,:) = besselj(n(2), z(2,:));
plot(x, y, z, nz,'kx')

and the ERROR
Error in zerossss (line 4)
z = besselzero(n, k, kind);

I also tried Jason's code below here but its the same kind of error am getting. Please help, am new to Matlab.
thanks

Jason Nicholson

Yao, I don't see the issue. Use the "contact" link to drop me an email. The code below does exactly what I think it should.

clc; clear; close all;

% This finds the first 5 zeros the order 1 bessel function of the first
% kind
order = 1;
kind =1;
numberOfZeros = 5;
zerosOfBesselOfFirstKind = besselzero(order,numberOfZeros,kind);

% This finds the first 5 zeros the order 1 bessel function of the second
% kind
kind =2;
zerosOfBesselOfSecondKind = besselzero(order,numberOfZeros,kind);

% plot order 1 bessel functions of first and second kind
x = linspace(0,max([zerosOfBesselOfFirstKind, zerosOfBesselOfSecondKind]), 1000);
j1 = besselj(order,x); % bessel function of first kind
y1 = bessely(order,x); % bessel function of second kind
plot(x,j1,'b');
xlim(xlim); ylim(ylim); % causes the axis limits to not update due to bessel function of second kind
hold all;
plot(x,y1,'r');

% plot zeros

plot(zerosOfBesselOfFirstKind,zeros(1,numberOfZeros),'bx', ...
zerosOfBesselOfSecondKind,zeros(1,numberOfZeros),'xr');

legend({'Bessel Function of 1st Kind', ...
'Bessel Function of 2nd Kind', ...
sprintf('First %d zeros of Bessel Function of 1st Kind',numberOfZeros), ...
sprintf('First %d zeros of Bessel Function of 2nd Kind',numberOfZeros)});

Luming YAO

I checked again when the order n=-0.5, this mistake will happen.

Luming YAO

Hi, I have a little problem when I use this code to solve the zeros of Bessel functions. When I set kind=1, the solution is the zeros for Bessel function of the second kind.

Mazin Mustafa

Jason Nicholson

Honcai, you miss understand the definition of k for "besselzero.m". k is the number of zeros. k cannot be 0.

Hongcai Chen

I think this code is missing the k=0 point for n>1 cases. Please check.

Igor Gayday

zip-file contains only license, without the actual code

##### MATLAB Release Compatibility
Created with R2014b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux