필터 지우기
필터 지우기

How not to display error message from fzero

조회 수: 13 (최근 30일)
Roberto Gargiulo
Roberto Gargiulo 2021년 3월 9일
댓글: Roberto Gargiulo 2021년 3월 9일
I've defined the function:
function zeros = findzeros(myf,n_points,min,max)
x = ones(n_points,1);
starting_points=linspace(min,max,n_points);
% warning('off')
for i=1:n_points
try
x(i) = fzero(myf, starting_points(i));
catch
fprintf('No root found');
end
end
x = x(x>=min);
zeros = [x(diff(x)>1e-12); x(1)];
end
In the while looping if often happens that there's an error. I tried using trycatch as suggested elsewhere, but it still results in error messages such as:
Exiting fzero: aborting search for an interval containing a sign change
because no sign change is detected during search.
Function may not have a root.
And there's no messa 'No root found' printed. How can I fix this?

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 9일
function zeros = findzeros(myf, n_points, xmin, xmax)
x = ones(n_points,1);
starting_points = linspace(xmin, xmax, n_points);
opts = optimset('display', 'none');
for i=1:n_points
x(i) = fzero(myf, starting_points(i), opts);
end
x = x(x >= xmin);
zeros = [x(diff(x)>1e-12); x(1)];
end
  댓글 수: 1
Roberto Gargiulo
Roberto Gargiulo 2021년 3월 9일
Thanks a lot! Now it works perfectly (and the code has a better format)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by