How to use fnzeros to find the zeros of a function?

조회 수: 7 (최근 30일)
Tristan
Tristan 2013년 10월 6일
편집: Matt J 2013년 10월 15일
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

채택된 답변

Yatin
Yatin 2013년 10월 15일
편집: Yatin 2013년 10월 15일
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);

추가 답변 (1개)

Matt J
Matt J 2013년 10월 15일
편집: Matt J 2013년 10월 15일
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.

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by