The aim is to find all the zeros of a function within an interval.
Input :
- fcn : an anonymous function (@sin, @cos...)
- lb : lower bound
- ub :upper bound
Output :
- output : vector with unique values for which the input function return zeroThe values must be sorted in ascending order.
Example
output = find_zeros(@sin,0,2*pi) will return :
output = [0.0000 3.1416 6.2832]
since the sinus function between [0 2pi] is zero for [0 pi 2pi]
Solution Stats
Problem Comments
5 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers24
Suggested Problems
-
Remove any row in which a NaN appears
8775 Solvers
-
1360 Solvers
-
129 Solvers
-
288 Solvers
-
Matrix with different incremental runs
584 Solvers
More from this Author30
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
I think test case 2 might be incorrect.
I disagree slightly with the expected solution to test 2.
Test 2 cos between [0 2pi]
[-pi/2 pi/2 3*pi/2]
I do not believe that -pi/2 is in the interval [0 2*pi].
If -pi/2 is desired then the answer to sin [0 2*pi] should be [-2*pi -pi 0 pi 2*pi]
Correction of assert function.
One of the ")" is in the wrong place.
is:
assert(all(abs(find_zeros(@sin,0,2*pi) -[0 pi 2*pi]<1e-9)))
Should be:
assert(all(abs(find_zeros(@sin,0,2*pi) -[0 pi 2*pi])<1e-9))
As mentioned by others, the 2nd test case is incorrect, -pi/2 is outside the interal [0,2*pi] for the function cosine. And if we do plot cos(x) on said interval, we notice that the cosine function crosses the x-axis only twice.
The –pi/2 has been removed from that test case.