How to use fnzeros to find the zeros of a function?
4 views (last 30 days)
Show older comments
For example I have y=sin(x*5)+sin(x*2) and I need to find where y=0 between x=-10 and x=+10
0 Comments
Accepted Answer
Yatin
on 15 Oct 2013
Edited: Yatin
on 15 Oct 2013
Hi,
The fnzeros function requires the inputs to be in a specific format as it is a function that works on splines and is a part of the Curve Fitting toolbox. You can however use " fzero " function to find roots iteratively in a loop until your root does not exceed the maximum limit of the range. So once you get the first zero(root), you can increment it by certain value to get the next zero. In this case 0.6 seems to be a reasonable guess. You can then insert all the roots in a vector. There are totally 33 zeros in this case but the above code finds only 27. So this is just a close approximation
A sample test code is as below:
imax = 10;
imin = -10;
fun = @(x)sin(5*x)+sin(2*x);
allZeros = [];
aZero1 = 0;
tempZero1 = imin;
while(tempZero1 <= imax)
aZero1 = fzero(fun, tempZero1);
tempZero1 = aZero1+ 0.6;
allZeros = [allZeros, aZero1];
end
allZeros = unique(allZeros);
0 Comments
More Answers (1)
Matt J
on 15 Oct 2013
Edited: Matt J
on 15 Oct 2013
Looks like you're meant to form a spline approximation to y=sin(x*5)+sin(x*2) using spmak and then apply fnzeros to get the zeros. You will probably want to use a small knot spacing and test agreement with the original y(x) via some plots.
Theoretically, it's not a bulletproof way of getting the zeros. A bulletproof method can't rely exclusively on numerical solvers like fzero or fnzero. It would require some prior analysis determine the minimal spacing between solutions, but as a homework exercise, I guess your instructor doesn't care.
0 Comments
See Also
Categories
Find more on Spline Postprocessing 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!