File Exchange

image thumbnail

Nzeros(fun, xmin, xmax, N)

version 1.3.0.0 (1.45 KB) by Lateef Adewale Kareem
%% This function computes atmost N zeros (z) between xmin and xmax

1 Download

Updated 27 Oct 2017

View Version History

View License

%% This function computes atmost N zeros (z) between xmin and xmax
% fun is the handle to the function whose zeros we want to compute
% xmin is the start of the search region
% xmax is the end of the search region
% N is the maximum number of the zeros we sort
% Example 1: z = Nzeros(@(x)besselj(0,x), 0, 20, 7)
% Example 2: z = Nzeros(@(x)sin(x*pi), 0, 20, 7)
% Example 3: z = Nzeros(@(x)cos(x*pi), 0, 20, 7)
% Example 4: z = Nzeros(@(x)besselj(0,x) - 2*besselj(1,x), 0, 20, 7)
% Example 5: z = Nzeros(@(x)3*sin(2*x) - 2*cos(3*x), 0, 20, 7)

Cite As

Lateef Adewale Kareem (2021). Nzeros(fun, xmin, xmax, N) (https://www.mathworks.com/matlabcentral/fileexchange/64844-nzeros-fun-xmin-xmax-n), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Lateef Adewale Kareem

good one Viktor. Nzeros does not have such problem

Viktor Witkovsky

Note of caution (!) FindRoots has problems with zeros at the inflection points, so it will crash at your example z = Nzeros(@(x)3*sin(2*x) - 2*cos(3*x), 0, 20, 7) ... (;-)

Lateef Adewale Kareem

usually in solution of PDE, the effect of each term is proportional to exponent of -root^2*t. so it dies off quickly. if one of your roots is 50, then you have exp(-2500*t) multiplying every other members of the term. it will most likely disappear from the computation.

so why waste computational effort on something that is negligible.?

Carl Witthoft

Right, so why do you allow limiting the number returned? You might not get the one you "want."

Lateef Adewale Kareem

lol. @Carl Witthoft. It actually can find all roots in that domain. if your N is more than the number of roots in the domain, you would get all in that domain plus 1.

Carl Witthoft

I'm intrigued: why did you want to find only **some** of the zeros in a given domain? I might point out in passing that failure to find all solutions was a design failure that played a key role in the plot of Jurassic Park.

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

Community Treasure Hunt

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

Start Hunting!