Reputation: 925
Consider the following function:
fun=@(x) tan(x);
x = NaN*ones(100,1); % Initializes x.
starting_points=linspace(0,4*pi,100);
for i=1:100
% Look for the zeros in the function's current window.
x(i)=fzero(fun, starting_points(i));
end
x_unique=x(diff(x)>1e-12)
The output of the function is:
x_unique =
-0.0000 1.5708 3.1416 4.7124 6.2832 7.8540 9.4248 10.9956
We know that this is not true as tan has only 5 roots namely 0, pi, 2*pi, 3*pi and 4*pi in the interval [0, 4*pi]. The problem is- fzero gives x value as output where the function becomes discontinuous. How to rectify this problem? Is there a workaround?
Upvotes: 0
Views: 178
Reputation: 925
I finally found the workaround. Thanks to @daniel
k=1;
for i =1:size(x_unique)
if abs(fun(x_unique(i))) < 0.01
R(k)=x_unique(i);
k=k+1;
end
end
Add this to the end of the code. This will give only the zeros of the given function.
Upvotes: 1