how to continue program with fzero error

6 ビュー (過去 30 日間)
Bob Johnson
Bob Johnson 2011 年 2 月 24 日
Hi,
I am using fzero to solve a nonlinear univariate function. However, I am iterating over many parameter values. So, if fzero can't find a root for one set of parameters, that is fine, but I want it to continue to the next set. However, I keep getting the error
??? Error using ==> fzero at 293 The function values at the interval endpoints must differ in sign.
Can anyone tell me the proper way to handle this? Should I go into the function, comment out the error and put a default output, or is there a better way to do this. Thanks.
All the best, Bob

採用された回答

Matt Fig
Matt Fig 2011 年 2 月 24 日
Use a TRY-CATCH block. I'll assume you are looping over the parameter set and storing the roots in a variable S.
cnt = 1;
for ii = PARAMS
try
T = fzero(...);
cnt = cnt + 1;
S(cnt) = T; % If FZERO errors, this assignment won't happen.
catch
fprintf('No root found for parameter %f\n',ii); %optioinal
end
end
  2 件のコメント
Jan
Jan 2011 年 2 月 24 日
Voted: Catching an error is more general than avoiding it.
Bob Johnson
Bob Johnson 2011 年 2 月 24 日
Great! This is what I was looking for. Thanks a lot.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 2 月 24 日
Try-catch will certainly work, but I wonder why you don't just do the two endpoint evaluations yourself and skip the fzero call if the signs are the same?
  2 件のコメント
Jan
Jan 2011 年 2 月 24 日
Voted. Avoiding an error is superior to catching it.
Bob Johnson
Bob Johnson 2011 年 2 月 24 日
True. Thanks. I will do this as well. Much appreciated

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProblem-Based Optimization Setup についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by